import { createContext, DomainError } from "@tailor-platform/erp-kit/app"; import { createResolver, t } from "@tailor-platform/sdk"; import { getDB } from "@/generated/kysely-tailordb"; import { orgModulesExport } from "@/modules"; export default createResolver({ name: "updateCompany", operation: "mutation", input: { companyId: t.string().description("Company ID"), legalName: t.string({ optional: true }).description("Legal name of the company"), taxId: t.string({ optional: true }).description("Tax ID"), registrationNumber: t.string({ optional: true }).description("Registration number"), baseCurrencyId: t.string({ optional: true }).description("Base currency ID"), street: t.string({ optional: true }).description("Street address"), city: t.string({ optional: true }).description("City"), state: t.string({ optional: true }).description("State or province"), postalCode: t.string({ optional: true }).description("Postal code"), country: t.string({ optional: true }).description("Country"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await orgModulesExport.commands.updateCompany( trx, { companyId: context.input.companyId, legalName: context.input.legalName ?? undefined, taxId: context.input.taxId ?? undefined, registrationNumber: context.input.registrationNumber ?? undefined, baseCurrencyId: context.input.baseCurrencyId ?? undefined, street: context.input.street ?? undefined, city: context.input.city ?? undefined, state: context.input.state ?? undefined, postalCode: context.input.postalCode ?? undefined, country: context.input.country ?? undefined, }, ctx, ); if (!result.ok) { switch (result.error.code) { case "ORGANIZATION_COMPANY_NOT_FOUND": throw new DomainError(`Company not found: ${context.input.companyId}`); case "ORGANIZATION_INVALID_LEGAL_NAME": throw new DomainError(`Invalid legal name: ${context.input.legalName}`); case "ORGANIZATION_CURRENCY_IMMUTABLE": throw new DomainError("Base currency cannot be changed once set"); case "ORGANIZATION_CURRENCY_NOT_FOUND": throw new DomainError(`Currency not found: ${context.input.baseCurrencyId}`); 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.company.id }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to update the company", { cause: err }); }); }, output: t .object({ id: t.string().description("Updated company ID"), }) .description("Updated company"), });