car body policy and sales extended

This commit is contained in:
2026-09-05 18:37:42 +03:30
parent eb072ef080
commit eb636bdbdd
10 changed files with 1097 additions and 14 deletions

View File

@@ -0,0 +1,194 @@
import {
filterPoliciesByLine,
parseLastCarPolicyInput,
pickVehicleId,
plaqueMatchesVehicle,
selectLastAmongCarMatches,
vehicleMatchesCar,
} from "./fanavaran-last-car-policy";
describe("fanavaran last car policy", () => {
const today = "2026-09-05";
it("parses national code and prefers VIN when both identifiers are sent", () => {
const parsed = parseLastCarPolicyInput({
nationalCode: "۰۰۱۲۳۴۵۶۷۸",
vin: " irnkaek4150012345 ",
plaqueLeft: "12",
plaqueLetter: "ب",
plaqueRight: "345",
plaqueSerial: "67",
});
expect(parsed).toEqual({
nationalCode: "0012345678",
vin: "IRNKAEK4150012345",
plaque: { left: "12", letter: "ب", right: "345", serial: "67" },
});
});
it("rejects a partial plaque when VIN is missing", () => {
const parsed = parseLastCarPolicyInput({
nationalCode: "0012345678",
plaqueLeft: "12",
plaqueLetter: "ب",
});
expect(parsed).toEqual({
error:
"plaque requires all four parts: plaqueLeft, plaqueLetter, plaqueRight, plaqueSerial",
});
});
it("keeps only the requested insurance line", () => {
const rows = filterPoliciesByLine(
[
{ PolicyId: 1, InsuranceLineId: 5, EndDate: "1405/01/01" },
{ PolicyId: 2, InsuranceLineId: 4, EndDate: "1405/01/01" },
{ PolicyId: 3, EndDate: "1405/01/01" },
],
5,
);
expect(rows.map((row) => row.PolicyId)).toEqual([1, 3]);
});
it("reads VehicleId from a one-item inquiry-by-vin array", () => {
expect(pickVehicleId([{ Id: 4118289, VIN: "NAAR03HFFRDE07024" }])).toBe(
4118289,
);
});
it("matches VIN against ChassisNo when VIN is empty", () => {
expect(
vehicleMatchesCar(
{ VIN: null, ChassisNo: "NAAR03HFFRDE07024" },
{ vin: "NAAR03HFFRDE07024" },
),
).toBe(true);
});
it("matches by VIN-inquiry VehicleId when GEN.15 VIN is empty", () => {
expect(
vehicleMatchesCar(
{ Id: 4118289, VIN: null, ChassisNo: "RDE07024" },
{ vin: "NAAR03HFFRDE07024" },
4118289,
),
).toBe(true);
});
it("matches VIN trim and case-insensitively", () => {
expect(
vehicleMatchesCar(
{ VIN: " irnKAEK4150012345 " },
{ vin: "IRNKAEK4150012345" },
),
).toBe(true);
});
it("matches plaque by all four parts including letter code", () => {
expect(
plaqueMatchesVehicle(
{ left: "12", letter: "ب", right: "345", serial: "67" },
{
PlaqueLeftNo: "12",
PlaqueMiddleCodeId: 2,
PlaqueRightNo: "345",
PlaqueSerial: "67",
},
),
).toBe(true);
});
it("lets VIN win when both VIN and plaque are sent", () => {
expect(
vehicleMatchesCar(
{
VIN: "IRNKAEK4150012345",
PlaqueLeftNo: "99",
PlaqueMiddleCodeId: 1,
PlaqueRightNo: "111",
PlaqueSerial: "22",
},
{
vin: "IRNKAEK4150012345",
plaque: { left: "12", letter: "ب", right: "345", serial: "67" },
},
),
).toBe(true);
});
it("does not match plaque when any part differs", () => {
expect(
plaqueMatchesVehicle(
{ left: "12", letter: "ب", right: "345", serial: "67" },
{
PlaqueLeftNo: "12",
PlaqueMiddleCodeId: 2,
PlaqueRightNo: "345",
PlaqueSerial: "68",
},
),
).toBe(false);
});
it("picks the active renewal with the newest BeginDate", () => {
const selected = selectLastAmongCarMatches(
[
{
policyId: 10,
beginDate: "1404/06/01",
endDate: "1405/06/01",
policy: {},
vehicle: null,
},
{
policyId: 20,
beginDate: "1405/06/02",
endDate: "1406/06/01",
policy: {},
vehicle: null,
},
{
policyId: 30,
beginDate: "1403/01/01",
endDate: "1404/01/01",
policy: {},
vehicle: null,
},
],
today,
);
expect(selected?.policyId).toBe(20);
});
it("falls back to the latest EndDate when no match is active", () => {
const selected = selectLastAmongCarMatches(
[
{
policyId: 10,
beginDate: "1402/01/01",
endDate: "1403/01/01",
policy: {},
vehicle: null,
},
{
policyId: 20,
beginDate: "1403/01/01",
endDate: "1404/06/01",
policy: {},
vehicle: null,
},
],
today,
);
expect(selected?.policyId).toBe(20);
});
it("returns null when there are no matches", () => {
expect(selectLastAmongCarMatches([], today)).toBeNull();
});
});