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"; /** * openAccountingPeriod — open an accounting period so journal entries can post. * Thin wrapper over the native `openPeriod` (NEVER_OPENED → OPEN, or CLOSED → * OPEN via reopen). */ export default createResolver({ name: "openAccountingPeriod", 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.openPeriod( 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( "The accounting period cannot be opened from its current status.", ); 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 open the accounting period", { cause: err }); }); }, output: t .object({ id: t.string().description("Accounting period ID"), status: t.string().description("New period status (OPEN)"), }) .description("OpenAccountingPeriod response"), });