import { createContext, DomainError } from "@tailor-platform/erp-kit/app"; import { createResolver, t } from "@tailor-platform/sdk"; import { getDB } from "@/generated/kysely-tailordb"; import { outboundShipmentModulesExport } from "@/modules"; /** * cancelOutboundShipment — abandon a DRAFT outbound shipment (DRAFT → CANCELLED). Thin * wrapper over the outbound-shipment `cancelOutboundShipment` command. A posted * outbound shipment cannot be cancelled. */ export default createResolver({ name: "cancelOutboundShipment", operation: "mutation", input: { id: t.string().description("Outbound shipment (OutboundShipment) ID"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await outboundShipmentModulesExport.commands.cancelOutboundShipment( trx, { id: context.input.id }, ctx, ); if (!result.ok) { switch (result.error.code) { case "OUTBOUND_SHIPMENT_INVALID_STATUS": throw new DomainError( `Outbound shipment cannot be cancelled in its current status: ${context.input.id}`, ); 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.outboundShipmentId, // A successful transition implies the target status. status: "CANCELLED", }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to cancel the outbound shipment", { cause: err }); }); }, output: t .object({ id: t.string().description("Outbound shipment (OutboundShipment) ID"), status: t.string().description("New document status (CANCELLED)"), }) .description("CancelOutboundShipment response"), });