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: "activateCompany", operation: "mutation", input: { id: t.string().description("Company ID to activate"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await orgModulesExport.commands.activateCompany( trx, { companyId: context.input.id }, ctx, ); if (!result.ok) { switch (result.error.code) { case "ORGANIZATION_COMPANY_NOT_FOUND": throw new DomainError(`Company not found: ${context.input.id}`); case "ORGANIZATION_INVALID_STATE": throw new DomainError(`Invalid state transition for company: ${context.input.id}`); case "ORGANIZATION_MISSING_REQUIRED_FIELDS": throw new DomainError( "Missing required fields for activation: baseCurrencyId, street, city, postalCode, and country are required", ); case "ORGANIZATION_INVALID_BASE_CURRENCY": throw new DomainError(`Base currency is not active for company: ${context.input.id}`); 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: context.input.id }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to activate the company", { cause: err }); }); }, output: t .object({ id: t.string().description("Activated company ID"), }) .description("Activated company"), });