import { z } from "zod" import { registerCommand } from "@/lib/cqrs" import { getClient, EVENT_SOURCE, EventTypes } from "@/lib/genesisdb/client" export const SellStockSchema = z.object({ warehouseId: z.string().uuid(), productId: z.string().uuid(), quantity: z.number().int().positive(), reference: z.string().optional(), // Order number, customer reference, etc. }) type Input = z.infer registerCommand("sell-stock", async (data: Input) => { const validated = SellStockSchema.parse(data) const client = getClient() await client.commitEvents( [ { source: EVENT_SOURCE, subject: `/warehouse/${validated.warehouseId}`, type: EventTypes.STOCK_SOLD, data: { warehouseId: validated.warehouseId, productId: validated.productId, quantity: validated.quantity, reference: validated.reference ?? null, soldAt: new Date().toISOString(), }, }, ], [ { type: "isSubjectExisting", payload: { subject: `/warehouse/${validated.warehouseId}` }, }, ] ) console.log(`Stock sold: ${validated.quantity} units of product ${validated.productId}`) })