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: "deleteSalesItem", operation: "mutation", input: { itemId: t.string().description("Item to withdraw from selling"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await salesModulesExport.commands.deleteSalesItem( trx, { itemId: context.input.itemId }, ctx, ); if (!result.ok) { switch (result.error.code) { case "SALES_ITEM_NOT_SELLABLE": throw new DomainError(`Item has no 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 withdraw the item from selling", { cause: err }); }); }, output: t .object({ id: t.string().description("Deleted sales item ID"), }) .description("DeleteSalesItem response"), });