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
@@ -312,7 +312,10 @@ import {
resolvePolicyholderFromParty,
resolveThirdPartyCulpritTypeId,
} from './fanavaran-party-roles';
import { selectLatestActiveFanavaranPolicy } from './fanavaran-policy-selection';
import {
selectLatestActiveFanavaranPolicy,
selectFanavaranPolicyForAccident,
} from './fanavaran-policy-selection';
export interface FanavaranAutoSubmitResult {
attempted: boolean;
@@ -3959,6 +3962,7 @@ export class ClaimRequestManagementService {
claimCaseId?: string;
product?: FanavaranClaimProduct;
insuranceLineId?: number;
accidentDate?: Date | string;
},
): Promise<number | null> {
const clientKey =
@@ -4024,7 +4028,10 @@ export class ClaimRequestManagementService {
this.logger.log(
`${logPrefix} Policy inquiry response status=${response.status} count=${policyCount}`,
);
const selectedPolicy = selectLatestActiveFanavaranPolicy(response.data);
const selectedPolicy =
product === 'car-body'
? selectFanavaranPolicyForAccident(response.data, options?.accidentDate)
: selectLatestActiveFanavaranPolicy(response.data);
this.logger.log(
`${logPrefix} Selected latest active policy PolicyId=${selectedPolicy.policyId} EndDate=${selectedPolicy.endDate}`,
);
@@ -4719,7 +4726,12 @@ export class ClaimRequestManagementService {
profile.auth,
logPrefix,
auditSession,
{ clientKey, claimCaseId, product: 'car-body' },
{
clientKey,
claimCaseId,
product: 'car-body',
accidentDate: firstParty?.statement?.accidentDate,
},
);
debug.values.policyId = policyId;
debug.steps.policyInquirySucceeded = policyId !== null;
@@ -1,5 +1,8 @@
import { BadRequestException } from "@nestjs/common";
import { selectLatestActiveFanavaranPolicy } from "./fanavaran-policy-selection";
import {
selectLatestActiveFanavaranPolicy,
selectFanavaranPolicyForAccident,
} from "./fanavaran-policy-selection";
describe("selectLatestActiveFanavaranPolicy", () => {
const today = "2026-06-30";
@@ -61,3 +64,29 @@ describe("selectLatestActiveFanavaranPolicy", () => {
).toThrow(BadRequestException);
});
});
describe("car-body accident policy selection", () => {
const policies = [
{ PolicyId: 2, BeginDate: "1401/01/02", EndDate: "1402/01/02" },
{ PolicyId: 1, BeginDate: "1400/01/01", EndDate: "1401/01/01" },
];
it("selects the previous renewal covering the accident, even when both expired today", () => {
expect(
selectFanavaranPolicyForAccident(policies, "1400/12/28").policyId,
).toBe(1);
});
it.each(["1400/01/01", "1401/01/01"])(
"includes the coverage boundary %s",
(date) => {
expect(selectFanavaranPolicyForAccident(policies, date).policyId).toBe(1);
},
);
it.each(["1399/12/29", "1402/01/03", "invalid", undefined])(
"rejects an uncovered or missing accident date %s",
(date) => {
expect(() => selectFanavaranPolicyForAccident(policies, date)).toThrow(
BadRequestException,
);
},
);
});
@@ -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);
}