import { createContext, DomainError } from "@tailor-platform/erp-kit/app"; import { createResolver, t } from "@tailor-platform/sdk"; import { getDB } from "@/generated/kysely-tailordb"; import { invModulesExport } from "@/modules"; export default createResolver({ name: "openTransferOrder", operation: "mutation", input: { id: t.string().description("Transfer order ID"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await invModulesExport.commands.openTransferOrder( trx, { id: context.input.id }, ctx, ); if (!result.ok) { switch (result.error.code) { case "INVENTORY_TRANSFER_ORDER_NOT_FOUND": throw new DomainError(`Transfer order not found: ${context.input.id}`); case "INVENTORY_INVALID_STATE_TRANSITION": throw new DomainError( `Transfer order cannot be opened in its current status: ${context.input.id}`, ); case "INVENTORY_INSUFFICIENT_AVAILABLE_STOCK": throw new DomainError( "Cannot open this transfer order: one or more lines request " + "more than the stock available at the source site.", ); case "INVENTORY_EMPTY_TRANSFER_LINES": throw new DomainError("The transfer order has no lines to open."); 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.transferOrder.id, status: result.value.transferOrder.status, }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to open the transfer order", { cause: err }); }); }, output: t .object({ id: t.string().description("Transfer order ID"), status: t.string().description("New document status"), }) .description("OpenTransferOrder response"), });