import { createContext, DomainError } from "@tailor-platform/erp-kit/app"; import { createResolver, t } from "@tailor-platform/sdk"; import { getDB } from "@/generated/kysely-tailordb"; import { invModulesExport } from "@/modules"; export default createResolver({ name: "publishStandardCost", operation: "mutation", input: { itemId: t.string().description("Item ID"), unitCost: t.string().description("Standard cost per unit (decimal string), zero or greater"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await invModulesExport.commands.publishStandardCost( trx, { itemId: context.input.itemId, unitCost: context.input.unitCost, }, ctx, ); if (!result.ok) { switch (result.error.code) { case "INVENTORY_INVALID_UNIT_COST": throw new DomainError(`Unit cost must be zero or greater: ${context.input.unitCost}`); case "INVENTORY_ITEM_VALUATION_NOT_FOUND": throw new DomainError( "A company holds on-hand stock of this item but the item is not cost-tracked there", ); case "INVENTORY_ACCOUNTING_PERIOD_NOT_FOUND": throw new DomainError("No open accounting period covers the posting date"); case "INVENTORY_JOURNAL_ENTRY_CREATE_FAILED": throw new DomainError("Failed to create the revaluation journal entry"); case "INVENTORY_JOURNAL_ENTRY_POST_FAILED": throw new DomainError("Failed to post the revaluation journal entry"); 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.standardCost.id }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to publish the standard cost", { cause: err }); }); }, output: t .object({ id: t.string().description("Published StandardCost generation ID"), }) .description("Published standard cost generation"), });