import { createContext, DomainError } from "@tailor-platform/erp-kit/app"; import { createResolver, t } from "@tailor-platform/sdk"; import { getDB } from "@/generated/kysely-tailordb"; import { salesModulesExport } from "@/modules"; export default createResolver({ name: "createSalesItem", operation: "mutation", input: { itemId: t.string().description("Item to adopt for selling"), requiresPhysicalFulfillment: t .bool() .description("Whether selling this item is expected to produce an outbound shipment"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await salesModulesExport.commands.createSalesItem( trx, { itemId: context.input.itemId, requiresPhysicalFulfillment: context.input.requiresPhysicalFulfillment, }, ctx, ); if (!result.ok) { switch (result.error.code) { case "SALES_ITEM_NOT_FOUND": throw new DomainError(`Item not found: ${context.input.itemId}`); case "SALES_DUPLICATE_SALES_ITEM": throw new DomainError(`Item already has a selling record: ${context.input.itemId}`); case "UNAUTHENTICATED": throw new DomainError("Authentication is required"); case "INSUFFICIENT_PERMISSION": throw new DomainError("You do not have permission to perform this action"); default: throw result.error satisfies never; } } return { id: result.value.salesItemId }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to adopt the item for selling", { cause: err }); }); }, // Entity-typed so clients can select SalesItem fields off the result. output: t.object({ id: t.uuid() }).typeName(salesModulesExport.db.salesItem.name), });