import { ApiRequestError, invalidResponseCode } from "../../../shared/api"; import { createOrganizationInvitation, getGetOrganizationInvitationContactsQueryKey, getGetOrganizationInvitationQueryKey, getOrganizationInvitation, getOrganizationInvitationContacts, sendOrganizationInvitation, updateOrganizationInvitation, } from "../../../shared/api/endpoints/invitation/invitation"; import { ContactDisclosure as contactDisclosureSchema, OrganizationInvitation as organizationInvitationSchema, type ContactDisclosure, type OrganizationInvitation, type OrganizationInvitationCreate, type OrganizationInvitationUpdate, } from "../../../shared/api/schemas"; // Transport adapters of the organization side of an invitation. The // generated client does not check response bodies, so every adapter parses // the response with the generated schema: a body that does not match the // contract is an invalid response, never a delivered invitation. function invalidResponse(): ApiRequestError { return new ApiRequestError({ status: 200, code: invalidResponseCode, title: "The response does not match the API contract.", }); } function parseInvitation(value: unknown): OrganizationInvitation { const parsed = organizationInvitationSchema.safeParse(value); if (!parsed.success) throw invalidResponse(); return parsed.data; } type CommandOptions = { organizationId: string; csrfToken: string; signal?: AbortSignal | undefined; }; function requestOptions(csrfToken: string, signal: AbortSignal | undefined): RequestInit { return { headers: { "X-CSRF-Token": csrfToken }, ...(signal ? { signal } : {}) }; } /** Creates a draft under an Idempotency-Key; a retry with the same key and body replays it. */ export async function createInvitationDraft( options: CommandOptions & { body: OrganizationInvitationCreate; idempotencyKey: string }, ): Promise { return parseInvitation( await createOrganizationInvitation( options.organizationId, options.body, { "Idempotency-Key": options.idempotencyKey }, requestOptions(options.csrfToken, options.signal), ), ); } /** Replaces the terms of a draft over the version the client read. */ export async function updateInvitationDraft( options: CommandOptions & { invitationId: string; body: OrganizationInvitationUpdate }, ): Promise { return parseInvitation( await updateOrganizationInvitation( options.organizationId, options.invitationId, options.body, requestOptions(options.csrfToken, options.signal), ), ); } /** Sends the current terms version of a draft under an Idempotency-Key. */ export async function sendInvitationDraft( options: CommandOptions & { invitationId: string; expectedVersion: number; idempotencyKey: string; }, ): Promise { return parseInvitation( await sendOrganizationInvitation( options.organizationId, options.invitationId, { expected_version: options.expectedVersion }, { "Idempotency-Key": options.idempotencyKey }, requestOptions(options.csrfToken, options.signal), ), ); } /** Reads one invitation of the organization and checks it against the contract. */ export async function readOrganizationInvitation( organizationId: string, invitationId: string, signal: AbortSignal | undefined = undefined, ): Promise { return parseInvitation( await getOrganizationInvitation(organizationId, invitationId, signal ? { signal } : {}), ); } /** * Reads the contacts that an active grant of an accepted invitation covers. * The server answers 404 when no grant is readable, which the caller shows as * "not available" without asking for a reason. */ export async function readInvitationContacts( organizationId: string, invitationId: string, signal: AbortSignal | undefined = undefined, ): Promise { const parsed = contactDisclosureSchema.safeParse( await getOrganizationInvitationContacts(organizationId, invitationId, signal ? { signal } : {}), ); if (!parsed.success) throw invalidResponse(); return parsed.data; } /** Invitation reads stay separate for each signed-in account. */ export const invitationQueryKeys = { detail: (accountId: string, organizationId: string, invitationId: string) => [...getGetOrganizationInvitationQueryKey(organizationId, invitationId), accountId] as const, contacts: (accountId: string, organizationId: string, invitationId: string) => [ ...getGetOrganizationInvitationContactsQueryKey(organizationId, invitationId), accountId, ] as const, };