forked from Yara724/api
Fix damaged vehicle identity across claim panels and price estimates
This commit is contained in:
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user