import { createContext, DomainError } from "@tailor-platform/erp-kit/app"; import { createResolver, t } from "@tailor-platform/sdk"; import { getDB } from "@/generated/kysely-tailordb"; import { apModulesExport } from "@/modules"; /** * cancelOutgoingPayment — cancel a DRAFT outgoing payment. * * Cancellation discards an unposted payment (and its draft settlements) and * moves it DRAFT -> CANCELLED. It never touches the general ledger — a POSTED * payment must be reversed (reverseOutgoingPayment), not cancelled. `id` is the * OutgoingPayment id. */ export default createResolver({ name: "cancelOutgoingPayment", operation: "mutation", input: { id: t.string().description("Outgoing payment ID"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await apModulesExport.commands.cancelOutgoingPayment( trx, { id: context.input.id }, ctx, ); if (!result.ok) { switch (result.error.code) { case "ACCOUNT_PAYABLE_OUTGOING_PAYMENT_NOT_FOUND": throw new DomainError(`Outgoing payment not found: ${context.input.id}`); case "ACCOUNT_PAYABLE_OUTGOING_PAYMENT_INVALID_STATUS": throw new DomainError(`Only a DRAFT payment can be cancelled: ${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.outgoingPayment.id, status: result.value.outgoingPayment.status, }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to cancel the outgoing payment", { cause: err }); }); }, output: t .object({ id: t.string().description("Cancelled outgoing payment ID"), status: t.string().description("Lifecycle status (CANCELLED after cancellation)"), }) .description("Cancelled outgoing payment"), });