fix(fanavaran): preserve assessed part ids in hull expertise

Use each saved expert-reply partId as AccessoryKindId and preserve its label. Resolve policy coverage separately so factory-bundle and caption matches cannot overwrite the selected part identity. Add coverage for the six-part expert reply and document the mapping.
This commit is contained in:
2026-11-21 14:45:31 +03:30
parent 7bff702514
commit 61ef4faaf4
5 changed files with 89 additions and 31 deletions
+2 -2
View File
@@ -152,9 +152,9 @@ Not ثالث GEN.08: no `DmgCaseId`, `InspectionPlaceId`, `RepairWage` on the ro
| AccidentPercent | درصد حادثه | number | yes | null |
| TheftCases | موارد سرقت | array | empty `[]` | system |
| DmgSections[].Count | تعداد | number | **no** | **system** `1` |
| DmgSections[].Desc | شرح قطعه | string | **no** | **user** part label |
| DmgSections[].Desc | شرح قطعه | string | **no** | Saved expert-reply part label (`carPartDamage.label_fa`) |
| DmgSections[].WasteValue | داغی ردیف | number | **no** | **user** |
| DmgSections[].AccessoryKindId | نوع قطعه کاتالوگ | number | preferred | **user** `partId` · lookup `car-components` |
| DmgSections[].AccessoryKindId | نوع قطعه کاتالوگ | number | preferred | Saved `evaluation.damageExpertReply.parts[].partId`, passed through directly; policy matching and factory bundles must never replace it |
| DmgSections[].DmgKindId | نوع آسیب بدنه | number | **no** | Current `vehicle-hull-dmg-kind` API row caption تخریب; no numeric fallback |
| DmgSections[].VehicleHullAccessoryId | شناسه لوازم بدنه | number | preferred | Current policy `dmg-accessories`: exact `AccessoryKindId` or caption match; else the «کليه قطعات فابريک» factory-bundle row `Id`. Keep selected car-components `AccessoryKindId` + part `Desc` |
| DmgSections[].DmgSectionCosts[].Caption | عنوان هزینه | string | **no** | **user** label |
@@ -214,7 +214,6 @@ import {
import {
FANAVARAN_HULL_DMG_COST_KIND_CAPTION,
FANAVARAN_HULL_DMG_KIND_CAPTION,
findLookupCaptionById,
findLookupIdByCaption,
findHullFactoryBundleAccessory,
asHullDmgAccessoryRows,
@@ -2848,6 +2847,7 @@ export class ClaimRequestManagementService {
contractOptions,
)
: Promise.resolve(null),
// Validate saved part IDs against the catalogue without remapping them.
input.parts.length ? this.getFanavaranLookupRows(input.clientKey, 'car-components', true) : Promise.resolve([]),
]);
@@ -2936,29 +2936,19 @@ export class ClaimRequestManagementService {
const factoryBundle = findHullFactoryBundleAccessory(accessories);
const hullSections = input.parts.map((part: any, index: number) => {
const partKindId = parseFanavaranId(part?.partId);
// Prefer live car-components Caption so Desc matches the frontend catalogue.
const catalogueCaption = findLookupCaptionById(componentRows, partKindId);
const localLabel = this.fanavaranPartLabel(part);
const partLabel = catalogueCaption || localLabel;
// The saved expert reply already identifies the selected catalogue part.
const partLabel = this.fanavaranPartLabel(part);
const resolved = resolveHullDmgSectionAccessory(
accessories,
partKindId,
partLabel,
);
// No per-part policy row → cover under «کليه قطعات فابريک» Id (e.g. 3061139).
// Use the bundle's own AccessoryKindId (e.g. 112) so Fanavaran receives a
// coherent Id/AccessoryKindId pair from the policy row.
// Policy coverage is resolved separately from the assessed part identity.
let vehicleHullAccessoryId = resolved?.vehicleHullAccessoryId ?? null;
let accessoryKindId: number | string | null | undefined =
resolved?.accessoryKindId ?? part?.partId;
if (!resolved && factoryBundle) {
vehicleHullAccessoryId = factoryBundle.vehicleHullAccessoryId;
// Replace the unrecognised per-part AccessoryKindId with the bundle's own
// kind (e.g. 112 = «کليه قطعات فابريک»). Fanavaran requires Id and
// AccessoryKindId to come from the same policy dmg-accessories row.
accessoryKindId = factoryBundle.accessoryKindId ?? accessoryKindId;
input.infoWarnings.push(
`DmgSections[${index}] (${partLabel || part?.partId || 'part'}): AccessoryKindId=${part?.partId ?? '(missing)'} is a fabrique part (no separate policy row); mapped to factory bundle VehicleHullAccessoryId=${factoryBundle.vehicleHullAccessoryId} AccessoryKindId=${accessoryKindId} («کليه قطعات فابريک»).`,
`DmgSections[${index}] (${partLabel || part?.partId || 'part'}): AccessoryKindId=${part?.partId ?? '(missing)'} is a fabrique part (no separate policy row); mapped to factory bundle VehicleHullAccessoryId=${factoryBundle.vehicleHullAccessoryId}; preserving AccessoryKindId=${partKindId} («کليه قطعات فابريک»).`,
);
} else if (!resolved) {
input.warnings.push(
@@ -2966,7 +2956,7 @@ export class ClaimRequestManagementService {
);
}
return toFanavaranHullExpertiseDmgSection({
partId: accessoryKindId,
partId: partKindId,
desc: partLabel,
wasteValue: this.fanavaranDaghiWasteValue(part?.daghi),
amount:
@@ -10,7 +10,7 @@ function setup(accessories: unknown) {
"vehicle-hull-dmg-cost-kinds": [
{ Id: 8002, Caption: "مجموع لوازم", IsActive: 1 },
],
"car-components": [{ Id: 9 }, { Id: 13 }, { Id: 112 }],
"car-components": [9, 13, 112, 10, 48, 76, 29, 100, 37].map(Id => ({ Id })),
})[name] ?? [],
);
service.fanavaranLookupService = {
@@ -19,16 +19,19 @@ function setup(accessories: unknown) {
.fn()
.mockResolvedValue(accessories),
};
const build = (policyId = 123) =>
const build = (
policyId = 123,
parts: any[] = [
{ partId: 9, partName: "سپر جلو", price: 50000000, salary: 25000000 },
{ partId: 13, partName: "چراغ جلو چپ", price: 0, salary: 2000000 },
],
) =>
service.buildCarBodyFanavaranExpertisePayload({
clientKey: "parsian",
claimCase: { fanavaranSync: { baseClaim: { policyId } } },
blame: { parties: [] },
profile: {},
parts: [
{ partId: 9, partName: "سپر جلو", price: 50000000, salary: 25000000 },
{ partId: 13, partName: "چراغ جلو چپ", price: 0, salary: 2000000 },
],
parts,
claimExpertId: 4044,
submittedAt: new Date("2026-11-11T09:09:00Z"),
priceDropTotal: 0,
@@ -82,12 +85,14 @@ describe("hull expertise policy accessory mapping", () => {
payload.DmgSections.map((row: any) => row.VehicleHullAccessoryId),
).toEqual([3061139, 3061139]);
expect(payload.DmgSections.map((row: any) => row.AccessoryKindId)).toEqual([
112, 112,
9, 13,
]);
expect(warnings).toEqual([]);
expect(infoWarnings).toEqual(
expect.arrayContaining([
expect.stringContaining("mapped to factory bundle VehicleHullAccessoryId=3061139 AccessoryKindId=112"),
expect.stringContaining(
"mapped to factory bundle VehicleHullAccessoryId=3061139; preserving AccessoryKindId=9",
),
]),
);
});
@@ -157,7 +162,7 @@ describe("hull expertise policy accessory mapping", () => {
},
);
it("matches by part caption and keeps AccessoryKindId from that policy row", async () => {
it("resolves the policy reference by caption without replacing the evaluated partId", async () => {
const { build } = setup([
{
Id: 900010,
@@ -168,9 +173,71 @@ describe("hull expertise policy accessory mapping", () => {
const { payload, warnings } = await build();
expect(payload.DmgSections[0]).toMatchObject({
VehicleHullAccessoryId: 900010,
AccessoryKindId: 44,
AccessoryKindId: 9,
});
expect(payload.DmgSections[1].VehicleHullAccessoryId).toBeNull();
expect(warnings.some((w) => w.includes("چراغ جلو چپ"))).toBe(true);
});
it("preserves the six saved expert-reply part IDs under shared factory coverage", async () => {
const savedReplyParts = [
{
partId: 10,
carPartDamage: { id: 10, label_fa: "سپر عقب" },
typeOfDamage: "تعویض",
price: "150000000",
salary: "30000000",
daghi: { option: "ارزش لوازم بازیافتی", price: "20000000" },
},
{
partId: 48,
carPartDamage: { id: 48, label_fa: "درب صندوق عقب" },
typeOfDamage: "تعمیر",
salary: "80000000",
},
{
partId: 76,
carPartDamage: { id: 76, label_fa: "سيني عقب" },
typeOfDamage: "تعمیر",
salary: "70000000",
},
{
partId: 29,
carPartDamage: { id: 29, label_fa: "راهنماي عقب راست" },
typeOfDamage: "تعویض",
price: "95000000",
salary: "5000000",
},
{
partId: 100,
carPartDamage: { id: 100, label_fa: "سنسور دنده عقب" },
typeOfDamage: "تعویض",
price: "25000000",
salary: "5000000",
},
{
partId: 37,
carPartDamage: { id: 37, label_fa: "گلگير عقب سمت سرنشين" },
typeOfDamage: "تعمیر",
salary: "40000000",
},
];
const { build } = setup([
{ Id: 3092174, AccessoryKindId: 112, AccessoryDesc: "کليه قطعات فابريک" },
]);
const { payload, warnings } = await build(123, savedReplyParts);
expect(warnings).toEqual([]);
expect(payload.DmgSections.map((row: any) => row.AccessoryKindId)).toEqual([
10, 48, 76, 29, 100, 37,
]);
expect(
payload.DmgSections.map((row: any) => row.VehicleHullAccessoryId),
).toEqual(Array(6).fill(3092174));
expect(payload.DmgSections.map((row: any) => row.Desc)).toEqual(
savedReplyParts.map((part) => part.carPartDamage.label_fa),
);
expect(
payload.DmgSections.map((row: any) => row.DmgSectionCosts[0].Amount),
).toEqual([180000000, 80000000, 70000000, 100000000, 30000000, 40000000]);
expect(payload.DmgSections[0].WasteValue).toBe(20000000);
});
});
@@ -134,13 +134,14 @@ function normalizeAccessoryCaption(value: unknown): string {
export type ResolvedHullDmgAccessory = {
vehicleHullAccessoryId: number;
/** Must come from the same policy dmg-accessories row (Fanavaran filter rule). */
/** Policy-row metadata; does not replace the saved expert-reply partId. */
accessoryKindId: number;
};
/**
* GEN.06 accessory pair from `GET …/vehicle-hull-policies/{policyId}/dmg-accessories`.
* Only returns a coherent Id + AccessoryKindId from one policy row.
* Returns the policy coverage reference and that policy row's metadata.
* The caller preserves the evaluated partId as DmgSections[].AccessoryKindId.
* Exact/caption match only. Factory-bundle fallback for unmatched parts is applied
* by the expertise builder (VehicleHullAccessoryId from «کليه قطعات فابريک»).
*/
@@ -199,7 +199,7 @@ export function collectFanavaranExpertiseReadinessWarnings(
if (!thirdPartyLookups) {
// VehicleHullAccessoryId may be null: many hull policies only list the
// factory bundle (کليه قطعات فابريک), not every car-components part.
// Do not invent a factory Id with a mismatched AccessoryKindId.
// A shared coverage bundle must not replace the evaluated part kind.
if (row.AccessoryKindId == null || row.AccessoryKindId === "") {
warnings.push(
`DmgSections[${index}].AccessoryKindId is required from car-components (selected part).`,