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"; /** * closeAccountingPeriod — close an OPEN accounting period so no further entries * can post (OPEN → CLOSED). Thin wrapper over the native `closePeriod`. A closed * period can be reopened via openAccountingPeriod. */ export default createResolver({ name: "closeAccountingPeriod", operation: "mutation", input: { id: t.string().description("Accounting period ID"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await finModulesExport.commands.closePeriod( trx, { id: context.input.id }, ctx, ); if (!result.ok) { switch (result.error.code) { case "FINANCIAL_ACCOUNTING_ACCOUNTING_PERIOD_NOT_FOUND": throw new DomainError(`Accounting period not found: ${context.input.id}`); case "FINANCIAL_ACCOUNTING_INVALID_STATUS_TRANSITION": throw new DomainError("Only an OPEN accounting period can be closed."); 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.accountingPeriod.id, status: result.value.accountingPeriod.status, }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to close the accounting period", { cause: err }); }); }, output: t .object({ id: t.string().description("Accounting period ID"), status: t.string().description("New period status (CLOSED)"), }) .description("CloseAccountingPeriod response"), });