import { createContext, DomainError } from "@tailor-platform/erp-kit/app"; import { createResolver, t } from "@tailor-platform/sdk"; import { getDB } from "@/generated/kysely-tailordb"; import { imModulesExport } from "@/modules"; export default createResolver({ name: "updateItem", operation: "mutation", input: { id: t.string().description("Item ID"), name: t.string({ optional: true }).description("Item name"), barcode: t.string({ optional: true }).description("Item barcode"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await imModulesExport.commands.updateItem( trx, { id: context.input.id, name: context.input.name ?? undefined, barcode: context.input.barcode ?? undefined, }, ctx, ); if (!result.ok) { switch (result.error.code) { case "ITEM_MANAGEMENT_ITEM_NOT_FOUND": throw new DomainError(`Item not found: ${context.input.id}`); case "ITEM_MANAGEMENT_NO_FIELDS_TO_UPDATE": throw new DomainError("No fields provided to update"); case "ITEM_MANAGEMENT_DUPLICATE_BARCODE": throw new DomainError(`Barcode already in use: ${context.input.barcode}`); case "ITEM_MANAGEMENT_UNIT_NOT_FOUND": throw new DomainError("A referenced unit was not found."); case "ITEM_MANAGEMENT_UOM_LOCKED": throw new DomainError("The item's unit of measure is locked and cannot be changed."); 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.item.id }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to update the item", { cause: err }); }); }, output: t .object({ id: t.string().description("Updated item ID"), }) .description("Updated item"), });