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,217 @@
|
||||
import { http, HttpResponse } from "msw";
|
||||
import { setupServer } from "msw/node";
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { ApiRequestError } from "../../../shared/api";
|
||||
import { candidateInvitationFixture, candidateInvitationId } from "../testing";
|
||||
import {
|
||||
acceptCandidateInvitation,
|
||||
declineCandidateInvitation,
|
||||
listCandidateInvitations,
|
||||
readCandidateInvitation,
|
||||
viewCandidateInvitation,
|
||||
} from "./candidate-invitations";
|
||||
|
||||
const base = "http://localhost/api/v1/me/invitations";
|
||||
const csrfToken = "A".repeat(43);
|
||||
const idempotencyKey = "11111111-2222-4333-8444-555555555555";
|
||||
|
||||
const server = setupServer();
|
||||
beforeAll(() => server.listen({ onUnhandledFrame: "error" }));
|
||||
beforeEach(() => {
|
||||
// The browser client uses same-origin paths; Node needs an origin.
|
||||
const realFetch = globalThis.fetch;
|
||||
vi.stubGlobal("fetch", (input: string, init?: RequestInit) =>
|
||||
realFetch(new URL(input, "http://localhost"), init),
|
||||
);
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
server.resetHandlers();
|
||||
});
|
||||
afterAll(() => server.close());
|
||||
|
||||
const problemHeaders = { "Content-Type": "application/problem+json" };
|
||||
|
||||
describe("candidate invitation transport", () => {
|
||||
it("reads the inbox page with its cursor and parses it with the contract", async () => {
|
||||
let cursor: string | null = null;
|
||||
server.use(
|
||||
http.get(base, ({ request }) => {
|
||||
cursor = new URL(request.url).searchParams.get("cursor");
|
||||
return HttpResponse.json({
|
||||
items: [
|
||||
{
|
||||
id: candidateInvitationId,
|
||||
organization: { id: "0199b4f2-7c1e-7d3a-9f00-aabbccdd3201", name: "Северный ветер" },
|
||||
state: "delivered",
|
||||
expiry_reason: null,
|
||||
version: 1,
|
||||
terms_version: 1,
|
||||
role_title: "Middle Go-разработчик",
|
||||
salary: {
|
||||
salary_from: 100000,
|
||||
salary_to: 140000,
|
||||
currency: "RUB",
|
||||
period: "month",
|
||||
tax_basis: "net",
|
||||
},
|
||||
work_formats: ["remote"],
|
||||
response_deadline_at: "2099-10-16T10:00:00Z",
|
||||
delivered_at: "2026-10-09T10:00:00Z",
|
||||
viewed_at: null,
|
||||
updated_at: "2026-10-09T10:00:00Z",
|
||||
},
|
||||
],
|
||||
page: { has_more: true, next_cursor: "next-1" },
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const list = await listCandidateInvitations({ cursor: "first-1" });
|
||||
|
||||
expect(cursor).toBe("first-1");
|
||||
expect(list.items[0]?.role_title).toBe("Middle Go-разработчик");
|
||||
expect(list.page).toEqual({ has_more: true, next_cursor: "next-1" });
|
||||
});
|
||||
|
||||
it("refuses a response that does not match the contract instead of showing it", async () => {
|
||||
server.use(http.get(base, () => HttpResponse.json({ items: [{ id: "not-an-invitation" }] })));
|
||||
|
||||
const failure = await listCandidateInvitations().catch((error: unknown) => error);
|
||||
|
||||
expect(failure).toBeInstanceOf(ApiRequestError);
|
||||
expect((failure as ApiRequestError).code).toBe("invalid_response");
|
||||
});
|
||||
|
||||
it("reads the terms without any side effect and parses the full invitation", async () => {
|
||||
const methods: string[] = [];
|
||||
server.use(
|
||||
http.get(`${base}/${candidateInvitationId}`, ({ request }) => {
|
||||
methods.push(request.method);
|
||||
return HttpResponse.json(candidateInvitationFixture());
|
||||
}),
|
||||
);
|
||||
|
||||
const invitation = await readCandidateInvitation(candidateInvitationId);
|
||||
|
||||
expect(methods).toEqual(["GET"]);
|
||||
expect(invitation.contact_consent?.available_fields).toEqual(["full_name", "email", "phone"]);
|
||||
});
|
||||
|
||||
it("records a view with the CSRF token and no Idempotency-Key", async () => {
|
||||
const seen: { csrf: string | null; key: string | null; method: string }[] = [];
|
||||
server.use(
|
||||
http.post(`${base}/${candidateInvitationId}/view`, ({ request }) => {
|
||||
seen.push({
|
||||
csrf: request.headers.get("X-CSRF-Token"),
|
||||
key: request.headers.get("Idempotency-Key"),
|
||||
method: request.method,
|
||||
});
|
||||
return HttpResponse.json(candidateInvitationFixture({ state: "viewed", version: 2 }));
|
||||
}),
|
||||
);
|
||||
|
||||
const viewed = await viewCandidateInvitation(candidateInvitationId, { csrfToken });
|
||||
|
||||
expect(viewed.state).toBe("viewed");
|
||||
expect(seen).toEqual([{ csrf: csrfToken, key: null, method: "POST" }]);
|
||||
});
|
||||
|
||||
it("sends the accepted fields, the versions, and the key, and returns the grant", async () => {
|
||||
let body: unknown = null;
|
||||
let key: string | null = null;
|
||||
server.use(
|
||||
http.post(`${base}/${candidateInvitationId}/accept`, async ({ request }) => {
|
||||
key = request.headers.get("Idempotency-Key");
|
||||
body = await request.json();
|
||||
return HttpResponse.json(
|
||||
candidateInvitationFixture({
|
||||
state: "accepted",
|
||||
version: 2,
|
||||
contact_grant: {
|
||||
id: "0199b4f2-7c1e-7d3a-9f00-aabbccdd3301",
|
||||
fields: ["email"],
|
||||
status: "active",
|
||||
readable: true,
|
||||
blocked_by: null,
|
||||
terms_version: 1,
|
||||
consent_text_version: "c4-2026-10",
|
||||
granted_at: "2026-10-10T09:00:00Z",
|
||||
revoked_at: null,
|
||||
revocation_reason: null,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const accepted = await acceptCandidateInvitation({
|
||||
invitationId: candidateInvitationId,
|
||||
csrfToken,
|
||||
idempotencyKey,
|
||||
body: {
|
||||
expected_version: 1,
|
||||
terms_version: 1,
|
||||
consent_c4: { text_version: "c4-2026-10", accepted: true, fields: ["email"] },
|
||||
},
|
||||
});
|
||||
|
||||
expect(key).toBe(idempotencyKey);
|
||||
expect(body).toEqual({
|
||||
expected_version: 1,
|
||||
terms_version: 1,
|
||||
consent_c4: { text_version: "c4-2026-10", accepted: true, fields: ["email"] },
|
||||
});
|
||||
expect(accepted.contact_grant?.fields).toEqual(["email"]);
|
||||
});
|
||||
|
||||
it("surfaces a stale terms version as the problem code the contract names", async () => {
|
||||
server.use(
|
||||
http.post(`${base}/${candidateInvitationId}/accept`, () =>
|
||||
HttpResponse.json(
|
||||
{
|
||||
type: "urn:lincodex:problem:version_conflict",
|
||||
title: "Version conflict",
|
||||
status: 412,
|
||||
code: "version_conflict",
|
||||
},
|
||||
{ status: 412, headers: problemHeaders },
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const failure = await acceptCandidateInvitation({
|
||||
invitationId: candidateInvitationId,
|
||||
csrfToken,
|
||||
idempotencyKey,
|
||||
body: {
|
||||
expected_version: 1,
|
||||
terms_version: 1,
|
||||
consent_c4: { text_version: "c4-2026-10", accepted: true, fields: ["phone"] },
|
||||
},
|
||||
}).catch((error: unknown) => error);
|
||||
|
||||
expect(failure).toBeInstanceOf(ApiRequestError);
|
||||
expect(failure).toMatchObject({ status: 412, code: "version_conflict" });
|
||||
});
|
||||
|
||||
it("sends a decline with its reason and without a comment when none was written", async () => {
|
||||
let body: unknown = null;
|
||||
server.use(
|
||||
http.post(`${base}/${candidateInvitationId}/decline`, async ({ request }) => {
|
||||
body = await request.json();
|
||||
return HttpResponse.json(candidateInvitationFixture({ state: "declined", version: 2 }));
|
||||
}),
|
||||
);
|
||||
|
||||
const declined = await declineCandidateInvitation({
|
||||
invitationId: candidateInvitationId,
|
||||
csrfToken,
|
||||
idempotencyKey,
|
||||
body: { expected_version: 1, reason: "other" },
|
||||
});
|
||||
|
||||
expect(body).toEqual({ expected_version: 1, reason: "other" });
|
||||
expect(declined.state).toBe("declined");
|
||||
});
|
||||
});
|
||||
@@ -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