update the IsLicenseReplaced and for the accessories

This commit is contained in:
2026-11-11 17:20:02 +03:30
parent 54459877a2
commit 90bdc51715
13 changed files with 693 additions and 124 deletions
@@ -0,0 +1,113 @@
import { asLookupRows } from "./fanavaran-hull-expertise-lookups";
export type HullLookupStage = "baseClaim" | "expertise" | "files";
type LookupField = {
row: Record<string, unknown>;
key: string;
lookup: string;
path: string;
};
/** Validate outbound IDs against responses for this tenant, never a sample ID. */
export async function validateFanavaranHullLookupIds(
payload: Record<string, unknown>,
stage: HullLookupStage,
fetchRows: (lookup: string) => Promise<unknown>,
): Promise<string[]> {
const fields: LookupField[] = [];
const add = (
row: Record<string, unknown>,
key: string,
lookup: string,
path = key,
required = true,
) => {
if (required || row[key] != null) fields.push({ row, key, lookup, path });
};
if (stage === "baseClaim") {
for (const [key, lookup] of Object.entries({
AccidentCityId: "cities",
AccidentReportTypeId: "accident-report-type",
AccidentCauseId: "accident-causes",
AccidentTypeId: "vehicle-hull-accident-types",
CulpritTypeId: "vehicle-hull-accident-culprit-type",
AuthorityCulpritId: "detection-accident-culprits",
CulpritLicenceTypeId: "driving-licence-types",
AccidentVehicleUsedId: "vehicle-use-types",
}))
add(payload, key, lookup);
for (const key of [
"IsLicenseReplaced",
"IsOwnerChanged",
"CostSeparationToDmgSections",
"IsAccidentOutOfBorder",
"IsSurplusArticleEighthLaw",
])
add(payload, key, "ans", key, false);
add(payload, "CulpritLicenceCountryId", "countries", undefined, false);
add(payload, "CulpritLicenceCityId", "cities", undefined, false);
add(payload, "NotifierTypeId", "notifier-type", undefined, false);
add(payload, "NotifierRelationId", "notifier-relation", undefined, false);
} else if (stage === "files") {
add(payload, "FileTypeId", "file-types");
for (const [i, row] of asLookupRows(payload.Files).entries())
add(row, "FileTypeId", "file-types", `Files[${i}].FileTypeId`);
} else {
for (const [i, row] of asLookupRows(payload.DmgSections).entries()) {
const prefix = `DmgSections[${i}]`;
add(
row,
"AccessoryKindId",
"car-components",
`${prefix}.AccessoryKindId`,
);
add(
row,
"VehicleHullAccessoryId",
"policy-dmg-accessories",
`${prefix}.VehicleHullAccessoryId`,
);
add(row, "DmgKindId", "vehicle-hull-dmg-kind", `${prefix}.DmgKindId`);
for (const [j, cost] of asLookupRows(row.DmgSectionCosts).entries())
add(
cost,
"DmgCostKindId",
"vehicle-hull-dmg-cost-kinds",
`${prefix}.DmgSectionCosts[${j}].DmgCostKindId`,
);
}
}
const sources = new Map<string, Record<string, unknown>[]>();
await Promise.all(
[...new Set(fields.map((field) => field.lookup))].map(async (lookup) => {
try {
sources.set(lookup, asLookupRows(await fetchRows(lookup)));
} catch {
sources.set(lookup, []);
}
}),
);
const warnings: string[] = [];
for (const field of fields) {
const raw = field.row[field.key];
const id = raw == null || String(raw).trim() === "" ? NaN : Number(raw);
const matched =
Number.isSafeInteger(id) && id >= 0
? sources
.get(field.lookup)
?.find(
(row) =>
row.Id != null &&
Number(row.Id) === id &&
row.IsActive !== false &&
(row.IsActive == null || Number(row.IsActive) !== 0),
)
: undefined;
if (!matched)
warnings.push(
`${field.path}=${raw ?? "(missing)"} is not in the current ${field.lookup} API response.`,
);
else field.row[field.key] = Number(matched.Id);
}
return warnings;
}