feat(frontend): list the company invitations in the employer cabinet

This commit is contained in:
shira committed 2026-10-10 18:05:25 +03:00
1 parent 84454116f2
commit d8abaae85b
23 files changed
+1349 -222

No files matched your search

@@ -0,0 +1,110 @@
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { ApiRequestError, invalidResponseCode } from "../../../shared/api";
import { invitationListPageSize, listInvitationsOfOrganization } from "./organization-invitations";
const organizationId = "0199b4f2-7c1e-7d3a-9f00-aabbccddee01";
const base = `http://localhost/api/v1/organizations/${organizationId}/invitations`;
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 summary = {
id: "0199b4f2-7c1e-7d3a-9f00-aabbccdd3301",
candidate_handle: `cv1_${"A".repeat(43)}`,
need: null,
state: "delivered",
expiry_reason: null,
version: 2,
terms_version: 1,
role_title: "Middle Go-разработчик",
salary: {
salary_from: 100000,
salary_to: 140000,
currency: "RUB",
period: "month",
tax_basis: "net",
},
response_deadline_at: "2099-10-16T10:00:00Z",
contact_access: { status: "not_granted", fields: [] },
created_at: "2026-10-09T10:00:00Z",
updated_at: "2026-10-09T10:05:00Z",
};
describe("organization invitation list transport", () => {
it("reads a page with the page size and the cursor and parses it with the contract", async () => {
const seen: { query: URLSearchParams | null } = { query: null };
server.use(
http.get(base, ({ request }) => {
seen.query = new URL(request.url).searchParams;
return HttpResponse.json({
items: [summary],
page: { has_more: true, next_cursor: "next-1" },
});
}),
);
const list = await listInvitationsOfOrganization(organizationId, { cursor: "first-1" });
expect(seen.query?.get("limit")).toBe(String(invitationListPageSize));
expect(seen.query?.get("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("sends no cursor for the first page", async () => {
const seen: { query: URLSearchParams | null } = { query: null };
server.use(
http.get(base, ({ request }) => {
seen.query = new URL(request.url).searchParams;
return HttpResponse.json({ items: [], page: { has_more: false, next_cursor: null } });
}),
);
await listInvitationsOfOrganization(organizationId);
expect(seen.query?.has("cursor")).toBe(false);
});
it("refuses a body that does not match the contract instead of listing it", async () => {
server.use(http.get(base, () => HttpResponse.json({ items: [{ id: "not-an-invitation" }] })));
const failure = await listInvitationsOfOrganization(organizationId).catch(
(error: unknown) => error,
);
expect(failure).toBeInstanceOf(ApiRequestError);
expect((failure as ApiRequestError).code).toBe(invalidResponseCode);
});
it("surfaces an access refusal with its status", async () => {
server.use(
http.get(base, () =>
HttpResponse.json(
{ type: "about:blank", title: "Forbidden", status: 403, code: "forbidden" },
{ status: 403, headers: { "Content-Type": "application/problem+json" } },
),
),
);
const failure = await listInvitationsOfOrganization(organizationId).catch(
(error: unknown) => error,
);
expect(failure).toBeInstanceOf(ApiRequestError);
expect((failure as ApiRequestError).status).toBe(403);
});
});
@@ -4,15 +4,19 @@ import {
getGetOrganizationInvitationContactsQueryKey,
getGetOrganizationInvitationQueryKey,
getOrganizationInvitation,
getListOrganizationInvitationsQueryKey,
getOrganizationInvitationContacts,
listOrganizationInvitations,
sendOrganizationInvitation,
updateOrganizationInvitation,
} from "../../../shared/api/endpoints/invitation/invitation";
import {
ContactDisclosure as contactDisclosureSchema,
OrganizationInvitation as organizationInvitationSchema,
OrganizationInvitationList as organizationInvitationListSchema,
type ContactDisclosure,
type OrganizationInvitation,
type OrganizationInvitationList,
type OrganizationInvitationCreate,
type OrganizationInvitationUpdate,
} from "../../../shared/api/schemas";
@@ -36,6 +40,12 @@ function parseInvitation(value: unknown): OrganizationInvitation {
return parsed.data;
}
function parseInvitationList(value: unknown): OrganizationInvitationList {
const parsed = organizationInvitationListSchema.safeParse(value);
if (!parsed.success) throw invalidResponse();
return parsed.data;
}
type CommandOptions = {
organizationId: string;
csrfToken: string;
@@ -93,6 +103,31 @@ export async function sendInvitationDraft(
);
}
/** Page size of the organization's invitation list. */
export const invitationListPageSize = 20;
/**
* Reads one page of the organization's invitations, drafts included, newest
* first. The cursor comes only from the previous page. The summaries name a
* candidate by the opaque handle alone and hold no contact value.
*/
export async function listInvitationsOfOrganization(
organizationId: string,
options: { cursor?: string | undefined; signal?: AbortSignal | undefined } = {},
): Promise<OrganizationInvitationList> {
const params = {
limit: invitationListPageSize,
...(options.cursor === undefined ? {} : { cursor: options.cursor }),
};
return parseInvitationList(
await listOrganizationInvitations(
organizationId,
params,
options.signal ? { signal: options.signal } : {},
),
);
}
/** Reads one invitation of the organization and checks it against the contract. */
export async function readOrganizationInvitation(
organizationId: string,
@@ -123,6 +158,12 @@ export async function readInvitationContacts(
/** Invitation reads stay separate for each signed-in account. */
export const invitationQueryKeys = {
/** Every page of the organization's invitation list for one account. */
list: (accountId: string, organizationId: string) =>
[
...getListOrganizationInvitationsQueryKey(organizationId, { limit: invitationListPageSize }),
accountId,
] as const,
detail: (accountId: string, organizationId: string, invitationId: string) =>
[...getGetOrganizationInvitationQueryKey(organizationId, invitationId), accountId] as const,
contacts: (accountId: string, organizationId: string, invitationId: string) =>
+10
View File
@@ -6,7 +6,9 @@
export {
createInvitationDraft,
invitationListPageSize,
invitationQueryKeys,
listInvitationsOfOrganization,
readInvitationContacts,
readOrganizationInvitation,
sendInvitationDraft,
@@ -21,6 +23,14 @@ export {
readCandidateInvitation,
viewCandidateInvitation,
} from "./api/candidate-invitations";
export {
filterInvitations,
invitationFilterOptions,
invitationGroup,
invitationStateTone,
type InvitationFilter,
type InvitationGroup,
} from "./model/list-groups";
export { formatRubles, formatSalaryRange, formatServerMoment } from "./model/format";
export {
candidateInvitationStateLabels,
@@ -0,0 +1,80 @@
import { describe, expect, it } from "vitest";
import { InvitationState } from "../../../shared/api/schemas";
import {
filterInvitations,
invitationFilterOptions,
invitationGroup,
invitationStateTone,
} from "./list-groups";
const allStates = InvitationState.options;
describe("invitation list groups", () => {
it("puts every state of the contract into exactly one group", () => {
for (const state of allStates) {
expect(["draft", "pending", "accepted", "closed"]).toContain(invitationGroup(state));
}
});
it("separates waiting, accepted, closed, and draft invitations", () => {
expect(invitationGroup("draft")).toBe("draft");
for (const state of ["delivered", "viewed", "counter"] as const) {
expect(invitationGroup(state)).toBe("pending");
}
expect(invitationGroup("accepted")).toBe("accepted");
expect(invitationGroup("terms_agreed")).toBe("accepted");
for (const state of [
"declined",
"expired",
"withdrawn",
"cancelled_by_candidate",
"no_deal",
] as const) {
expect(invitationGroup(state)).toBe("closed");
}
});
it("offers the four groups of the screen and the drafts", () => {
expect(invitationFilterOptions.map((option) => option.label)).toEqual([
"Все",
"Ожидают ответа",
"Приняты",
"Закрыты",
"Черновики",
]);
});
it("filters by group and keeps the server order", () => {
const items = [
{ id: "1", state: "declined" },
{ id: "2", state: "draft" },
{ id: "3", state: "viewed" },
{ id: "4", state: "accepted" },
{ id: "5", state: "delivered" },
{ id: "6", state: "expired" },
] as const;
expect(filterInvitations(items, "all").map((item) => item.id)).toEqual([
"1",
"2",
"3",
"4",
"5",
"6",
]);
expect(filterInvitations(items, "pending").map((item) => item.id)).toEqual(["3", "5"]);
expect(filterInvitations(items, "accepted").map((item) => item.id)).toEqual(["4"]);
expect(filterInvitations(items, "closed").map((item) => item.id)).toEqual(["1", "6"]);
expect(filterInvitations(items, "draft").map((item) => item.id)).toEqual(["2"]);
});
it("repeats the meaning of the state in the tone of its tag", () => {
expect(invitationStateTone("draft")).toBe("neutral");
expect(invitationStateTone("delivered")).toBe("accent");
expect(invitationStateTone("viewed")).toBe("accent");
expect(invitationStateTone("accepted")).toBe("success");
expect(invitationStateTone("declined")).toBe("danger");
expect(invitationStateTone("expired")).toBe("danger");
expect(invitationStateTone("withdrawn")).toBe("danger");
});
});
@@ -0,0 +1,79 @@
import type { InvitationState } from "../../../shared/api/schemas";
import type { TagTone } from "../../../shared/ui";
/**
* Groups of the organization's invitation list. The groups cover every state
* of the contract once, so a state that a later release adds fails the
* exhaustive map below at compile time instead of vanishing from the list.
*/
export type InvitationGroup = "draft" | "pending" | "accepted" | "closed";
export type InvitationFilter = "all" | InvitationGroup;
const groupOfState: Readonly<Record<InvitationState, InvitationGroup>> = {
draft: "draft",
delivered: "pending",
viewed: "pending",
counter: "pending",
accepted: "accepted",
interview_scheduled: "accepted",
interviewed: "accepted",
negotiation: "accepted",
terms_agreed: "accepted",
no_deal: "closed",
declined: "closed",
expired: "closed",
withdrawn: "closed",
cancelled_by_candidate: "closed",
};
export function invitationGroup(state: InvitationState): InvitationGroup {
return groupOfState[state];
}
/** Filters in the order the list offers them; drafts have their own group. */
export const invitationFilterOptions: readonly { value: InvitationFilter; label: string }[] = [
{ value: "all", label: "Все" },
{ value: "pending", label: "Ожидают ответа" },
{ value: "accepted", label: "Приняты" },
{ value: "closed", label: "Закрыты" },
{ value: "draft", label: "Черновики" },
];
/** Items of one filter, in the server order. */
export function filterInvitations<T extends { state: InvitationState }>(
items: readonly T[],
filter: InvitationFilter,
): T[] {
return filter === "all"
? [...items]
: items.filter((item) => groupOfState[item.state] === filter);
}
/**
* Tone of the state tag. The tag text always names the state; the tone only
* repeats it: a draft is plain, an invitation that waits for the candidate is
* accented, an acceptance is a success, and every closed outcome is danger.
*/
export function invitationStateTone(state: InvitationState): TagTone {
switch (state) {
case "delivered":
case "viewed":
case "counter":
case "negotiation":
return "accent";
case "accepted":
case "interview_scheduled":
case "interviewed":
case "terms_agreed":
return "success";
case "declined":
case "expired":
case "withdrawn":
case "cancelled_by_candidate":
case "no_deal":
return "danger";
case "draft":
return "neutral";
}
}