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: "createStorageLocation", operation: "mutation", input: { name: t.string().description("Storage location name"), code: t.string({ optional: true }).description("Storage location code (unique within site)"), siteId: t.string().description("Site ID"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await invModulesExport.commands.createStorageLocation( trx, { name: context.input.name, code: context.input.code ?? null, siteId: context.input.siteId, }, ctx, ); if (!result.ok) { switch (result.error.code) { case "INVENTORY_SITE_NOT_FOUND": throw new DomainError(`Site not found: ${context.input.siteId}`); case "INVENTORY_SITE_INACTIVE": throw new DomainError(`Site is inactive: ${context.input.siteId}`); case "INVENTORY_DUPLICATE_LOCATION_NAME": throw new DomainError(`Storage location name already exists: ${context.input.name}`); 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.storageLocation.id }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to create the storage location", { cause: err }); }); }, output: t .object({ id: t.string().description("Created storage location ID"), }) .description("Created storage location"), });