import { pathToFileURL } from "node:url"; import { runWithRlsContext } from "@hivegrid/db/rls-context.server"; import { CustomerPortalUatError, parseCustomerPortalUatCommand, safeCustomerPortalUatError, type CustomerPortalFixtureCommand, } from "./contracts"; import { withCustomerPortalOperatorDatabase } from "./database"; import { applyProjectInvitationContact, assessProjectInvitationContact, parseProjectInvitationContactInput, readProjectInvitationContact, type ProjectInvitationContactCommand, type ProjectInvitationContactHandoff, } from "./project-invitation-contact"; export type ProjectInvitationContactExecutionResult = { readonly mode: CustomerPortalFixtureCommand["mode"]; readonly complete: boolean; readonly handoff: ProjectInvitationContactHandoff | null; }; function writeResult(result: ProjectInvitationContactExecutionResult): void { process.stdout.write(`${JSON.stringify(result, null, 2)}\n`); } async function executeProjectInvitationContactCommand( command: ProjectInvitationContactCommand ): Promise { return withCustomerPortalOperatorDatabase(command, (db) => runWithRlsContext({ tenantId: command.expectedTenantId }, async () => { const inspect = async () => { const snapshot = await readProjectInvitationContact(db, command); return { snapshot, assessment: assessProjectInvitationContact(snapshot, command) }; }; const initial = await inspect(); if (command.mode === "plan") { return { mode: command.mode, complete: initial.assessment.action === "noop", handoff: initial.assessment.handoff }; } if (command.mode === "verify") { if (initial.assessment.action !== "noop" || !initial.assessment.handoff) { throw new CustomerPortalUatError("customer-portal UAT: project invitation contact verification found missing, drifted, or unsafe state"); } return { mode: command.mode, complete: true, handoff: initial.assessment.handoff }; } if (initial.assessment.action === "blocked") { throw new CustomerPortalUatError("customer-portal UAT: project invitation contact apply is blocked by unsafe or non-UAT state"); } await applyProjectInvitationContact(db, command, initial.snapshot); const final = await inspect(); if (final.assessment.action !== "noop" || !final.assessment.handoff) { throw new CustomerPortalUatError("customer-portal UAT: project invitation contact did not converge to the canonical contract"); } return { mode: command.mode, complete: true, handoff: final.assessment.handoff }; }) ); } export async function main(argv: readonly string[] = process.argv.slice(2), env: NodeJS.ProcessEnv = process.env): Promise { const baseCommand = parseCustomerPortalUatCommand("fixture", argv, env); if (baseCommand.target !== "fixture") throw new CustomerPortalUatError("customer-portal UAT: invalid project invitation fixture command"); const command: ProjectInvitationContactCommand = { ...baseCommand, ...parseProjectInvitationContactInput(env) }; writeResult(await executeProjectInvitationContactCommand(command)); } const directEntry = process.argv[1] ? pathToFileURL(process.argv[1]).href === import.meta.url : false; if (directEntry) { void main().catch((error) => { process.stderr.write(`${safeCustomerPortalUatError(error)}\n`); process.exitCode = 1; }); }