update the IsLicenseReplaced and for the accessories

This commit is contained in:
2026-11-11 17:20:02 +03:30
parent 54459877a2
commit 90bdc51715
13 changed files with 693 additions and 124 deletions
@@ -188,7 +188,8 @@ import {
actualPremiumFromHullPolicyRecord,
authorityCulpritIdFromDetectionRows,
customerIdFromHullPolicyRecord,
FANAVARAN_DEFAULT_HULL_AUTHORITY_CULPRIT_ID,
FANAVARAN_HULL_ACCIDENT_TYPE_CAPTION,
FANAVARAN_HULL_CULPRIT_TYPE_CAPTION,
FANAVARAN_DEFAULT_HULL_CUSTOMER_FAULT_PERCENT,
fanavaranHullNestedClaimId,
toFanavaranHullBaseClaimPayload,
@@ -205,13 +206,16 @@ import {
vehicleCurrentValueFromHullPolicyRecord,
} from './fanavaran-hull-expertise';
import {
FANAVARAN_DEFAULT_HULL_DMG_COST_KIND_ID,
FANAVARAN_DEFAULT_HULL_DMG_KIND_ID,
FANAVARAN_DEFAULT_HULL_VEHICLE_ACCESSORY_ID,
FANAVARAN_HULL_DMG_COST_KIND_CAPTION,
FANAVARAN_HULL_DMG_KIND_CAPTION,
findLookupIdByCaption,
asHullDmgAccessoryRows,
resolveVehicleHullAccessoryId,
} from './fanavaran-hull-expertise-lookups';
import {
validateFanavaranHullLookupIds,
type HullLookupStage,
} from './fanavaran-hull-lookup-validation';
import {
activeStageLookupRows,
applyFanavaranStageFieldReview,
@@ -2636,23 +2640,15 @@ export class ClaimRequestManagementService {
input.payload.CustomerFaultPercent =
FANAVARAN_DEFAULT_HULL_CUSTOMER_FAULT_PERCENT;
if (parseFanavaranId(input.payload.AuthorityCulpritId) == null) {
try {
const authorityRows = await this.getFanavaranLookupRows(
input.clientKey,
'detection-accident-culprits',
);
input.payload.AuthorityCulpritId =
authorityCulpritIdFromDetectionRows(authorityRows);
} catch (error) {
this.logger.warn(
`${input.logPrefix} detection-accident-culprits lookup failed; using default AuthorityCulpritId=${FANAVARAN_DEFAULT_HULL_AUTHORITY_CULPRIT_ID}`,
error,
);
input.payload.AuthorityCulpritId =
FANAVARAN_DEFAULT_HULL_AUTHORITY_CULPRIT_ID;
}
}
const [accidentTypes, culpritTypes, authorities] = await Promise.all([
this.getFanavaranLookupRows(input.clientKey, 'vehicle-hull-accident-types', true),
this.getFanavaranLookupRows(input.clientKey, 'vehicle-hull-accident-culprit-type', true),
this.getFanavaranLookupRows(input.clientKey, 'detection-accident-culprits', true),
]);
input.payload.AccidentTypeId ??= findLookupIdByCaption(accidentTypes, FANAVARAN_HULL_ACCIDENT_TYPE_CAPTION);
if (input.payload.CulpritTypeId == null || input.payload.CulpritTypeId === 337)
input.payload.CulpritTypeId = findLookupIdByCaption(culpritTypes, FANAVARAN_HULL_CULPRIT_TYPE_CAPTION);
input.payload.AuthorityCulpritId ??= authorityCulpritIdFromDetectionRows(authorities);
}
/** GEN.06: VehicleCurrentValue from hull policy when price-drop / frontend value is missing. */
@@ -2728,11 +2724,12 @@ export class ClaimRequestManagementService {
),
};
const [dmgKindRows, dmgCostKindRows, policyRecord] = await Promise.all([
this.getFanavaranLookupRows(input.clientKey, 'vehicle-hull-dmg-kind'),
const [dmgKindRows, dmgCostKindRows, policyRecord, componentRows] = await Promise.all([
this.getFanavaranLookupRows(input.clientKey, 'vehicle-hull-dmg-kind', true),
this.getFanavaranLookupRows(
input.clientKey,
'vehicle-hull-dmg-cost-kinds',
true,
),
policyId != null
? this.fanavaranLookupService.bodyPolicyById(
@@ -2741,16 +2738,34 @@ export class ClaimRequestManagementService {
contractOptions,
)
: Promise.resolve(null),
input.parts.length ? this.getFanavaranLookupRows(input.clientKey, 'car-components', true) : Promise.resolve([]),
]);
const dmgKindId =
findLookupIdByCaption(dmgKindRows, FANAVARAN_HULL_DMG_KIND_CAPTION) ??
FANAVARAN_DEFAULT_HULL_DMG_KIND_ID;
findLookupIdByCaption(dmgKindRows, FANAVARAN_HULL_DMG_KIND_CAPTION);
const dmgCostKindId =
findLookupIdByCaption(
dmgCostKindRows,
FANAVARAN_HULL_DMG_COST_KIND_CAPTION,
) ?? FANAVARAN_DEFAULT_HULL_DMG_COST_KIND_ID;
);
let accessories = asHullDmgAccessoryRows([]);
if (policyId == null && input.parts.length > 0) input.warnings.push('VehicleHullAccessoryId cannot be resolved: missing PolicyId.');
if (policyId != null && input.parts.length > 0) {
try {
accessories = asHullDmgAccessoryRows(
await this.fanavaranLookupService.vehicleHullDmgAccessoriesByPolicyId(
input.clientKey,
policyId,
contractOptions,
),
);
} catch (error) {
this.logger.warn(
`[buildCarBodyFanavaranExpertisePayload] policy ${policyId} dmg-accessories lookup failed: ${error instanceof Error ? error.message : error}`,
);
}
}
let vehicle = pickHullVehicleIdentity(input.blame);
let colorId: number | null = null;
@@ -2795,6 +2810,10 @@ export class ClaimRequestManagementService {
);
const hullSections = input.parts.map((part: any) => {
const vehicleHullAccessoryId = resolveVehicleHullAccessoryId(
accessories,
parseFanavaranId(part?.partId),
);
return toFanavaranHullExpertiseDmgSection({
partId: part?.partId,
desc: this.fanavaranPartLabel(part),
@@ -2804,7 +2823,7 @@ export class ClaimRequestManagementService {
this.parseFanavaranMoney(part?.salary),
dmgKindId,
dmgCostKindId,
vehicleHullAccessoryId: FANAVARAN_DEFAULT_HULL_VEHICLE_ACCESSORY_ID,
vehicleHullAccessoryId,
});
});
@@ -2824,23 +2843,27 @@ export class ClaimRequestManagementService {
input.claimCase?.fanavaranFieldReview?.values?.RepairDuration ??
FANAVARAN_DEFAULT_HULL_REPAIR_DURATION;
const payload = toFanavaranHullExpertisePayload({
claimExpertId: input.claimExpertId,
dmgAssessmentDate: this.convertToPersianDate(input.submittedAt),
inspectionTime: this.getTime24Hour(input.submittedAt),
wage, componentReplacementCost, wasteValue,
dropAmount: input.priceDropTotal || 0,
vehicleCurrentValue, vehicle, colorId, repairDuration,
dmgSections: hullSections,
});
const sources = {
'vehicle-hull-dmg-kind': dmgKindRows,
'vehicle-hull-dmg-cost-kinds': dmgCostKindRows,
'car-components': componentRows,
'policy-dmg-accessories': accessories,
};
input.warnings.push(...await validateFanavaranHullLookupIds(payload, 'expertise', async lookup => sources[lookup] ?? []));
return {
replyKey: input.replyKey,
warnings: input.warnings,
payload: toFanavaranHullExpertisePayload({
claimExpertId: input.claimExpertId,
dmgAssessmentDate: this.convertToPersianDate(input.submittedAt),
inspectionTime: this.getTime24Hour(input.submittedAt),
wage,
componentReplacementCost,
wasteValue,
dropAmount: input.priceDropTotal || 0,
vehicleCurrentValue,
vehicle,
colorId,
repairDuration,
dmgSections: hullSections,
}),
payload,
};
}
@@ -4775,6 +4798,18 @@ export class ClaimRequestManagementService {
const hull = toFanavaranHullBaseClaimPayload(payload, clientKey);
for (const key of Object.keys(payload)) delete payload[key];
Object.assign(payload, hull);
try {
await this.assertFanavaranHullApiValues(
`${getFanavaranLookupBaseUrl(clientKey)}/car/vehicle-hull-claims`,
payload, clientKey, claimCaseId,
);
} catch (error) {
if (requirePolicyId) throw error;
previewWarnings.push({
code: 'FANAVARAN_HULL_LOOKUP_INVALID', field: 'lookups',
message: error instanceof Error ? error.message : String(error),
});
}
}
if (
@@ -5655,8 +5690,8 @@ export class ClaimRequestManagementService {
AccidentReportTypeId: defaults.AccidentReportTypeId,
DmgHistoryStatus: defaults.DmgHistoryStatus,
InspectionPlaceId: 282,
AccidentTypeId: 2,
CulpritTypeId: 300,
AccidentTypeId: findLookupIdByCaption(options.get('vehicle-hull-accident-types') ?? [], FANAVARAN_HULL_ACCIDENT_TYPE_CAPTION),
CulpritTypeId: findLookupIdByCaption(options.get('vehicle-hull-accident-culprit-type') ?? [], FANAVARAN_HULL_CULPRIT_TYPE_CAPTION),
AuthorityCulpritId: authorityCulpritIdFromDetectionRows(
options.get('detection-accident-culprits') ?? [],
),
@@ -5715,11 +5750,65 @@ export class ClaimRequestManagementService {
};
}
private async assertFanavaranHullApiValues(
url: string,
payload: Record<string, unknown>,
clientKey: FanavaranClientKey,
claimCaseId?: string,
): Promise<void> {
if (!/\/car\/vehicle-hull-claims(?:\/|$)/.test(url)) return;
const stage: HullLookupStage = url.endsWith('/expertise')
? 'expertise'
: url.endsWith('/files') ? 'files' : 'baseClaim';
const warnings = await validateFanavaranHullLookupIds(
payload, stage, async (lookup) => {
if (lookup !== 'policy-dmg-accessories') {
return this.getFanavaranLookupRows(clientKey, lookup, true);
}
const claim = claimCaseId
? await this.claimCaseDbService.findById(claimCaseId)
: null;
const policyId = this.parseHullClaimPolicyId(claim);
if (policyId == null) return [];
return this.fanavaranLookupService.vehicleHullDmgAccessoriesByPolicyId(
clientKey, policyId, {
contractIdOverride: resolveFanavaranProductContractId(clientKey, 'car-body'),
},
);
},
);
if (stage === 'baseClaim') {
const policyId = parseFanavaranId(payload.PolicyId);
try {
const policy = policyId == null ? null : asObjectRecord(
await this.fanavaranLookupService.bodyPolicyById(clientKey, policyId, {
contractIdOverride: resolveFanavaranProductContractId(clientKey, 'car-body'),
}),
);
const premium = actualPremiumFromHullPolicyRecord(policy);
if (premium == null) warnings.push('ActualPremium could not be resolved from the current hull policy API response.');
else payload.ActualPremium = premium;
} catch {
warnings.push('Hull policy API is unavailable; policy values could not be verified.');
}
}
if (warnings.length) {
throw new BadRequestException({
code: 'FANAVARAN_HULL_LOOKUP_INVALID',
message: 'Hull submission requires valid values from the current Fanavaran API responses.',
warnings,
});
}
}
private async getFanavaranLookupRows(
clientKey: FanavaranClientKey,
name: string,
requireRemote = false,
): Promise<unknown[]> {
if (clientKey === 'tejaratno' && isTejaratNotifierLookup(name)) {
if (
!requireRemote && clientKey === 'tejaratno' && isTejaratNotifierLookup(name)
) {
const stored = await this.fanavaranLookupService.readStoredLookup(name);
let rows: unknown = stored;
for (
@@ -5733,11 +5822,15 @@ export class ClaimRequestManagementService {
}
const definition = this.fanavaranLookupDefinition(name);
try {
const data = await this.fanavaranLookupService.getRemoteLookup(
clientKey,
definition.url,
definition.cacheFile,
);
const data = requireRemote
? await this.fanavaranLookupService.fetchFromFanavaran(clientKey, definition.url, {
contractIdOverride: resolveFanavaranProductContractId(clientKey, 'car-body'),
})
: await this.fanavaranLookupService.getRemoteLookup(
clientKey,
definition.url,
definition.cacheFile,
);
let rows: unknown = data;
for (
let level = 0;
@@ -5890,13 +5983,26 @@ export class ClaimRequestManagementService {
warnings.push('No submit-ready expert pricing lines exist yet.');
}
const priceDrop = input.claimCase.evaluation?.priceDrop ?? {};
const priceDropTotal = this.parseFanavaranMoney(priceDrop?.total);
const submittedAt = reply?.submittedAt ?? new Date();
const product = await this.resolveFanavaranClaimProduct(
input.claimCase?._id ? String(input.claimCase._id) : undefined,
);
if (product === 'car-body') {
return this.buildCarBodyFanavaranExpertisePayload({
clientKey: input.clientKey, claimCase: input.claimCase,
blame: blameForExpertIds, profile, parts, claimExpertId,
submittedAt, priceDropTotal, priceDropCarPrice: priceDrop?.carPrice,
warnings, replyKey: active?.replyKey,
});
}
const [inspectionPlaces, dropAmountStatuses] = await Promise.all([
this.getFanavaranLookupRows(input.clientKey, 'inspection-place'),
this.getFanavaranLookupRows(input.clientKey, 'drop-amount-status'),
]);
const priceDrop = input.claimCase.evaluation?.priceDrop ?? {};
const priceDropTotal = this.parseFanavaranMoney(priceDrop?.total);
const inspectionPlaceId =
input.claimCase?.fanavaranFieldReview?.values?.InspectionPlaceId ?? 282;
const dropAmountStatus = 5458;
@@ -5920,28 +6026,6 @@ export class ClaimRequestManagementService {
(sum, section) => sum + this.parseFanavaranMoney(section.WasteValue),
0,
);
const submittedAt = reply?.submittedAt ?? new Date();
const product = await this.resolveFanavaranClaimProduct(
input.claimCase?._id ? String(input.claimCase._id) : undefined,
);
if (product === 'car-body') {
const blame = blameForExpertIds;
return this.buildCarBodyFanavaranExpertisePayload({
clientKey: input.clientKey,
claimCase: input.claimCase,
blame,
profile,
parts,
claimExpertId,
submittedAt,
priceDropTotal,
priceDropCarPrice: priceDrop?.carPrice,
warnings,
replyKey: active?.replyKey,
});
}
const payload: Record<string, unknown> = {
ClaimExpertId: claimExpertId,
ComponentReplacementCost: componentReplacementCost,
@@ -6510,6 +6594,7 @@ export class ClaimRequestManagementService {
claimCaseId?: string,
) {
this.fanavaranAuthService.assertNotInBackoff(clientKey);
await this.assertFanavaranHullApiValues(url, payload, clientKey, claimCaseId ?? auditSession?.claimCaseId);
const headers = await this.getFanavaranAuthHeaders(
clientKey,
auditSession,
@@ -6541,6 +6626,7 @@ export class ClaimRequestManagementService {
claimCaseId?: string,
) {
this.fanavaranAuthService.assertNotInBackoff(clientKey);
await this.assertFanavaranHullApiValues(url, payload, clientKey, claimCaseId ?? auditSession?.claimCaseId);
const headers = await this.getFanavaranAuthHeaders(
clientKey,
auditSession,
@@ -6618,6 +6704,7 @@ export class ClaimRequestManagementService {
claimCaseId?: string,
) {
this.fanavaranAuthService.assertNotInBackoff(clientKey);
await this.assertFanavaranHullApiValues(url, content, clientKey, claimCaseId ?? auditSession?.claimCaseId);
const headers = await this.getFanavaranAuthHeaders(
clientKey,
auditSession,
@@ -7507,6 +7594,7 @@ export class ClaimRequestManagementService {
claimCaseId,
);
const submitUrl = await this.fanavaranClaimsUrlForClaim(claimCaseId);
await this.assertFanavaranHullApiValues(submitUrl, fanavaranData, clientKey, claimCaseId);
const requestHeaders = {
...headers,