feat(invitations): add the invitation compose screen with a candidate-side preview
This commit is contained in:
1 parent
1a067221f7
commit
cb08f212cc
35 files changed
+4170
-67
No files matched your search
@@ -0,0 +1,107 @@
|
||||
import { ApiRequestError, invalidResponseCode } from "../../../shared/api";
|
||||
import {
|
||||
createOrganizationInvitation,
|
||||
getGetOrganizationInvitationQueryKey,
|
||||
getOrganizationInvitation,
|
||||
sendOrganizationInvitation,
|
||||
updateOrganizationInvitation,
|
||||
} from "../../../shared/api/endpoints/invitation/invitation";
|
||||
import {
|
||||
OrganizationInvitation as organizationInvitationSchema,
|
||||
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<OrganizationInvitation> {
|
||||
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<OrganizationInvitation> {
|
||||
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<OrganizationInvitation> {
|
||||
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<OrganizationInvitation> {
|
||||
return parseInvitation(
|
||||
await getOrganizationInvitation(organizationId, invitationId, signal ? { signal } : {}),
|
||||
);
|
||||
}
|
||||
|
||||
/** 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,
|
||||
};
|
||||
Reference in new issue
Block a user