import { TEJARAT_NOTIFIER_FIELDS } from "src/fanavaran/tejarat-notifier"; import type { FanavaranClaimProduct } from "./fanavaran-claim-product"; import type { FanavaranClientKey } from "src/core/config/fanavaran-client.config"; export type FanavaranFieldStage = "baseClaim" | "damageCase" | "expertise"; export type FanavaranFieldValues = Partial< Record >; export type FanavaranFieldKey = | "AccidentLocationAddress" | "AccidentCityId" | "AccidentReportTypeId" | "DmgHistoryStatus" | "InspectionPlaceId" | "AccidentTypeId" | "CulpritTypeId" | "AuthorityCulpritId" | "CustomerFaultPercent" | "RepairDuration" | "VehicleCurrentValue" | "NotifierTypeId" | "NotifierRelationId"; export interface FanavaranFieldReview { values: FanavaranFieldValues; captions: Partial>; 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; requiredBeforeSubmit?: 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, clientKey?: FanavaranClientKey, ): FanavaranStageFieldDefinition[] { const baseFields: FanavaranStageFieldDefinition[] = [ ...common, ...(clientKey === "tejaratno" ? [ { key: "NotifierTypeId" as const, label: "نوع اعلام‌کننده", stage: "baseClaim" as const, type: "lookup" as const, lookup: "notifier-type", requiredBeforeSubmit: true, }, { key: "NotifierRelationId" as const, label: "نسبت اعلام‌کننده", stage: "baseClaim" as const, type: "lookup" as const, lookup: "notifier-relation", requiredBeforeSubmit: true, }, ] : []), ]; return product === "car-body" ? [ ...baseFields, { 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, }, ] : [ ...baseFields, { 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, claim: any, product: FanavaranClaimProduct, stage: FanavaranFieldStage, clientKey?: FanavaranClientKey, ): void { const values = claim?.fanavaranFieldReview?.values; if (clientKey === "tejaratno" && stage === "baseClaim") { const nested = claim?.fanavaranSync?.baseClaim?.lastPayload ?.DmgNotifications?.[0]; for (const key of Object.keys(TEJARAT_NOTIFIER_FIELDS)) { const value = values?.[key] ?? claim?.fanavaranSync?.baseClaim?.lastPayload?.[key] ?? nested?.[key]; // Preserve legacy submitted claims: adding fields must not overwrite unknown remote data. if (value != null || !fanavaranFieldStageSent(claim, stage)) payload[key] = value ?? null; } } if (!values) return; for (const field of fanavaranStageFields(product, clientKey)) { const value = values[field.key]; if (field.stage === stage && value != null) payload[field.key] = value; } }