|
| 1 | +import { oc } from "@orpc/contract"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { ProductSchema, ProductPriceSchema } from "../products"; |
| 4 | +import { |
| 5 | + PaginationInputSchema, |
| 6 | + PaginationOutputSchema, |
| 7 | +} from "../../schemas/pagination"; |
| 8 | +import { ProductPriceInputSchema } from "../../schemas/product-price-input"; |
| 9 | + |
| 10 | +// Output schema - product with its active price |
| 11 | +// Note: Uses modifiedAt to match Prisma schema naming |
| 12 | +const ProductWithPriceSchema = ProductSchema.omit({ prices: true }).extend({ |
| 13 | + price: ProductPriceSchema.nullable(), |
| 14 | + userMetadata: z.record(z.string(), z.any()).nullable(), |
| 15 | + createdAt: z.date(), |
| 16 | + modifiedAt: z.date().nullable(), |
| 17 | +}); |
| 18 | + |
| 19 | +const ListProductsOutputSchema = PaginationOutputSchema.extend({ |
| 20 | + products: z.array(ProductWithPriceSchema), |
| 21 | +}); |
| 22 | + |
| 23 | +// Create input - NO benefitIds (not exposed on dashboard MCP) |
| 24 | +const CreateProductInputSchema = z.object({ |
| 25 | + name: z.string().min(1), |
| 26 | + description: z.string().optional(), |
| 27 | + price: ProductPriceInputSchema, |
| 28 | + userMetadata: z.record(z.string(), z.string()).optional(), |
| 29 | +}); |
| 30 | + |
| 31 | +// Update input - includes price (matches dashboard updateProduct), NO benefitIds |
| 32 | +const UpdateProductInputSchema = z.object({ |
| 33 | + id: z.string(), |
| 34 | + name: z.string().min(1).optional(), |
| 35 | + description: z.string().optional(), |
| 36 | + price: ProductPriceInputSchema.optional(), // Can update pricing (immutable pattern applies) |
| 37 | + userMetadata: z.record(z.string(), z.string()).optional(), |
| 38 | +}); |
| 39 | + |
| 40 | +export const listProductsContract = oc |
| 41 | + .input(PaginationInputSchema) |
| 42 | + .output(ListProductsOutputSchema); |
| 43 | + |
| 44 | +export const getProductContract = oc |
| 45 | + .input(z.object({ id: z.string() })) |
| 46 | + .output(ProductWithPriceSchema); |
| 47 | + |
| 48 | +export const createProductContract = oc |
| 49 | + .input(CreateProductInputSchema) |
| 50 | + .output(ProductWithPriceSchema); |
| 51 | + |
| 52 | +export const updateProductContract = oc |
| 53 | + .input(UpdateProductInputSchema) |
| 54 | + .output(ProductWithPriceSchema); |
| 55 | + |
| 56 | +export const deleteProductContract = oc |
| 57 | + .input(z.object({ id: z.string() })) |
| 58 | + .output(z.object({ ok: z.literal(true) })); |
| 59 | + |
| 60 | +export const products = { |
| 61 | + list: listProductsContract, |
| 62 | + get: getProductContract, |
| 63 | + create: createProductContract, |
| 64 | + update: updateProductContract, |
| 65 | + delete: deleteProductContract, |
| 66 | +}; |
0 commit comments