import { createContext, DomainError } from "@tailor-platform/erp-kit/app"; import { createResolver, t } from "@tailor-platform/sdk"; import { getDB } from "@/generated/kysely-tailordb"; import { finModulesExport } from "@/modules"; /** * cancelJournalEntry — cancel a DRAFT journal entry (DRAFT → CANCELLED). * * Thin wrapper over the native `cancelJournalEntry`. Only DRAFT entries can be * cancelled; a POSTED entry must be reversed instead. */ export default createResolver({ name: "cancelJournalEntry", operation: "mutation", input: { id: t.string().description("Journal entry ID"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await finModulesExport.commands.cancelJournalEntry( trx, { journalEntryId: context.input.id }, ctx, ); if (!result.ok) { switch (result.error.code) { case "FINANCIAL_ACCOUNTING_JOURNAL_ENTRY_NOT_FOUND": throw new DomainError(`Journal entry not found: ${context.input.id}`); case "FINANCIAL_ACCOUNTING_INVALID_STATUS_FOR_CANCEL": throw new DomainError("Only a DRAFT journal entry can be cancelled."); 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.journalEntry.id, status: result.value.journalEntry.status, }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to cancel the journal entry", { cause: err }); }); }, output: t .object({ id: t.string().description("Journal entry ID"), status: t.string().description("New document status (CANCELLED)"), }) .description("CancelJournalEntry response"), });