fix(car-body): validate policy coverage on accident date

Validate hull coverage against the accident date, select the covering renewal, and defer validation until that date is collected.
This commit is contained in:
2026-11-21 12:04:09 +03:30
parent 8f15040080
commit 5c6e21dd14
13 changed files with 499 additions and 56 deletions
@@ -1,3 +1,8 @@
import {
isPolicyActiveOn,
policyAccidentDate,
selectLastAmongCarMatches,
} from "src/lookups/fanavaran-last-car-policy";
import { BadRequestException } from "@nestjs/common";
import { jalaliToGregorianDate } from "src/helpers/date-jalali";
import { gregorianDateInIran } from "src/helpers/iran-datetime";
@@ -93,3 +98,34 @@ export function selectLatestActiveFanavaranPolicy(
return { ...latest, policyId };
}
/** Hull renewals must be selected for the loss date, regardless of submission date. */
export function selectFanavaranPolicyForAccident(
policies: unknown,
accidentDate: unknown,
): SelectedFanavaranPolicy {
const date = policyAccidentDate(accidentDate);
if (!date)
throw new BadRequestException(
"A valid accidentDate is required for car-body policy selection.",
);
if (!Array.isArray(policies) || policies.length === 0) {
throw new BadRequestException(NO_POLICY_MESSAGE);
}
const matches = policies
.filter((row) => row && typeof row === "object")
.map((row) => ({
policyId: parsePolicyId(row.PolicyId) ?? 0,
beginDate: row.BeginDate,
endDate: row.EndDate,
policy: row,
vehicle: null,
}));
const selected = selectLastAmongCarMatches(matches, date);
if (!selected || !isPolicyActiveOn(selected, date)) {
throw new BadRequestException(
"No Fanavaran car-body policy covers the accident date.",
);
}
return selectLatestActiveFanavaranPolicy([selected.policy], date);
}