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: "updateSite", operation: "mutation", input: { siteId: t.string().description("Site ID"), name: t.string({ optional: true }).description("Site name"), type: t.string({ optional: true }).description("Site type"), 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"), timezone: t.string({ optional: true }).description("Timezone"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await orgModulesExport.commands.updateSite( trx, { siteId: context.input.siteId, name: context.input.name ?? undefined, type: context.input.type ?? 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, timezone: context.input.timezone ?? undefined, }, ctx, ); if (!result.ok) { switch (result.error.code) { case "ORGANIZATION_SITE_NOT_FOUND": throw new DomainError(`Site not found: ${context.input.siteId}`); case "ORGANIZATION_INVALID_NAME": throw new DomainError(`Invalid name: ${context.input.name}`); case "ORGANIZATION_DUPLICATE_NAME": throw new DomainError(`Site name already exists: ${context.input.name}`); case "ORGANIZATION_INVALID_TYPE": throw new DomainError(`Invalid site type: ${context.input.type}`); case "ORGANIZATION_MISSING_REQUIRED_FIELDS": throw new DomainError("Missing required fields"); case "ORGANIZATION_INVALID_COUNTRY": throw new DomainError(`Invalid country: ${context.input.country}`); case "ORGANIZATION_INVALID_TIMEZONE": throw new DomainError(`Invalid timezone: ${context.input.timezone}`); 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.site.id }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to update the site", { cause: err }); }); }, output: t .object({ id: t.string().description("Updated site ID"), }) .description("Updated site"), });