forked from Yara724/api
feat(depreciation): apply Fanavaran tables and age eligibility
Use the supplied part, severity and age codes for depreciation. Exclude vehicles older than ten from calculated and manual amounts, preserve historical assessments, and send zero depreciation to Fanavaran for ineligible vehicles.
This commit is contained in:
@@ -0,0 +1,347 @@
|
||||
import { BadRequestException } from "@nestjs/common";
|
||||
import { model, Schema, Types } from "mongoose";
|
||||
import { ExpertClaimService } from "./expert-claim.service";
|
||||
import { ClaimPriceDropSchema } from "src/claim-request-management/entites/schema/claim-case.evaluation.schema";
|
||||
|
||||
const Stored = model(
|
||||
"FanavaranPriceDropTest",
|
||||
new Schema({ priceDrop: ClaimPriceDropSchema }),
|
||||
);
|
||||
|
||||
function setup() {
|
||||
const claim: any = {
|
||||
_id: new Types.ObjectId(),
|
||||
blameRequestId: new Types.ObjectId(),
|
||||
owner: { userId: "beneficiary" },
|
||||
vehicle: { carType: "sedan" },
|
||||
damage: {
|
||||
selectedParts: [
|
||||
{
|
||||
id: 9876,
|
||||
name: "9876",
|
||||
label_fa: "قطعه اختصاصی شرکت",
|
||||
side: "",
|
||||
catalogKey: "9876",
|
||||
},
|
||||
],
|
||||
},
|
||||
evaluation: {},
|
||||
};
|
||||
const blame: any = {
|
||||
type: "CAR_BODY",
|
||||
parties: [
|
||||
{
|
||||
role: "FIRST",
|
||||
person: { userId: "beneficiary" },
|
||||
vehicle: { inquiry: { mapped: { ModelField: "1402" } } },
|
||||
statement: { accidentDate: "2026-11-23" },
|
||||
},
|
||||
],
|
||||
};
|
||||
const service = Object.create(ExpertClaimService.prototype) as any;
|
||||
service.assertExpertCanEditClaimDuringReviewV2 = jest
|
||||
.fn()
|
||||
.mockResolvedValue(undefined);
|
||||
service.blameRequestDbService = {
|
||||
find: jest.fn().mockResolvedValue([blame]),
|
||||
};
|
||||
service.claimCaseDbService = {
|
||||
findById: jest.fn().mockResolvedValue(claim),
|
||||
findByIdAndUpdate: jest.fn(async (_id, update) => {
|
||||
// Exercise Mongoose's real strict schema casting: IDs and override flags
|
||||
// must survive persistence, not merely exist in the response object.
|
||||
claim.evaluation.priceDrop = new Stored({
|
||||
priceDrop: update.$set["evaluation.priceDrop"],
|
||||
}).toObject().priceDrop;
|
||||
return claim;
|
||||
}),
|
||||
};
|
||||
const body = {
|
||||
carPrice: 400_000_000,
|
||||
partSeverities: [
|
||||
{ partId: 9876, fanavaranPartId: 14, severity: "Moderate" },
|
||||
],
|
||||
};
|
||||
return { service, claim, blame, body, id: String(claim._id) };
|
||||
}
|
||||
|
||||
describe("Fanavaran depreciation review and persistence", () => {
|
||||
it("returns the Fanavaran catalog and age at accident without changing the saved assessment", async () => {
|
||||
const { service, claim, id } = setup();
|
||||
claim.evaluation.priceDrop = { total: 123, partLines: [] };
|
||||
const result = await service.getPriceDropContextV2(id, {});
|
||||
expect(result).toMatchObject({
|
||||
calculationVersion: "fanavaran-v1",
|
||||
accidentYear: 1405,
|
||||
suggestedCarModelYear: 1402,
|
||||
suggestedVehicleAge: 3,
|
||||
priceDrop: { total: 123 },
|
||||
});
|
||||
expect(result.priceDropCatalog).toHaveLength(17);
|
||||
expect(result.ageCatalog).toHaveLength(11);
|
||||
expect(result.damagedParts[0]).toMatchObject({
|
||||
partId: 9876,
|
||||
fanavaranPartId: null,
|
||||
});
|
||||
expect(service.claimCaseDbService.findByIdAndUpdate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("derives age and persists real Fanavaran category/severity/age codes", async () => {
|
||||
const { service, claim, id, body } = setup();
|
||||
const response = await service.upsertPriceDropV2(id, body, {});
|
||||
const expected = {
|
||||
total: 10_800_000,
|
||||
carPrice: 400_000_000,
|
||||
carModel: 1402,
|
||||
accidentYear: 1405,
|
||||
vehicleAge: 3,
|
||||
fanavaranAgeId: 4,
|
||||
coefficientYear: 2.7,
|
||||
manualOverride: false,
|
||||
calculationVersion: "fanavaran-v1",
|
||||
};
|
||||
expect(response.priceDrop).toMatchObject(expected);
|
||||
expect(claim.evaluation.priceDrop).toMatchObject(expected);
|
||||
expect(claim.evaluation.priceDrop.partLines[0]).toMatchObject({
|
||||
partId: 9876,
|
||||
fanavaranPartId: 14,
|
||||
fanavaranSeverityId: 41,
|
||||
coefficient: 4,
|
||||
});
|
||||
});
|
||||
|
||||
it("never uses a legacy tenant's caption just because component IDs collide", async () => {
|
||||
const { service, claim, id } = setup();
|
||||
// ID 52 means trunk-floor tray in the fallback snapshot, but this tenant
|
||||
// saved its own live caption: roof. The saved tenant caption is authoritative.
|
||||
claim.damage.selectedParts = [
|
||||
{ id: 52, name: "52", catalogKey: "52", side: "", label_fa: "سقف" },
|
||||
];
|
||||
const context = await service.getPriceDropContextV2(id, {});
|
||||
expect(context.damagedParts[0]).toMatchObject({
|
||||
partId: 52,
|
||||
label_fa: "سقف",
|
||||
fanavaranPartId: 1,
|
||||
});
|
||||
await service.upsertPriceDropV2(
|
||||
id,
|
||||
{
|
||||
carPrice: 400_000_000,
|
||||
vehicleAge: 3,
|
||||
partSeverities: [{ partId: 52, severity: "Severe" }],
|
||||
},
|
||||
{},
|
||||
);
|
||||
expect(claim.evaluation.priceDrop.partLines[0]).toMatchObject({
|
||||
partId: 52,
|
||||
fanavaranPartId: 1,
|
||||
fanavaranSeverityId: 3,
|
||||
coefficient: 7,
|
||||
});
|
||||
});
|
||||
|
||||
it("allows expert age when inquiry/date is unavailable and never sends a Gregorian year into the age table", async () => {
|
||||
const { service, claim, blame, id, body } = setup();
|
||||
blame.parties[0].vehicle.inquiry.mapped.ModelField = "2020";
|
||||
delete blame.parties[0].statement.accidentDate;
|
||||
await expect(service.upsertPriceDropV2(id, body, {})).rejects.toThrow(
|
||||
"سن خودرو",
|
||||
);
|
||||
await service.upsertPriceDropV2(id, { ...body, vehicleAge: 0 }, {});
|
||||
expect(claim.evaluation.priceDrop).toMatchObject({
|
||||
vehicleAge: 0,
|
||||
fanavaranAgeId: 1,
|
||||
total: 12_000_000,
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves the manual zero path and market price, and clears stale calculated rows", async () => {
|
||||
const { service, claim, id } = setup();
|
||||
claim.evaluation.priceDrop = {
|
||||
carPrice: 400_000_000,
|
||||
partLines: [{ coefficient: 7 }],
|
||||
total: 123,
|
||||
};
|
||||
await service.upsertPriceDropV2(id, { manualPriceDrop: 0 }, {});
|
||||
expect(claim.evaluation.priceDrop).toMatchObject({
|
||||
manualOverride: true,
|
||||
carPrice: 400_000_000,
|
||||
total: 0,
|
||||
partLines: [],
|
||||
});
|
||||
expect(service.blameRequestDbService.find).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("explicit recalculation replaces an old manual value and stores its source", async () => {
|
||||
const { service, claim, id, body } = setup();
|
||||
claim.evaluation.priceDrop = { manualOverride: true, total: 42 };
|
||||
await service.upsertPriceDropV2(id, body, {});
|
||||
expect(claim.evaluation.priceDrop).toMatchObject({
|
||||
manualOverride: false,
|
||||
total: 10_800_000,
|
||||
calculationVersion: "fanavaran-v1",
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ vehicleAge: -1 },
|
||||
{ vehicleAge: 1.5 },
|
||||
{ carPrice: "invalid" },
|
||||
{ carPrice: -1 },
|
||||
{
|
||||
partSeverities: [
|
||||
{ partId: 9876, fanavaranPartId: 17, severity: "Minor" },
|
||||
],
|
||||
},
|
||||
{
|
||||
partSeverities: [
|
||||
{ partId: 9876, fanavaranPartId: 999, severity: "Severe" },
|
||||
],
|
||||
},
|
||||
{ partSeverities: [{ partId: 50, severity: "Severe" }] },
|
||||
{ partSeverities: [null] },
|
||||
{ partSeverities: "bad" },
|
||||
])(
|
||||
"invalid calculation does not overwrite a previously saved result: %j",
|
||||
async (invalid) => {
|
||||
const { service, claim, id, body } = setup();
|
||||
claim.evaluation.priceDrop = { total: 777 };
|
||||
await expect(
|
||||
service.upsertPriceDropV2(id, { ...body, ...invalid }, {}),
|
||||
).rejects.toBeInstanceOf(BadRequestException);
|
||||
expect(claim.evaluation.priceDrop.total).toBe(777);
|
||||
expect(
|
||||
service.claimCaseDbService.findByIdAndUpdate,
|
||||
).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps the existing review authorization check before either write mode", async () => {
|
||||
const { service, id, body } = setup();
|
||||
service.assertExpertCanEditClaimDuringReviewV2.mockRejectedValue(
|
||||
new Error("denied"),
|
||||
);
|
||||
await expect(service.upsertPriceDropV2(id, body, {})).rejects.toThrow(
|
||||
"denied",
|
||||
);
|
||||
await expect(
|
||||
service.upsertPriceDropV2(id, { manualPriceDrop: 0 }, {}),
|
||||
).rejects.toThrow("denied");
|
||||
expect(service.claimCaseDbService.findByIdAndUpdate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("respects the age table start date without blocking manual totals", async () => {
|
||||
const { service, claim, blame, id, body } = setup();
|
||||
blame.parties[0].statement.accidentDate = "1404/05/27";
|
||||
const context = await service.getPriceDropContextV2(id, {});
|
||||
expect(context.calculationUnavailableReason).toContain("۱۴۰۴/۰۵/۲۸");
|
||||
await expect(
|
||||
service.upsertPriceDropV2(id, { ...body, vehicleAge: 3 }, {}),
|
||||
).rejects.toThrow("۱۴۰۴/۰۵/۲۸");
|
||||
expect(service.claimCaseDbService.findByIdAndUpdate).not.toHaveBeenCalled();
|
||||
await service.upsertPriceDropV2(id, { manualPriceDrop: 0 }, {});
|
||||
expect(claim.evaluation.priceDrop).toMatchObject({
|
||||
total: 0,
|
||||
manualOverride: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("makes an inferred age above ten ineligible in context and saves zero without requiring parts/prices", async () => {
|
||||
const { service, claim, blame, id } = setup();
|
||||
blame.parties[0].vehicle.inquiry.mapped.ModelField = "1394";
|
||||
const context = await service.getPriceDropContextV2(id, {});
|
||||
expect(context).toMatchObject({
|
||||
suggestedVehicleAge: 11,
|
||||
depreciationEligible: false,
|
||||
});
|
||||
expect(context.ineligibilityReason).toContain("بیشتر از ۱۰ سال");
|
||||
await service.upsertPriceDropV2(id, {}, {});
|
||||
expect(claim.evaluation.priceDrop).toMatchObject({
|
||||
vehicleAge: 11,
|
||||
eligible: false,
|
||||
total: 0,
|
||||
partLines: [],
|
||||
fanavaranAgeId: null,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([undefined, 0, 10])(
|
||||
"an entered age %s cannot bypass known old age for a positive manual override",
|
||||
async (vehicleAge) => {
|
||||
const { service, claim, blame, id } = setup();
|
||||
blame.parties[0].vehicle.inquiry.mapped.ModelField = "1394";
|
||||
await expect(
|
||||
service.upsertPriceDropV2(id, { manualPriceDrop: 1, vehicleAge }, {}),
|
||||
).rejects.toThrow("بیشتر از ۱۰ سال");
|
||||
expect(
|
||||
service.claimCaseDbService.findByIdAndUpdate,
|
||||
).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it("accepts manual zero for an old car and preserves its market value", async () => {
|
||||
const { service, claim, id } = setup();
|
||||
await service.upsertPriceDropV2(
|
||||
id,
|
||||
{ manualPriceDrop: 0, vehicleAge: 12, carPrice: 400_000_000 },
|
||||
{},
|
||||
);
|
||||
expect(claim.evaluation.priceDrop).toMatchObject({
|
||||
eligible: false,
|
||||
total: 0,
|
||||
vehicleAge: 12,
|
||||
carPrice: 400_000_000,
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps exactly age ten eligible for manual and calculated amounts", async () => {
|
||||
const { service, claim, id, body } = setup();
|
||||
await service.upsertPriceDropV2(id, { ...body, vehicleAge: 10 }, {});
|
||||
expect(claim.evaluation.priceDrop).toMatchObject({
|
||||
eligible: true,
|
||||
total: 8_200_000,
|
||||
vehicleAge: 10,
|
||||
});
|
||||
await service.upsertPriceDropV2(
|
||||
id,
|
||||
{ manualPriceDrop: 99, vehicleAge: 10 },
|
||||
{},
|
||||
);
|
||||
expect(claim.evaluation.priceDrop).toMatchObject({
|
||||
eligible: true,
|
||||
total: 99,
|
||||
vehicleAge: 10,
|
||||
});
|
||||
});
|
||||
|
||||
it("requires age for a positive manual amount when inquiry age is unknown, while allowing zero", async () => {
|
||||
const { service, claim, blame, id } = setup();
|
||||
delete blame.parties[0].vehicle.inquiry;
|
||||
await expect(
|
||||
service.upsertPriceDropV2(id, { manualPriceDrop: 99 }, {}),
|
||||
).rejects.toThrow("سن خودرو");
|
||||
await service.upsertPriceDropV2(id, { manualPriceDrop: 0 }, {});
|
||||
expect(claim.evaluation.priceDrop.total).toBe(0);
|
||||
});
|
||||
|
||||
it("allows correction of a mistyped expert-entered age when inquiry age is unknown", async () => {
|
||||
const { service, claim, blame, id } = setup();
|
||||
delete blame.parties[0].vehicle.inquiry;
|
||||
await service.upsertPriceDropV2(
|
||||
id,
|
||||
{ vehicleAge: 12, manualPriceDrop: 0 },
|
||||
{},
|
||||
);
|
||||
expect(claim.evaluation.priceDrop.eligible).toBe(false);
|
||||
await service.upsertPriceDropV2(
|
||||
id,
|
||||
{ vehicleAge: 10, manualPriceDrop: 99 },
|
||||
{},
|
||||
);
|
||||
expect(claim.evaluation.priceDrop).toMatchObject({
|
||||
eligible: true,
|
||||
vehicleAge: 10,
|
||||
total: 99,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user