Fix damaged vehicle identity across claim panels and price estimates

This commit is contained in:
SepehrYahyaee
2026-10-06 14:20:15 +03:30
parent b11128c638
commit 4564798db1
10 changed files with 375 additions and 199 deletions
@@ -3,7 +3,7 @@ import {
coerceDamagedPartsMediaToArray,
normalizeDamageSelectedParts,
} from "src/helpers/outer-damage-parts";
import { resolveClaimBeneficiary } from "src/helpers/blame-claim-eligibility";
import { resolveClaimVehicleSources } from "src/helpers/claim-vehicle-identity";
import {
CreatePriceEstimateDto,
EstimatePartQuality,
@@ -42,44 +42,9 @@ export function buildEstimateContext(
options: CreatePriceEstimateDto,
now = new Date(),
): EstimateContext {
const beneficiary = blame ? resolveClaimBeneficiary(blame) : null;
const beneficiaryId =
beneficiary?.person?.userId ?? claim?.damagedPartyUserId;
const sourceParties = blame?.parties?.length
? blame.parties
: claim?.snapshot?.parties;
const party =
beneficiary ??
sourceParties?.find(
(p: any) =>
beneficiaryId && String(p.person?.userId) === String(beneficiaryId),
);
const vehicle = party?.vehicle ?? {};
const inquiry = vehicle.inquiry?.mapped ?? vehicle.inquiry ?? {};
// FIRST inquiry on a third-party claim belongs to the culprit. Use only the resolved damaged party's role.
const policy = party?.role
? (claim?.inquiries?.carBody?.data?.[party.role]?.mapped ??
claim?.inquiries?.thirdParty?.data?.[party.role]?.mapped ??
{})
: {};
const review = claim?.fanavaranVehicleReview?.approved
? claim.fanavaranVehicleReview
: {};
const isBeneficiaryOwner =
beneficiaryId && String(claim?.owner?.userId) === String(beneficiaryId);
// An absent blame record does not make a historical culprit-owned snapshot safe.
// Only standalone legacy claims can use their owner as the beneficiary fallback.
const standaloneOwner =
!claim?.blameRequestId &&
!blame &&
!claim?.snapshot?.parties?.length &&
!beneficiaryId &&
!!claim?.owner?.userId;
const snapshot =
isBeneficiaryOwner || standaloneOwner ? (claim?.vehicle ?? {}) : {};
const { party, vehicle, inquiry, policy, snapshot, review, identity } =
resolveClaimVehicleSources(claim, blame);
const bodyType = text(snapshot.carType ?? vehicle.type);
const cleanName = (raw: unknown) =>
text(raw) === bodyType ? null : text(raw);
const rawYear =
options.modelYear ??
positive(
@@ -175,6 +140,10 @@ export function buildEstimateContext(
warnings.push(
"هویت خودروی زیان‌دیده در اطلاعات طرفین مشخص نیست؛ نام یا مدل خودرو ممکن است ناقص باشد.",
);
if (identity.conflictingNames)
warnings.push(
"نام خودرو در فیلدهای استعلام هم‌خوان نیست؛ تیپ تأییدنشده ارسال نشد و اطلاعات خودرو نیاز به بررسی کارشناس دارد.",
);
if (
parts.some(
(part) => !photos.some((photo) => photo.partKey === part.partKey),
@@ -192,17 +161,8 @@ export function buildEstimateContext(
partQuality: options.partQuality ?? EstimatePartQuality.OEM,
},
vehicle: {
make:
text(review.vehicleSystem?.caption) ??
cleanName(vehicle.name) ??
text(inquiry.SystemField ?? inquiry.MapTypNam) ??
text(policy.SystemField ?? policy.MapTypNam) ??
cleanName(snapshot.carName),
model:
text(review.vehicleKind?.caption) ??
cleanName(vehicle.model) ??
text(inquiry.TypeField ?? policy.TypeField) ??
cleanName(snapshot.carModel),
make: identity.name ?? null,
model: identity.model ?? null,
bodyType,
vehicleSystemId: positive(review.vehicleSystem?.id),
vehicleKindId: positive(review.vehicleKind?.id),
@@ -323,6 +323,25 @@ describe("price estimate input isolation", () => {
const safe = JSON.stringify(publicEstimateContext(input));
expect(safe).not.toMatch(/PRIVATE|CULPRIT|WRONG-OWNER|files\/claim/);
});
it("does not send the unrelated 111SE variant from a conflicting FAW inquiry", () => {
const { claim, blame } = source();
blame.parties[1].vehicle = {
name: "فائو ( FAW )",
model: "111SE",
inquiry: {
mapped: {
MapTypNam: "فائو ( FAW )",
SystemField: "سايپا",
TypeField: "111SE",
},
},
};
const input = buildEstimateContext(claim, blame, {});
expect(input.vehicle.make).toBe("فائو ( FAW )");
expect(input.vehicle.model).toBeNull();
expect(input.warnings.join(" ")).toContain("هم‌خوان نیست");
expect(JSON.stringify(publicEstimateContext(input))).not.toContain("111SE");
});
it("uses body insurance FIRST, expert-approved vehicle names and supplied current market value", () => {
const { claim, blame } = source();
blame.type = "CAR_BODY";