forked from Yara724/api
Fix damaged vehicle identity across claim panels and price estimates
This commit is contained in:
@@ -46,6 +46,12 @@ export class ClaimDetailV2ResponseDto {
|
||||
plate?: any;
|
||||
};
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: "Party whose damaged vehicle is represented by vehicle and the approved vehicle review",
|
||||
enum: ["FIRST", "SECOND"],
|
||||
})
|
||||
vehiclePartyRole?: "FIRST" | "SECOND";
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: "Blame file type from linked blame case",
|
||||
enum: BlameRequestType,
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { Types } from "mongoose";
|
||||
import { resolveClaimVehicleSources } from "src/helpers/claim-vehicle-identity";
|
||||
import { ClaimRequestManagementService } from "src/claim-request-management/claim-request-management.service";
|
||||
import { ClaimCaseDbService } from "src/claim-request-management/entites/db-service/claim-case.db.service";
|
||||
import { ClaimRequestManagementDbService } from "src/claim-request-management/entites/db-service/claim-request-management.db.service";
|
||||
@@ -547,120 +548,31 @@ export class ExpertClaimService {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Map blame party `Vehicle` (name/model/type) to expert panel shape. */
|
||||
private expertVehicleFromPartyVehicle(v: any):
|
||||
| {
|
||||
carName?: string;
|
||||
carModel?: string;
|
||||
carType?: string;
|
||||
plate?: { plateId?: string };
|
||||
}
|
||||
| undefined {
|
||||
if (!v || typeof v !== "object") return undefined;
|
||||
const carName = v.name ?? v.carName;
|
||||
const carModel = v.model ?? v.carModel;
|
||||
const carType = v.type ?? v.carType;
|
||||
const plateId = v.plateId;
|
||||
const plate =
|
||||
plateId != null && String(plateId).length > 0
|
||||
? { plateId: String(plateId) }
|
||||
: undefined;
|
||||
if (!carName && !carModel && !carType && !plate) return undefined;
|
||||
return { carName, carModel, carType, ...(plate ? { plate } : {}) };
|
||||
}
|
||||
|
||||
/** Damaged party vehicle on blame (matches claim owner, or non-guilty party for THIRD_PARTY). */
|
||||
private damagedPartyVehicleFromBlame(
|
||||
blame: any,
|
||||
claim: any,
|
||||
):
|
||||
| {
|
||||
carName?: string;
|
||||
carModel?: string;
|
||||
carType?: string;
|
||||
plate?: { plateId?: string };
|
||||
}
|
||||
| undefined {
|
||||
const parties = blame?.parties;
|
||||
if (!Array.isArray(parties) || parties.length === 0) return undefined;
|
||||
|
||||
const ownerId = claim?.owner?.userId ? String(claim.owner.userId) : "";
|
||||
let party = ownerId
|
||||
? parties.find(
|
||||
(p: any) => p?.person?.userId && String(p.person.userId) === ownerId,
|
||||
)
|
||||
: undefined;
|
||||
|
||||
if (!party && blame?.expert?.decision?.guiltyPartyId) {
|
||||
const guilty = String(blame.expert.decision.guiltyPartyId);
|
||||
party = parties.find(
|
||||
(p: any) => p?.person?.userId && String(p.person.userId) !== guilty,
|
||||
);
|
||||
}
|
||||
|
||||
if (!party && parties.length === 1) party = parties[0];
|
||||
|
||||
return this.expertVehicleFromPartyVehicle(party?.vehicle);
|
||||
}
|
||||
|
||||
/** Extract car name / model from the inquiry snapshot embedded on the claim. */
|
||||
private vehicleNamesFromClaimInquiries(claim: any): {
|
||||
carName?: string;
|
||||
carModel?: string;
|
||||
} {
|
||||
// Claim copies inquiries from blame at creation time.
|
||||
// Try thirdParty first, then carBody.
|
||||
const mapped =
|
||||
claim?.inquiries?.thirdParty?.data?.FIRST?.mapped ??
|
||||
claim?.inquiries?.carBody?.data?.FIRST?.mapped ??
|
||||
{};
|
||||
const carName: string | undefined =
|
||||
mapped.MapTypNam || mapped.SystemField || undefined;
|
||||
const carModel: string | undefined = mapped.TypeField || undefined;
|
||||
return { carName, carModel };
|
||||
}
|
||||
|
||||
private vehicleForExpertFromClaimAndBlameMap(
|
||||
claim: any,
|
||||
blameById: Map<string, any>,
|
||||
):
|
||||
| {
|
||||
carName?: string;
|
||||
carModel?: string;
|
||||
carType?: string;
|
||||
plate?: { plateId?: string };
|
||||
}
|
||||
| undefined {
|
||||
const cv = claim?.vehicle;
|
||||
if (cv && (cv.carName || cv.carModel || cv.carType || (cv as any).plate)) {
|
||||
const carType: string | undefined = cv.carType;
|
||||
// carName/carModel that match the enum value (e.g. "sedan") are invalid —
|
||||
// the expert may have accidentally copied the carType value into those fields.
|
||||
// Fall back to the inquiry snapshot for the real brand / variant strings.
|
||||
let carName: string | undefined = cv.carName;
|
||||
let carModel: string | undefined = cv.carModel;
|
||||
if (
|
||||
!carName ||
|
||||
carName === carType ||
|
||||
!carModel ||
|
||||
carModel === carType
|
||||
) {
|
||||
const fromInquiry = this.vehicleNamesFromClaimInquiries(claim);
|
||||
if (!carName || carName === carType) carName = fromInquiry.carName;
|
||||
if (!carModel || carModel === carType) carModel = fromInquiry.carModel;
|
||||
}
|
||||
return {
|
||||
carName,
|
||||
carModel,
|
||||
carType,
|
||||
...((cv as any).plate ? { plate: (cv as any).plate } : {}),
|
||||
};
|
||||
}
|
||||
const bid = claim?.blameRequestId?.toString();
|
||||
if (!bid) return undefined;
|
||||
const blame = blameById.get(bid);
|
||||
if (!blame) return undefined;
|
||||
return this.damagedPartyVehicleFromBlame(blame, claim);
|
||||
): {
|
||||
carName?: string;
|
||||
carModel?: string;
|
||||
carType?: string;
|
||||
plate?: { plateId?: string };
|
||||
} {
|
||||
const blame = blameById.get(String(claim?.blameRequestId ?? ""));
|
||||
const { identity, vehicle, snapshot } = resolveClaimVehicleSources(
|
||||
claim,
|
||||
blame,
|
||||
);
|
||||
const plateId = vehicle?.plateId;
|
||||
return {
|
||||
carName: identity.name,
|
||||
carModel: identity.model,
|
||||
carType: claim?.vehicle?.carType ?? identity.type,
|
||||
...(snapshot.plate
|
||||
? { plate: snapshot.plate }
|
||||
: plateId
|
||||
? { plate: { plateId: String(plateId) } }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3907,15 +3819,6 @@ export class ExpertClaimService {
|
||||
currentSelectedParts: selectedNormExpert,
|
||||
});
|
||||
|
||||
// Vehicle payload — fall back to blame inquiry if claim vehicle is sparse
|
||||
let vehiclePayload = claim.vehicle as any;
|
||||
const hasClaimVehicle =
|
||||
vehiclePayload &&
|
||||
(vehiclePayload.carName ||
|
||||
vehiclePayload.carModel ||
|
||||
vehiclePayload.carType ||
|
||||
vehiclePayload.plate);
|
||||
|
||||
let blameLean: any = null;
|
||||
|
||||
const videoCaptureIdRaw = (claim.media as any)?.videoCaptureId;
|
||||
@@ -3944,33 +3847,15 @@ export class ExpertClaimService {
|
||||
blameLean = blameLeanRows[0] ?? null;
|
||||
}
|
||||
|
||||
if (!hasClaimVehicle && blameLean) {
|
||||
const fromBlame = this.damagedPartyVehicleFromBlame(blameLean, claim);
|
||||
if (fromBlame) vehiclePayload = fromBlame;
|
||||
}
|
||||
|
||||
// Patch up carName/carModel when they equal carType (invalid — expert copied the enum value).
|
||||
if (vehiclePayload) {
|
||||
const carType: string | undefined = vehiclePayload.carType;
|
||||
if (
|
||||
carType &&
|
||||
(!vehiclePayload.carName ||
|
||||
vehiclePayload.carName === carType ||
|
||||
!vehiclePayload.carModel ||
|
||||
vehiclePayload.carModel === carType)
|
||||
) {
|
||||
const fromInquiry = this.vehicleNamesFromClaimInquiries(claim);
|
||||
if (!vehiclePayload.carName || vehiclePayload.carName === carType) {
|
||||
vehiclePayload = { ...vehiclePayload, carName: fromInquiry.carName };
|
||||
}
|
||||
if (!vehiclePayload.carModel || vehiclePayload.carModel === carType) {
|
||||
vehiclePayload = {
|
||||
...vehiclePayload,
|
||||
carModel: fromInquiry.carModel,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
// Use the same damaged-party identity resolution as price estimation and list rows.
|
||||
const resolvedVehicle = this.vehicleForExpertFromClaimAndBlameMap(
|
||||
claim,
|
||||
new Map(blameLean ? [[String(claim.blameRequestId), blameLean]] : []),
|
||||
);
|
||||
const vehiclePayload = {
|
||||
...((claim.vehicle as any) ?? {}),
|
||||
...resolvedVehicle,
|
||||
};
|
||||
|
||||
const blameFileContext = blameLean
|
||||
? this.blameFileContextForExpert(blameLean)
|
||||
@@ -4088,6 +3973,7 @@ export class ExpertClaimService {
|
||||
vehicle: vehiclePayload
|
||||
? this.sanitizeVehicleInquiryForApi(vehiclePayload)
|
||||
: undefined,
|
||||
vehiclePartyRole: resolveClaimVehicleSources(claim, blameLean).party?.role,
|
||||
...blameFileContext,
|
||||
priceCap,
|
||||
blameRequestId: claim.blameRequestId?.toString(),
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
import { resolveVehicleIdentity } from "./vehicle-identity";
|
||||
import { resolveClaimVehicleSources } from "./claim-vehicle-identity";
|
||||
import { ExpertClaimService } from "src/expert-claim/expert-claim.service";
|
||||
import { buildEstimateContext } from "src/price-estimation/price-estimation-context";
|
||||
|
||||
describe("vehicle identity consistency", () => {
|
||||
it("retains saved vehicle names when the verified beneficiary's party data is sparse", () => {
|
||||
const claim = {
|
||||
damagedPartyUserId: "damaged",
|
||||
owner: { userId: "damaged", userRole: "SECOND" },
|
||||
vehicle: { carName: "پژو", carModel: "۲۰۶", carType: "sedan" },
|
||||
snapshot: {
|
||||
parties: [
|
||||
{ role: "SECOND", person: { userId: "damaged" }, vehicle: {} },
|
||||
],
|
||||
},
|
||||
};
|
||||
expect(resolveClaimVehicleSources(claim, null).identity).toMatchObject({
|
||||
name: "پژو",
|
||||
model: "۲۰۶",
|
||||
});
|
||||
claim.owner.userId = "culprit";
|
||||
expect(
|
||||
resolveClaimVehicleSources(claim, null).identity.name,
|
||||
).toBeUndefined();
|
||||
});
|
||||
it("rejects body classes as names and never uses manufacturing year as model", () => {
|
||||
expect(
|
||||
resolveVehicleIdentity({ name: "sedan", model: "sedan" }, [
|
||||
{ MapTypNam: "پژو", TypeField: "۲۰۶", ModelField: "1402" },
|
||||
]),
|
||||
).toMatchObject({ name: "پژو", model: "۲۰۶" });
|
||||
expect(
|
||||
resolveVehicleIdentity({ name: "sedan", model: "SUV" }, [
|
||||
{ ModelField: "1402" },
|
||||
]),
|
||||
).toMatchObject({ name: undefined, model: undefined });
|
||||
});
|
||||
it("leaves the variant unknown for the historical FAW/Saipa mixed fixture", () => {
|
||||
expect(
|
||||
resolveVehicleIdentity({ name: "فائو ( FAW )" }, [
|
||||
{ MapTypNam: "فائو ( FAW )", SystemField: "سايپا", TypeField: "111SE" },
|
||||
]),
|
||||
).toMatchObject({
|
||||
name: "فائو ( FAW )",
|
||||
model: undefined,
|
||||
conflictingNames: true,
|
||||
});
|
||||
});
|
||||
it("keeps explicit Fanavaran system and kind paired and permits approved corrections", () => {
|
||||
const vehicle = { name: "old-name", model: "old-model" };
|
||||
expect(
|
||||
resolveVehicleIdentity(vehicle, [
|
||||
{
|
||||
vehicleSystemTitle: "پژو",
|
||||
vehicleKind: "۲۰۶ تیپ ۲",
|
||||
MapTypNam: "۲۰۶ تیپ ۲",
|
||||
builtYear: 1402,
|
||||
},
|
||||
]),
|
||||
).toMatchObject({
|
||||
name: "پژو",
|
||||
model: "۲۰۶ تیپ ۲",
|
||||
conflictingNames: false,
|
||||
});
|
||||
expect(
|
||||
resolveVehicleIdentity(vehicle, [], {
|
||||
approved: true,
|
||||
vehicleSystem: { caption: "مزدا" },
|
||||
vehicleKind: { caption: "۳" },
|
||||
}),
|
||||
).toMatchObject({ name: "مزدا", model: "۳" });
|
||||
});
|
||||
it.each(["FIRST", "SECOND"])(
|
||||
"uses the same damaged %s vehicle for expert lists and AI without FIRST-policy borrowing",
|
||||
(damagedRole) => {
|
||||
const first = {
|
||||
role: "FIRST",
|
||||
person: { userId: "first" },
|
||||
vehicle: {
|
||||
name: "sedan",
|
||||
type: "sedan",
|
||||
inquiry: {
|
||||
mapped: {
|
||||
MapTypNam: "پژو",
|
||||
TypeField: "۲۰۶",
|
||||
SystemField: "پژو",
|
||||
ModelField: 1402,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const second = {
|
||||
role: "SECOND",
|
||||
person: { userId: "second" },
|
||||
vehicle: {
|
||||
name: "sedan",
|
||||
type: "sedan",
|
||||
inquiry: {
|
||||
mapped: {
|
||||
MapTypNam: "فائو ( FAW )",
|
||||
SystemField: "فائو ( FAW )",
|
||||
TypeField: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const damaged = damagedRole === "FIRST" ? first : second;
|
||||
const guilty = damagedRole === "FIRST" ? second : first;
|
||||
const blame = {
|
||||
type: "THIRD_PARTY",
|
||||
expert: { decision: { guiltyPartyId: guilty.person.userId } },
|
||||
parties: [first, second],
|
||||
};
|
||||
const claim = {
|
||||
blameRequestId: "blame",
|
||||
damagedPartyUserId: damaged.person.userId,
|
||||
owner: { userId: guilty.person.userId },
|
||||
vehicle: { carName: "sedan", carModel: "sedan", carType: "sedan" },
|
||||
damage: {
|
||||
selectedParts: [
|
||||
{ id: 11, name: "bumper", side: "front", label_fa: "سپر جلو" },
|
||||
],
|
||||
},
|
||||
inquiries: {
|
||||
thirdParty: {
|
||||
data: {
|
||||
FIRST: { mapped: first.vehicle.inquiry.mapped },
|
||||
SECOND: { mapped: second.vehicle.inquiry.mapped },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const service: any = new (ExpertClaimService as any)(
|
||||
...new Array(21).fill(undefined),
|
||||
);
|
||||
const panel = service.vehicleForExpertFromClaimAndBlameMap(
|
||||
claim,
|
||||
new Map([["blame", blame]]),
|
||||
);
|
||||
const ai = buildEstimateContext(claim, blame, {}).vehicle;
|
||||
const target = resolveClaimVehicleSources(claim, blame).identity;
|
||||
expect(panel.carName).toBe(target.name);
|
||||
expect(panel.carModel ?? null).toBe(target.model ?? null);
|
||||
expect(ai.make).toBe(panel.carName);
|
||||
expect(ai.model).toBe(panel.carModel ?? null);
|
||||
expect(panel.carName).not.toBe("sedan");
|
||||
expect(ai.make).toBe(damagedRole === "FIRST" ? "پژو" : "فائو ( FAW )");
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { resolveClaimBeneficiary } from "./blame-claim-eligibility";
|
||||
import { resolveVehicleIdentity } from "./vehicle-identity";
|
||||
|
||||
/** Resolve the damaged car once for panel responses and AI requests alike. */
|
||||
export function resolveClaimVehicleSources(claim: any, blame: any) {
|
||||
const beneficiary = blame ? resolveClaimBeneficiary(blame) : null;
|
||||
const beneficiaryId =
|
||||
beneficiary?.person?.userId ?? claim?.damagedPartyUserId;
|
||||
const parties = blame?.parties?.length
|
||||
? blame.parties
|
||||
: claim?.snapshot?.parties;
|
||||
const party =
|
||||
beneficiary ??
|
||||
parties?.find(
|
||||
(p: any) =>
|
||||
beneficiaryId && String(p.person?.userId) === String(beneficiaryId),
|
||||
);
|
||||
const isBeneficiaryOwner =
|
||||
beneficiaryId && String(claim?.owner?.userId) === String(beneficiaryId);
|
||||
const standaloneOwner =
|
||||
!claim?.blameRequestId &&
|
||||
!blame &&
|
||||
!claim?.snapshot?.parties?.length &&
|
||||
!beneficiaryId &&
|
||||
!!claim?.owner?.userId;
|
||||
const snapshot =
|
||||
isBeneficiaryOwner || standaloneOwner ? (claim?.vehicle ?? {}) : {};
|
||||
const role =
|
||||
party?.role ?? (isBeneficiaryOwner ? claim?.owner?.userRole : undefined);
|
||||
const bodyPolicy = role
|
||||
? claim?.inquiries?.carBody?.data?.[role]?.mapped
|
||||
: undefined;
|
||||
const thirdPolicy = role
|
||||
? claim?.inquiries?.thirdParty?.data?.[role]?.mapped
|
||||
: undefined;
|
||||
const records =
|
||||
blame?.type === "CAR_BODY"
|
||||
? [bodyPolicy, thirdPolicy]
|
||||
: [thirdPolicy, bodyPolicy];
|
||||
// Retain saved claim details only when that snapshot's owner is verified above.
|
||||
const vehicle = { ...snapshot, ...(party?.vehicle ?? {}) };
|
||||
const inquiry = vehicle.inquiry?.mapped ?? vehicle.inquiry ?? {};
|
||||
const policy = records.find(Boolean) ?? {};
|
||||
const review = claim?.fanavaranVehicleReview;
|
||||
const identity = resolveVehicleIdentity(vehicle, records, review);
|
||||
return {
|
||||
party,
|
||||
vehicle,
|
||||
inquiry,
|
||||
policy,
|
||||
snapshot,
|
||||
review: review?.approved === true ? review : {},
|
||||
identity,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/** Provider vehicle identity only. Body class and manufacturing year are separate fields. */
|
||||
export function vehicleIdentityText(value: unknown): string | undefined {
|
||||
if (typeof value !== "string" || !value.trim()) return undefined;
|
||||
const text = value.trim();
|
||||
if (
|
||||
/^(sedan|hatchback|suv|crossover|coupe|convertible|pickup|van|truck|bus|motorcycle|station[_ -]?wagon|unknown|undefined|null|نامشخص|ثبت نشده|-)$/i.test(
|
||||
text,
|
||||
)
|
||||
)
|
||||
return undefined;
|
||||
return text;
|
||||
}
|
||||
|
||||
const key = (value: string) =>
|
||||
value
|
||||
.toLowerCase()
|
||||
.replace(/ي/g, "ی")
|
||||
.replace(/ك/g, "ک")
|
||||
.replace(/[\s()_-]+/g, "");
|
||||
const compatible = (a: string, b: string) =>
|
||||
key(a).includes(key(b)) || key(b).includes(key(a));
|
||||
const pick = (...values: unknown[]) =>
|
||||
values.map(vehicleIdentityText).find(Boolean);
|
||||
|
||||
export function resolveVehicleIdentity(
|
||||
vehicle: any = {},
|
||||
records: any[] = [],
|
||||
review?: any,
|
||||
) {
|
||||
const rows = [
|
||||
vehicle?.inquiry?.mapped,
|
||||
vehicle?.inquiry?.carBody?.mapped,
|
||||
...records,
|
||||
].filter(Boolean);
|
||||
const row =
|
||||
rows.find((r) =>
|
||||
pick(
|
||||
r.vehicleSystemTitle,
|
||||
r.MapTypNam,
|
||||
r.vehiclePersianName,
|
||||
r.SystemField,
|
||||
r.CarName,
|
||||
r.TypeNameCii,
|
||||
),
|
||||
) ?? {};
|
||||
const legacyName = pick(
|
||||
row.MapTypNam,
|
||||
row.vehiclePersianName,
|
||||
row.CarName,
|
||||
row.TypeNameCii,
|
||||
);
|
||||
const systemName = pick(row.vehicleSystemTitle, row.SystemField);
|
||||
const conflictingNames = !!(
|
||||
legacyName &&
|
||||
systemName &&
|
||||
!row.vehicleSystemTitle &&
|
||||
!compatible(legacyName, systemName)
|
||||
);
|
||||
const storedName = pick(vehicle?.name, vehicle?.carName);
|
||||
const name = pick(row.vehicleSystemTitle, legacyName, systemName, storedName);
|
||||
const storedModel =
|
||||
storedName && name && compatible(storedName, name)
|
||||
? pick(vehicle?.model, vehicle?.carModel)
|
||||
: !name
|
||||
? pick(vehicle?.model, vehicle?.carModel)
|
||||
: undefined;
|
||||
const model = conflictingNames
|
||||
? undefined
|
||||
: pick(storedModel, row.vehicleKind, row.TypeField);
|
||||
const approved = review?.approved === true;
|
||||
const reviewedName = approved
|
||||
? pick(review.vehicleSystem?.caption)
|
||||
: undefined;
|
||||
const reviewedModel = approved
|
||||
? pick(review.vehicleKind?.caption)
|
||||
: undefined;
|
||||
return {
|
||||
name: reviewedName ?? name,
|
||||
model:
|
||||
approved && (reviewedName || reviewedModel)
|
||||
? (reviewedModel ??
|
||||
(reviewedName && name && compatible(reviewedName, name)
|
||||
? model
|
||||
: undefined))
|
||||
: model,
|
||||
type:
|
||||
typeof (vehicle?.type ?? vehicle?.carType) === "string"
|
||||
? (vehicle.type ?? vehicle.carType)
|
||||
: undefined,
|
||||
conflictingNames: conflictingNames && !(reviewedName && reviewedModel),
|
||||
};
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
@@ -62,6 +62,8 @@ describe("SandHubService inquiry mocks", () => {
|
||||
});
|
||||
|
||||
expect(isMappedPolicyCurrent(result.mapped)).toBe(true);
|
||||
expect(result.mapped.MapTypNam).toBe(result.mapped.SystemField);
|
||||
expect(result.mapped.TypeField).toBeNull();
|
||||
});
|
||||
|
||||
it("returns car-body mock without HTTP when carBodyPlate is off", async () => {
|
||||
@@ -78,6 +80,8 @@ describe("SandHubService inquiry mocks", () => {
|
||||
expect(result.mapped.policyNumber).toBe("1405/1143-70591/220/1/0");
|
||||
expect(result.mapped.CompanyName).toBe("بیمه پارسیان");
|
||||
expect(result.mapped.companyId).toBe(8);
|
||||
expect(result.mapped.MapTypNam).toBe(result.raw.data.vehicleSystemTitle);
|
||||
expect(result.mapped.MapTypNam).not.toBe(result.raw.data.vehicleGroupTitle);
|
||||
expect(result.mapped.insurerNationalCode).toBe("1234567890");
|
||||
expect(result.mapped.platePartOne).toBe(16);
|
||||
expect(result.mapped.platePartThree).toBe(498);
|
||||
|
||||
@@ -211,8 +211,9 @@ export class SandHubService {
|
||||
PrintEndorsCompanyDocumentNumber: "",
|
||||
EndorseDate: null,
|
||||
InsuranceFullName: null,
|
||||
SystemField: "سايپا",
|
||||
TypeField: "111SE",
|
||||
// Keep the mock's vehicle aliases consistent; no FAW trim is known here.
|
||||
SystemField: "فائو ( FAW )",
|
||||
TypeField: null,
|
||||
UsageField: "سواري",
|
||||
MainColorField: "سفيد",
|
||||
SecondColorField: "سفيد شيري",
|
||||
@@ -1152,7 +1153,7 @@ export class SandHubService {
|
||||
VinNumberField: data.vin ?? data.VinNumberField ?? null,
|
||||
vehicleGroupTitle: data.vehicleGroupTitle ?? null,
|
||||
vehicleSystemTitle: data.vehicleSystemTitle ?? null,
|
||||
MapTypNam: data.vehicleGroupTitle ?? data.vehicleSystemTitle ?? null,
|
||||
MapTypNam: data.vehicleSystemTitle ?? null,
|
||||
|
||||
// Policy dates (Jalali strings from the API)
|
||||
IssueDate: data.issueDate ?? data.persianStartDate ?? null,
|
||||
|
||||
Reference in New Issue
Block a user