feat: add safe match response adapter
This commit is contained in:
1 parent
6ad4402ff4
commit
e6bd66c560
4 files changed
+588
No files matched your search
@@ -0,0 +1,320 @@
|
||||
import { ApiRequestError, invalidResponseCode } from "../../../shared/api";
|
||||
import {
|
||||
MatchSearchResponse as matchSearchResponseSchema,
|
||||
type MatchSearchResponseOutput,
|
||||
} from "../../../shared/api/schemas";
|
||||
|
||||
type MatchSearchSource = MatchSearchResponseOutput["source"];
|
||||
type NeedSource = Extract<MatchSearchSource, { kind: "need" }>;
|
||||
type BankSource = Extract<MatchSearchSource, { kind: "bank" }>;
|
||||
type BankConditions = NonNullable<BankSource["filters"]["conditions"]>;
|
||||
type ConfirmedCandidate = MatchSearchResponseOutput["groups"]["main"][number];
|
||||
type UnconfirmedCandidate = MatchSearchResponseOutput["groups"]["unconfirmed"][number];
|
||||
|
||||
export type MatchGroup = "main" | "near" | "unconfirmed";
|
||||
|
||||
export type MatchSearchSourceModel =
|
||||
| {
|
||||
readonly kind: "need";
|
||||
readonly needId: NeedSource["need_id"];
|
||||
readonly requirementsVersion: NeedSource["requirements_version"];
|
||||
}
|
||||
| {
|
||||
readonly kind: "bank";
|
||||
readonly filters: {
|
||||
readonly specialization: BankSource["filters"]["specialization"];
|
||||
readonly confirmedGrade: BankSource["filters"]["confirmed_grade"];
|
||||
readonly confirmedCategory: BankSource["filters"]["confirmed_category"];
|
||||
readonly confirmationStatus: BankSource["filters"]["confirmation_status"];
|
||||
readonly declaredGrade: BankSource["filters"]["declared_grade"];
|
||||
readonly stack: BankSource["filters"]["stack"];
|
||||
readonly conditions: {
|
||||
readonly workFormats: BankConditions["work_formats"];
|
||||
readonly salaryCompatibility: BankConditions["salary_compatibility"];
|
||||
} | null;
|
||||
};
|
||||
};
|
||||
|
||||
type MatchPublicContext = {
|
||||
readonly stack: ConfirmedCandidate["public_context"]["stack"];
|
||||
readonly experienceBand: ConfirmedCandidate["public_context"]["experience_band"];
|
||||
readonly workFormats: ConfirmedCandidate["public_context"]["work_formats"];
|
||||
readonly salaryCompatibility: ConfirmedCandidate["public_context"]["salary_compatibility"];
|
||||
};
|
||||
|
||||
type MatchEvidence = ConfirmedCandidate["evidence_summary"][number];
|
||||
type MatchUnknownFlag = ConfirmedCandidate["unknown_flags"][number];
|
||||
type ConfirmedComponents = ConfirmedCandidate["components"];
|
||||
type UnconfirmedComponents = UnconfirmedCandidate["components"];
|
||||
|
||||
export type MatchConfirmedComponentsModel = Omit<ConfirmedComponents, "rank_score"> & {
|
||||
readonly rankScore: ConfirmedComponents["rank_score"];
|
||||
};
|
||||
|
||||
export type MatchConfirmedCardModel = {
|
||||
readonly group: "main" | "near";
|
||||
readonly candidateHandle: ConfirmedCandidate["candidate_handle"];
|
||||
readonly specialization: ConfirmedCandidate["specialization"];
|
||||
readonly confirmationStatus: "confirmed";
|
||||
readonly declaredGrade: ConfirmedCandidate["declared_grade"];
|
||||
readonly category: ConfirmedCandidate["category"];
|
||||
readonly strength: ConfirmedCandidate["strength"];
|
||||
readonly publicContext: MatchPublicContext;
|
||||
readonly evidenceSummary: readonly MatchEvidence[];
|
||||
readonly components: MatchConfirmedComponentsModel;
|
||||
readonly unknownFlags: readonly MatchUnknownFlag[];
|
||||
};
|
||||
|
||||
export type MatchUnconfirmedCardModel = {
|
||||
readonly group: "unconfirmed";
|
||||
readonly candidateHandle: UnconfirmedCandidate["candidate_handle"];
|
||||
readonly specialization: UnconfirmedCandidate["specialization"];
|
||||
readonly confirmationStatus: "unconfirmed";
|
||||
readonly declaredGrade: UnconfirmedCandidate["declared_grade"];
|
||||
readonly category: null;
|
||||
readonly strength: null;
|
||||
readonly publicContext: MatchPublicContext;
|
||||
readonly evidenceSummary: readonly MatchEvidence[];
|
||||
readonly components: UnconfirmedComponents;
|
||||
readonly unknownFlags: readonly MatchUnknownFlag[];
|
||||
};
|
||||
|
||||
export type MatchCardModel = MatchConfirmedCardModel | MatchUnconfirmedCardModel;
|
||||
|
||||
export type MatchSearchModel = {
|
||||
readonly source: MatchSearchSourceModel;
|
||||
readonly cards: readonly MatchCardModel[];
|
||||
};
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function containsOnlyContractProperties(input: unknown, parsed: unknown): boolean {
|
||||
if (Array.isArray(input) || Array.isArray(parsed)) {
|
||||
return (
|
||||
Array.isArray(input) &&
|
||||
Array.isArray(parsed) &&
|
||||
input.length === parsed.length &&
|
||||
input.every((item, index) => containsOnlyContractProperties(item, parsed[index]))
|
||||
);
|
||||
}
|
||||
|
||||
if (isRecord(input) || isRecord(parsed)) {
|
||||
if (!isRecord(input) || !isRecord(parsed)) return false;
|
||||
const parsedKeys = new Set(Object.keys(parsed));
|
||||
if (Object.keys(input).some((key) => !parsedKeys.has(key))) return false;
|
||||
|
||||
return Object.entries(parsed).every(
|
||||
([key, value]) =>
|
||||
!Object.prototype.hasOwnProperty.call(input, key) ||
|
||||
containsOnlyContractProperties(input[key], value),
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function invalidMatchResponse(status: number): ApiRequestError {
|
||||
return new ApiRequestError({
|
||||
status,
|
||||
code: invalidResponseCode,
|
||||
title: "The response does not match the API contract.",
|
||||
});
|
||||
}
|
||||
|
||||
export function parseMatchSearchResponse(value: unknown, status = 200): MatchSearchResponseOutput {
|
||||
const parsed = matchSearchResponseSchema.safeParse(value);
|
||||
if (!parsed.success || !containsOnlyContractProperties(value, parsed.data)) {
|
||||
throw invalidMatchResponse(status);
|
||||
}
|
||||
return parsed.data;
|
||||
}
|
||||
|
||||
function sourceFromTransport(source: MatchSearchSource): MatchSearchSourceModel {
|
||||
if (source.kind === "need") {
|
||||
return {
|
||||
kind: "need",
|
||||
needId: source.need_id,
|
||||
requirementsVersion: source.requirements_version,
|
||||
};
|
||||
}
|
||||
|
||||
const conditions = source.filters.conditions;
|
||||
return {
|
||||
kind: "bank",
|
||||
filters: {
|
||||
specialization: source.filters.specialization,
|
||||
confirmedGrade: source.filters.confirmed_grade,
|
||||
confirmedCategory: source.filters.confirmed_category,
|
||||
confirmationStatus: source.filters.confirmation_status,
|
||||
declaredGrade: source.filters.declared_grade,
|
||||
stack: source.filters.stack,
|
||||
conditions:
|
||||
conditions === null
|
||||
? null
|
||||
: {
|
||||
workFormats: conditions.work_formats,
|
||||
salaryCompatibility: conditions.salary_compatibility,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function sourceToTransport(source: MatchSearchSourceModel): MatchSearchSource {
|
||||
if (source.kind === "need") {
|
||||
return {
|
||||
kind: "need",
|
||||
need_id: source.needId,
|
||||
requirements_version: source.requirementsVersion,
|
||||
};
|
||||
}
|
||||
|
||||
const conditions = source.filters.conditions;
|
||||
return {
|
||||
kind: "bank",
|
||||
filters: {
|
||||
specialization: source.filters.specialization,
|
||||
confirmed_grade: source.filters.confirmedGrade,
|
||||
confirmed_category: source.filters.confirmedCategory,
|
||||
confirmation_status: source.filters.confirmationStatus,
|
||||
declared_grade: source.filters.declaredGrade,
|
||||
stack: source.filters.stack === null ? null : [...source.filters.stack],
|
||||
conditions:
|
||||
conditions === null
|
||||
? null
|
||||
: {
|
||||
work_formats: conditions.workFormats === null ? null : [...conditions.workFormats],
|
||||
salary_compatibility: conditions.salaryCompatibility,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function publicContextFromTransport(
|
||||
context: ConfirmedCandidate["public_context"],
|
||||
): MatchPublicContext {
|
||||
return {
|
||||
stack: context.stack === null ? null : [...context.stack],
|
||||
experienceBand: context.experience_band,
|
||||
workFormats: context.work_formats === null ? null : [...context.work_formats],
|
||||
salaryCompatibility: context.salary_compatibility,
|
||||
};
|
||||
}
|
||||
|
||||
function publicContextToTransport(
|
||||
context: MatchPublicContext,
|
||||
): ConfirmedCandidate["public_context"] {
|
||||
return {
|
||||
stack: context.stack === null ? null : [...context.stack],
|
||||
experience_band: context.experienceBand,
|
||||
work_formats: context.workFormats === null ? null : [...context.workFormats],
|
||||
salary_compatibility: context.salaryCompatibility,
|
||||
};
|
||||
}
|
||||
|
||||
function confirmedFromTransport(
|
||||
group: "main" | "near",
|
||||
candidate: ConfirmedCandidate,
|
||||
): MatchConfirmedCardModel {
|
||||
const { rank_score: rankScore, ...components } = candidate.components;
|
||||
return {
|
||||
group,
|
||||
candidateHandle: candidate.candidate_handle,
|
||||
specialization: candidate.specialization,
|
||||
confirmationStatus: candidate.confirmation_status,
|
||||
declaredGrade: candidate.declared_grade,
|
||||
category: candidate.category,
|
||||
strength: candidate.strength,
|
||||
publicContext: publicContextFromTransport(candidate.public_context),
|
||||
evidenceSummary: candidate.evidence_summary.map(({ code, state }) => ({ code, state })),
|
||||
components: { ...components, rankScore },
|
||||
unknownFlags: candidate.unknown_flags.map(({ component, reason }) => ({ component, reason })),
|
||||
};
|
||||
}
|
||||
|
||||
function unconfirmedFromTransport(candidate: UnconfirmedCandidate): MatchUnconfirmedCardModel {
|
||||
return {
|
||||
group: "unconfirmed",
|
||||
candidateHandle: candidate.candidate_handle,
|
||||
specialization: candidate.specialization,
|
||||
confirmationStatus: candidate.confirmation_status,
|
||||
declaredGrade: candidate.declared_grade,
|
||||
category: null,
|
||||
strength: null,
|
||||
publicContext: publicContextFromTransport(candidate.public_context),
|
||||
evidenceSummary: candidate.evidence_summary.map(({ code, state }) => ({ code, state })),
|
||||
components: { fit: candidate.components.fit, coverage: candidate.components.coverage },
|
||||
unknownFlags: candidate.unknown_flags.map(({ component, reason }) => ({ component, reason })),
|
||||
};
|
||||
}
|
||||
|
||||
export function matchSearchFromTransport(value: unknown, status = 200): MatchSearchModel {
|
||||
const response = parseMatchSearchResponse(value, status);
|
||||
return {
|
||||
source: sourceFromTransport(response.source),
|
||||
cards: [
|
||||
...response.groups.main.map((candidate) => confirmedFromTransport("main", candidate)),
|
||||
...response.groups.near.map((candidate) => confirmedFromTransport("near", candidate)),
|
||||
...response.groups.unconfirmed.map(unconfirmedFromTransport),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
function confirmedToTransport(candidate: MatchConfirmedCardModel): ConfirmedCandidate {
|
||||
const { rankScore, ...components } = candidate.components;
|
||||
return {
|
||||
candidate_handle: candidate.candidateHandle,
|
||||
specialization: candidate.specialization,
|
||||
confirmation_status: "confirmed",
|
||||
declared_grade: candidate.declaredGrade,
|
||||
category: candidate.category,
|
||||
strength: candidate.strength,
|
||||
public_context: publicContextToTransport(candidate.publicContext),
|
||||
evidence_summary: candidate.evidenceSummary.map(({ code, state }) => ({ code, state })),
|
||||
components: { ...components, rank_score: rankScore },
|
||||
unknown_flags: candidate.unknownFlags.map(({ component, reason }) => ({ component, reason })),
|
||||
};
|
||||
}
|
||||
|
||||
function unconfirmedToTransport(candidate: MatchUnconfirmedCardModel): UnconfirmedCandidate {
|
||||
return {
|
||||
candidate_handle: candidate.candidateHandle,
|
||||
specialization: candidate.specialization,
|
||||
confirmation_status: "unconfirmed",
|
||||
declared_grade: candidate.declaredGrade,
|
||||
category: null,
|
||||
strength: null,
|
||||
public_context: publicContextToTransport(candidate.publicContext),
|
||||
evidence_summary: candidate.evidenceSummary.map(({ code, state }) => ({ code, state })),
|
||||
components: { fit: candidate.components.fit, coverage: candidate.components.coverage },
|
||||
unknown_flags: candidate.unknownFlags.map(({ component, reason }) => ({ component, reason })),
|
||||
};
|
||||
}
|
||||
|
||||
export function matchSearchToTransport(model: MatchSearchModel): MatchSearchResponseOutput {
|
||||
const groups: MatchSearchResponseOutput["groups"] = {
|
||||
main: [],
|
||||
near: [],
|
||||
unconfirmed: [],
|
||||
};
|
||||
|
||||
for (const candidate of model.cards) {
|
||||
switch (candidate.group) {
|
||||
case "main":
|
||||
groups.main.push(confirmedToTransport(candidate));
|
||||
break;
|
||||
case "near":
|
||||
groups.near.push(confirmedToTransport(candidate));
|
||||
break;
|
||||
case "unconfirmed":
|
||||
groups.unconfirmed.push(unconfirmedToTransport(candidate));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return matchSearchResponseSchema.parse({
|
||||
source: sourceToTransport(model.source),
|
||||
groups,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user