feat(invitations): add the candidate invitation inbox with C4 acceptance and decline
This commit is contained in:
1 parent
12a7213d4a
commit
a856066316
54 files changed
+3897
-136
No files matched your search
@@ -0,0 +1,131 @@
|
||||
import { ApiRequestError, invalidResponseCode } from "../../../shared/api";
|
||||
import {
|
||||
acceptMyInvitation,
|
||||
declineMyInvitation,
|
||||
getGetMyInvitationQueryKey,
|
||||
getListMyInvitationsQueryKey,
|
||||
getMyInvitation,
|
||||
listMyInvitations,
|
||||
viewMyInvitation,
|
||||
} from "../../../shared/api/endpoints/invitation/invitation";
|
||||
import {
|
||||
CandidateInvitation as candidateInvitationSchema,
|
||||
CandidateInvitationList as candidateInvitationListSchema,
|
||||
type CandidateInvitation,
|
||||
type CandidateInvitationList,
|
||||
type InvitationAcceptance,
|
||||
type InvitationDeclineRequest,
|
||||
} from "../../../shared/api/schemas";
|
||||
|
||||
// Transport adapters of the candidate 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 shown invitation.
|
||||
|
||||
function invalidResponse(): ApiRequestError {
|
||||
return new ApiRequestError({
|
||||
status: 200,
|
||||
code: invalidResponseCode,
|
||||
title: "The response does not match the API contract.",
|
||||
});
|
||||
}
|
||||
|
||||
function parseInvitation(value: unknown): CandidateInvitation {
|
||||
const parsed = candidateInvitationSchema.safeParse(value);
|
||||
if (!parsed.success) throw invalidResponse();
|
||||
return parsed.data;
|
||||
}
|
||||
|
||||
function parseInvitationList(value: unknown): CandidateInvitationList {
|
||||
const parsed = candidateInvitationListSchema.safeParse(value);
|
||||
if (!parsed.success) throw invalidResponse();
|
||||
return parsed.data;
|
||||
}
|
||||
|
||||
type CommandOptions = {
|
||||
csrfToken: string;
|
||||
signal?: AbortSignal | undefined;
|
||||
};
|
||||
|
||||
function commandRequest(csrfToken: string, signal: AbortSignal | undefined): RequestInit {
|
||||
return { headers: { "X-CSRF-Token": csrfToken }, ...(signal ? { signal } : {}) };
|
||||
}
|
||||
|
||||
/** One page of the candidate's inbox, newest first; the cursor comes only from the previous page. */
|
||||
export async function listCandidateInvitations(
|
||||
options: { cursor?: string | undefined; signal?: AbortSignal | undefined } = {},
|
||||
): Promise<CandidateInvitationList> {
|
||||
const params = options.cursor === undefined ? {} : { cursor: options.cursor };
|
||||
return parseInvitationList(
|
||||
await listMyInvitations(params, options.signal ? { signal: options.signal } : {}),
|
||||
);
|
||||
}
|
||||
|
||||
/** Reads the terms of one invitation. A read never records a view and never answers it. */
|
||||
export async function readCandidateInvitation(
|
||||
invitationId: string,
|
||||
signal: AbortSignal | undefined = undefined,
|
||||
): Promise<CandidateInvitation> {
|
||||
return parseInvitation(await getMyInvitation(invitationId, signal ? { signal } : {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Records that the candidate opened the terms. The server keeps the first
|
||||
* view and answers with the current invitation on every repeat, so the call
|
||||
* needs no Idempotency-Key.
|
||||
*/
|
||||
export async function viewCandidateInvitation(
|
||||
invitationId: string,
|
||||
options: CommandOptions,
|
||||
): Promise<CandidateInvitation> {
|
||||
return parseInvitation(
|
||||
await viewMyInvitation(invitationId, commandRequest(options.csrfToken, options.signal)),
|
||||
);
|
||||
}
|
||||
|
||||
/** Accepts the terms version the candidate read and discloses the chosen contact kinds. */
|
||||
export async function acceptCandidateInvitation(
|
||||
options: CommandOptions & {
|
||||
invitationId: string;
|
||||
body: InvitationAcceptance;
|
||||
idempotencyKey: string;
|
||||
},
|
||||
): Promise<CandidateInvitation> {
|
||||
return parseInvitation(
|
||||
await acceptMyInvitation(
|
||||
options.invitationId,
|
||||
options.body,
|
||||
{ "Idempotency-Key": options.idempotencyKey },
|
||||
commandRequest(options.csrfToken, options.signal),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/** Declines the invitation with a structured reason and an optional comment. */
|
||||
export async function declineCandidateInvitation(
|
||||
options: CommandOptions & {
|
||||
invitationId: string;
|
||||
body: InvitationDeclineRequest;
|
||||
idempotencyKey: string;
|
||||
},
|
||||
): Promise<CandidateInvitation> {
|
||||
return parseInvitation(
|
||||
await declineMyInvitation(
|
||||
options.invitationId,
|
||||
options.body,
|
||||
{ "Idempotency-Key": options.idempotencyKey },
|
||||
commandRequest(options.csrfToken, options.signal),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Candidate reads are separate for each signed-in account, so a response for
|
||||
* one account never fills another account's cache.
|
||||
*/
|
||||
export const candidateInvitationQueryKeys = {
|
||||
/** Every inbox query of the account; invalidating it refreshes all pages. */
|
||||
inbox: (accountId: string) => [...getListMyInvitationsQueryKey(), accountId] as const,
|
||||
detail: (accountId: string, invitationId: string) =>
|
||||
[...getGetMyInvitationQueryKey(invitationId), accountId] as const,
|
||||
};
|
||||
Reference in new issue
Block a user