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"; /** * confirmStockAdjustment — approve a SUBMITTED adjustment (SUBMITTED → * CONFIRMED). This is where the adjustment is actually applied: the native * command posts inventory ledger entries and updates StockLevel rows for every * line. Confirmation can fail if there is not enough stock to decrease / scrap * / block. */ export default createResolver({ name: "confirmStockAdjustment", operation: "mutation", input: { id: t.string().description("Stock adjustment ID"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await invModulesExport.commands.confirmStockAdjustment( trx, { id: context.input.id }, ctx, ); if (!result.ok) { switch (result.error.code) { case "INVENTORY_STOCK_ADJUSTMENT_NOT_FOUND": throw new DomainError(`Stock adjustment not found: ${context.input.id}`); case "INVENTORY_INVALID_STATUS": throw new DomainError( `Stock adjustment cannot be confirmed in its current status: ${context.input.id}`, ); case "INVENTORY_INSUFFICIENT_STOCK": throw new DomainError( `Insufficient stock to apply the adjustment: ${result.error.message}`, ); case "INVENTORY_INVENTORY_SUPPLY_PLAN_NOT_FOUND": throw new DomainError("A related supply plan was not found."); case "INVENTORY_SUPPLY_PLAN_NOT_OPEN": throw new DomainError("A related supply plan is not open."); case "INVENTORY_STOCK_RESERVATION_NOT_FOUND": throw new DomainError("A related stock reservation was not found."); case "INVENTORY_STOCK_RESERVATION_NOT_OPEN": throw new DomainError("A related stock reservation is not open."); case "INVENTORY_ACCOUNTING_PERIOD_NOT_FOUND": throw new DomainError("No accounting period covers the posting date."); case "INVENTORY_JOURNAL_ENTRY_CREATE_FAILED": throw new DomainError("Failed to create the inventory journal entry."); case "INVENTORY_JOURNAL_ENTRY_POST_FAILED": throw new DomainError("Failed to post the inventory journal entry."); case "INVENTORY_CONSUMPTION_TREATMENT_NOT_SUPPORTED": throw new DomainError("An item's consumption treatment is not supported."); case "INVENTORY_UNIT_COST_REQUIRED": throw new DomainError("A unit cost is required to apply the adjustment."); case "INVENTORY_AVERAGE_COST_NOT_FOUND": throw new DomainError("No average cost is available for an item."); case "INVENTORY_FIFO_COST_NOT_FOUND": throw new DomainError("No FIFO cost layer is available for an item."); case "INVENTORY_STANDARD_COST_NOT_FOUND": throw new DomainError("No standard cost is defined for an item."); case "INVENTORY_COSTING_EVENT_NOT_SUPPORTED": throw new DomainError( "The costing event is not supported by an item's valuation policy.", ); case "INVENTORY_SITE_NOT_FOUND": throw new DomainError("A referenced site was not found."); case "INVENTORY_DEFAULT_VALUATION_POLICY_NOT_FOUND": throw new DomainError("No default valuation policy is defined for the company."); case "INVENTORY_INVALID_SUPPLY_PLAN_QUANTITIES": throw new DomainError("Supply plan quantities are invalid."); case "INVENTORY_INVALID_RESERVATION_QUANTITIES": throw new DomainError("Reservation quantities are invalid."); case "INVENTORY_EMPTY_MOVEMENT_LINES": throw new DomainError("The adjustment has no movement lines."); case "INVENTORY_INVALID_QUANTITY": throw new DomainError("A movement line quantity is invalid."); case "INVENTORY_INVALID_UNIT_COST": throw new DomainError("A movement line unit cost is invalid."); case "INVENTORY_ITEM_NOT_FOUND": throw new DomainError("A referenced item was not found."); case "INVENTORY_STORAGE_LOCATION_NOT_FOUND": throw new DomainError("A referenced storage location was not found."); 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.stockAdjustment.id, status: result.value.stockAdjustment.status, }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to confirm the stock adjustment", { cause: err }); }); }, output: t .object({ id: t.string().description("Stock adjustment ID"), status: t.string().description("New document status (CONFIRMED)"), }) .description("ConfirmStockAdjustment response"), });