// @ts-nocheck import { mutation, query } from "./_generated/server"; import { v } from "convex/values"; const productArgs = { id: v.optional(v.id("products")), name: v.string(), description: v.optional(v.string()), price: v.number(), currency: v.string(), images: v.array(v.string()), metadata: v.optional(v.record(v.string(), v.string())), stripeProductId: v.optional(v.string()), stripePriceId: v.optional(v.string()), active: v.boolean(), featured: v.optional(v.boolean()), }; export const listActive = query({ args: {}, handler: async (ctx) => { return await ctx.db .query("products") .withIndex("by_active", (q) => q.eq("active", true)) .collect(); }, }); export const listAdmin = query({ args: { search: v.optional(v.string()), }, handler: async (ctx, args) => { const products = await ctx.db.query("products").collect(); const search = args.search?.trim().toLowerCase(); if (!search) { return products.sort((a, b) => a.name.localeCompare(b.name)); } return products .filter((product) => { return ( product.name.toLowerCase().includes(search) || product.description?.toLowerCase().includes(search) ); }) .sort((a, b) => a.name.localeCompare(b.name)); }, }); export const getById = query({ args: { id: v.id("products"), }, handler: async (ctx, args) => { return await ctx.db.get(args.id); }, }); export const upsert = mutation({ args: productArgs, handler: async (ctx, args) => { const now = Date.now(); if (args.id) { await ctx.db.patch(args.id, { name: args.name, description: args.description, price: args.price, currency: args.currency, images: args.images, metadata: args.metadata, stripeProductId: args.stripeProductId, stripePriceId: args.stripePriceId, active: args.active, featured: args.featured, updatedAt: now, }); return await ctx.db.get(args.id); } const id = await ctx.db.insert("products", { name: args.name, description: args.description, price: args.price, currency: args.currency, images: args.images, metadata: args.metadata, stripeProductId: args.stripeProductId, stripePriceId: args.stripePriceId, active: args.active, featured: args.featured, createdAt: now, updatedAt: now, }); return await ctx.db.get(id); }, }); export const deactivate = mutation({ args: { id: v.id("products"), }, handler: async (ctx, args) => { await ctx.db.patch(args.id, { active: false, updatedAt: Date.now(), }); return await ctx.db.get(args.id); }, }); export const bulkDeactivate = mutation({ args: { ids: v.array(v.id("products")), }, handler: async (ctx, args) => { const now = Date.now(); const results = []; for (const id of args.ids) { await ctx.db.patch(id, { active: false, updatedAt: now }); results.push(await ctx.db.get(id)); } return results; }, });