Added extra fanavaran fields

This commit is contained in:
SepehrYahyaee
2026-10-04 12:01:45 +03:30
parent 91074ca1e5
commit 268258a23c
13 changed files with 1216 additions and 38 deletions
@@ -58,6 +58,11 @@ import {
fanavaranHullNestedClaimId,
toFanavaranHullBaseClaimPayload,
} from "./fanavaran-hull-base-claim";
import {
activeStageLookupRows, applyFanavaranStageFieldReview,
fanavaranFieldStageSent, fanavaranStageFields,
type FanavaranFieldValues,
} from "./fanavaran-stage-fields";
import {
collectFanavaranExpertiseReadinessWarnings,
hullExpertiseAssertFields,
@@ -4395,7 +4400,8 @@ export class ClaimRequestManagementService {
vehicleCurrentValue = vehicleCurrentValueFromHullPolicyRecord(policy);
}
const repairDuration = FANAVARAN_DEFAULT_HULL_REPAIR_DURATION;
vehicleCurrentValue = input.claimCase?.fanavaranFieldReview?.values?.VehicleCurrentValue ?? vehicleCurrentValue;
const repairDuration = input.claimCase?.fanavaranFieldReview?.values?.RepairDuration ?? FANAVARAN_DEFAULT_HULL_REPAIR_DURATION;
return {
replyKey: input.replyKey,
@@ -4757,7 +4763,7 @@ export class ClaimRequestManagementService {
"shsNam",
]) ??
(typeof apiVehicle?.ChassisNo === "string" ? apiVehicle.ChassisNo : null),
DmgHistoryStatus: input.defaults.DmgHistoryStatus,
DmgHistoryStatus: input.claimCase?.fanavaranFieldReview?.values?.DmgHistoryStatus ?? input.defaults.DmgHistoryStatus,
Desc: this.formatFanavaranSelectedPartsDesc(input.selectedParts),
DmgCaseTypeId: input.defaults.DmgCaseTypeId,
DriverId: driverFanavaranId ?? null,
@@ -6189,12 +6195,17 @@ export class ClaimRequestManagementService {
logPrefix,
claimCaseId,
});
applyFanavaranStageFieldReview(payload, claimCase, "car-body", "baseClaim");
delete payload.IsLicenseReplacement;
const hull = toFanavaranHullBaseClaimPayload(payload);
for (const key of Object.keys(payload)) delete payload[key];
Object.assign(payload, hull);
}
if (fanavaranClaimProductFromBlameType(blameCase.type) === "third-party") {
applyFanavaranStageFieldReview(payload, claimCase, "third-party", "baseClaim");
}
const fileRoleExpertIds =
await this.resolveFanavaranFileRoleExpertIdsForBlame(blameCase);
if (fileRoleExpertIds) {
@@ -6983,6 +6994,93 @@ export class ClaimRequestManagementService {
return definition;
}
/** Read-only panel context. Does not create a remote claim or persist a preview. */
async getFanavaranStageFieldsContext(claim: any, blame: any) {
if (!blame || !isFanavaranSubmitSupportedBlameType(blame.type)) {
throw new BadRequestException(
"Fanavaran field review only applies to third-party and car-body claims.",
);
}
const product = fanavaranClaimProductFromBlameType(blame.type);
const clientKey = resolveFanavaranClientKey();
const defaults = getFanavaranClientProfile(clientKey).defaults;
const definitions = fanavaranStageFields(product);
const options = new Map<string, ReturnType<typeof activeStageLookupRows>>();
await Promise.all(
definitions
.filter((field) => field.lookup)
.map(async (field) => {
options.set(
field.lookup,
activeStageLookupRows(
await this.getFanavaranLookupRows(clientKey, field.lookup),
),
);
}),
);
const initial: FanavaranFieldValues = {
AccidentLocationAddress: FANAVARAN_ACCIDENT_LOCATION_ADDRESS,
AccidentCityId: defaults.AccidentCityId,
AccidentReportTypeId: defaults.AccidentReportTypeId,
DmgHistoryStatus: defaults.DmgHistoryStatus,
InspectionPlaceId: 282,
AccidentTypeId: 2,
CulpritTypeId: 300,
AuthorityCulpritId: authorityCulpritIdFromDetectionRows(
options.get("detection-accident-culprits") ?? [],
),
CustomerFaultPercent: FANAVARAN_DEFAULT_HULL_CUSTOMER_FAULT_PERCENT,
RepairDuration: FANAVARAN_DEFAULT_HULL_REPAIR_DURATION,
};
if (product === "car-body") {
const price =
this.parseFanavaranMoney(claim?.vehicle?.carPrice) ||
this.parseFanavaranMoney(claim?.vehicle?.price) ||
this.parseFanavaranMoney(claim?.evaluation?.priceDrop?.carPrice) ||
(await this.resolveHullVehicleCurrentValueFromPolicy(clientKey, claim));
if (price > 0) initial.VehicleCurrentValue = price;
}
return {
product,
version: claim.fanavaranFieldReview?.version ?? 0,
reviewedAt: claim.fanavaranFieldReview?.reviewedAt,
reviewedByActorName: claim.fanavaranFieldReview?.reviewedByActorName,
fields: definitions.map((field) => {
const sent = fanavaranFieldStageSent(claim, field.stage);
const sentValue = sent
? claim.fanavaranSync?.[field.stage]?.lastPayload?.[field.key]
: undefined;
const reviewedValue = claim.fanavaranFieldReview?.values?.[field.key];
const value = sentValue ?? reviewedValue ?? initial[field.key] ?? null;
const rows = field.lookup
? (options.get(field.lookup) ?? [])
: undefined;
return {
...field,
value,
source:
sentValue != null
? "submitted"
: reviewedValue != null
? "reviewed"
: "default",
editable: !sent,
...(field.lookup
? {
options: rows,
lookupUnavailable: !rows.length,
caption:
rows.find((row) => row.Id === value)?.Caption ??
(reviewedValue === value
? claim.fanavaranFieldReview?.captions?.[field.key]
: undefined),
}
: {}),
};
}),
};
}
private async getFanavaranLookupRows(
clientKey: FanavaranClientKey,
name: string,
@@ -6994,7 +7092,11 @@ export class ClaimRequestManagementService {
definition.url,
definition.cacheFile,
);
return Array.isArray(data) ? data : [];
let rows: unknown = data;
for (let level = 0; level < 3 && rows && !Array.isArray(rows) && typeof rows === "object"; level++) {
rows = (rows as { data?: unknown }).data;
}
return Array.isArray(rows) ? rows : [];
} catch {
this.logger.warn(
`Lookup "${name}" unavailable for ${clientKey}; returning empty.`,
@@ -7239,7 +7341,7 @@ export class ClaimRequestManagementService {
const priceDrop = input.claimCase.evaluation?.priceDrop ?? {};
const priceDropTotal = this.parseFanavaranMoney(priceDrop?.total);
const inspectionPlaceId = 282;
const inspectionPlaceId = input.claimCase?.fanavaranFieldReview?.values?.InspectionPlaceId ?? 282;
const dropAmountStatus = 5458;
const dmgSections = parts.map((part) =>
@@ -33,6 +33,7 @@ import {
ClaimWorkflowSchema,
} from "./claim-case.workflow.schema";
import { UserClaimRating } from "./claim-request-management.schema";
import type { FanavaranFieldReview } from "../../fanavaran-stage-fields";
import {
CaseInquiries,
CaseInquiriesSchema,
@@ -342,6 +343,10 @@ export class ClaimCase {
@Prop({ type: FanavaranVehicleReviewSchema })
fanavaranVehicleReview?: FanavaranVehicleReview;
/** Validated expert edits; separate stages lock after their remote creation. */
@Prop({ type: MongooseSchema.Types.Mixed })
fanavaranFieldReview?: FanavaranFieldReview;
@Prop({ type: ClaimDamageSelectionSchema, default: () => ({}) })
damage?: ClaimDamageSelection;
@@ -0,0 +1,197 @@
import type { FanavaranClaimProduct } from "./fanavaran-claim-product";
export type FanavaranFieldStage = "baseClaim" | "damageCase" | "expertise";
export type FanavaranFieldValues = Partial<
Record<FanavaranFieldKey, string | number>
>;
export type FanavaranFieldKey =
| "AccidentLocationAddress"
| "AccidentCityId"
| "AccidentReportTypeId"
| "DmgHistoryStatus"
| "InspectionPlaceId"
| "AccidentTypeId"
| "CulpritTypeId"
| "AuthorityCulpritId"
| "CustomerFaultPercent"
| "RepairDuration"
| "VehicleCurrentValue";
export interface FanavaranFieldReview {
values: FanavaranFieldValues;
captions: Partial<Record<FanavaranFieldKey, string>>;
version: number;
reviewedAt: Date;
reviewedByActorId: string;
reviewedByActorName?: string;
reviewedByRole: string;
}
export interface FanavaranStageFieldDefinition {
key: FanavaranFieldKey;
label: string;
stage: FanavaranFieldStage;
type: "text" | "lookup" | "number";
lookup?: string;
min?: number;
max?: number;
unit?: string;
optional?: boolean;
}
const common: FanavaranStageFieldDefinition[] = [
{
key: "AccidentLocationAddress",
label: "نشانی محل حادثه",
stage: "baseClaim",
type: "text",
},
{
key: "AccidentCityId",
label: "شهر محل حادثه",
stage: "baseClaim",
type: "lookup",
lookup: "cities",
},
{
key: "AccidentReportTypeId",
label: "نحوه اعلام حادثه",
stage: "baseClaim",
type: "lookup",
lookup: "accident-report-type",
},
];
export function fanavaranStageFields(
product: FanavaranClaimProduct,
): FanavaranStageFieldDefinition[] {
return product === "car-body"
? [
...common,
{
key: "AccidentTypeId",
label: "نوع حادثه بدنه",
stage: "baseClaim",
type: "lookup",
lookup: "vehicle-hull-accident-types",
},
{
key: "CulpritTypeId",
label: "نوع مقصر بدنه",
stage: "baseClaim",
type: "lookup",
lookup: "vehicle-hull-accident-culprit-type",
},
{
key: "AuthorityCulpritId",
label: "مرجع تعیین مقصر",
stage: "baseClaim",
type: "lookup",
lookup: "detection-accident-culprits",
},
{
key: "CustomerFaultPercent",
label: "درصد تقصیر بیمه‌گذار",
stage: "baseClaim",
type: "number",
min: 0,
max: 100,
unit: "درصد",
},
{
key: "RepairDuration",
label: "مدت تعمیر",
stage: "expertise",
type: "number",
min: 1,
unit: "روز",
},
{
key: "VehicleCurrentValue",
label: "ارزش روز خودرو",
stage: "expertise",
type: "number",
min: 1,
unit: "ریال",
optional: true,
},
]
: [
...common,
{
key: "DmgHistoryStatus",
label: "وضعیت سابقه خسارت",
stage: "damageCase",
type: "lookup",
lookup: "dmg-history-status",
},
{
key: "InspectionPlaceId",
label: "محل بازدید",
stage: "expertise",
type: "lookup",
lookup: "inspection-place",
},
];
}
export function fanavaranFieldStageSent(
claim: any,
stage: FanavaranFieldStage,
): boolean {
const id = {
baseClaim: "claimId",
damageCase: "dmgCaseId",
expertise: "expertiseId",
}[stage];
return (
claim?.[id] != null ||
claim?.fanavaranSync?.[stage]?.[id] != null ||
claim?.fanavaranSync?.[stage]?.status === "success" ||
(stage === "baseClaim" && claim?.claimNo != null)
);
}
/** Strict codebook IDs, never provider legacy codes or guessed captions. */
export function activeStageLookupRows(
raw: unknown,
): Array<{ Id: number; Caption: string }> {
let value = raw;
for (
let level = 0;
level < 3 && value && !Array.isArray(value) && typeof value === "object";
level++
) {
value = (value as { data?: unknown }).data;
}
if (!Array.isArray(value)) return [];
return value
.filter(
(row) =>
row &&
typeof row === "object" &&
row.IsActive !== false &&
Number(row.IsActive) !== 0 &&
Number.isSafeInteger(Number(row.Id)) &&
Number(row.Id) > 0 &&
typeof row.Caption === "string" &&
row.Caption.trim(),
)
.map((row) => ({ Id: Number(row.Id), Caption: row.Caption.trim() }))
.sort((a, b) => a.Caption.localeCompare(b.Caption, "fa"));
}
/** Apply only applicable, reviewed fields at their own outbound stage. */
export function applyFanavaranStageFieldReview(
payload: Record<string, unknown>,
claim: any,
product: FanavaranClaimProduct,
stage: FanavaranFieldStage,
): void {
const values = claim?.fanavaranFieldReview?.values;
if (!values) return;
for (const field of fanavaranStageFields(product)) {
const value = values[field.key];
if (field.stage === stage && value != null) payload[field.key] = value;
}
}