fix(fanavaran): honor supplied submission payload fields

Apply every supplied JSON field over generated defaults, preserving IDs, nested values and explicit nulls across base, damage and expertise submits. Validate manual hull payloads without rewriting their values or premium, and derive an empty replacement flag for old hull licences.
This commit is contained in:
2026-11-21 13:33:41 +03:30
parent 83d7351310
commit 99e6fa0850
10 changed files with 334 additions and 90 deletions
@@ -198,6 +198,7 @@ import {
FANAVARAN_DEFAULT_HULL_CUSTOMER_FAULT_PERCENT,
fanavaranHullNestedClaimId,
toFanavaranHullBaseClaimPayload,
resolveFanavaranHullIsLicenseReplaced,
} from './fanavaran-hull-base-claim';
import { toTejaratV15ThirdPartyBasePayload } from './fanavaran-v15-third-party-base';
import {
@@ -268,9 +269,6 @@ import {
asVehicleInquiryRows,
collectPartyVinCandidates,
completePlaqueParts,
FANAVARAN_BASE_MANUAL_OVERRIDE_KEYS,
FANAVARAN_DAMAGE_MANUAL_OVERRIDE_KEYS,
FANAVARAN_EXPERTISE_MANUAL_OVERRIDE_KEYS,
fanavaranVehicleMatchesDamagedIdentity,
normalizeVin,
parseFanavaranId,
@@ -2739,7 +2737,7 @@ export class ClaimRequestManagementService {
);
input.payload.CulpritLicenceIssuDate =
resolveFanavaranLicenceIssuDate(driverIdentity);
const licenseReplaced = resolveFanavaranIsLicenseReplaced(
const licenseReplaced = resolveFanavaranHullIsLicenseReplaced(
input.payload.CulpritLicenceIssuDate,
input.payload.AccidentDate,
);
@@ -4642,6 +4640,8 @@ export class ClaimRequestManagementService {
requirePolicyId?: boolean;
/** Only submission may register the hull driver through GEN.44. */
registerMissingHullDriver?: boolean;
/** Internal manual-submit default source; the final body retains its exact value. */
policyIdOverride?: unknown;
/**
* When false (HTTP preview), missing staff ClaimExpertId stays null and is
* reported in previewWarnings instead of throwing.
@@ -4792,6 +4792,11 @@ export class ClaimRequestManagementService {
logPrefix,
requirePolicyId,
resolvePolicyId: async () => {
if (
Object.prototype.hasOwnProperty.call(options ?? {}, 'policyIdOverride')
) {
return options.policyIdOverride as number;
}
const product = fanavaranClaimProductFromBlameType(blameCase.type);
if (cachedPolicyId != null && !forceRefreshPolicy) {
@@ -6086,8 +6091,10 @@ export class ClaimRequestManagementService {
payload: Record<string, unknown>,
clientKey: FanavaranClientKey,
claimCaseId?: string,
preservePayload = false,
): Promise<void> {
if (!/\/car\/vehicle-hull-claims(?:\/|$)/.test(url)) return;
if (preservePayload) payload = JSON.parse(JSON.stringify(payload));
const stage: HullLookupStage = url.endsWith('/expertise')
? 'expertise'
: url.endsWith('/files') ? 'files' : 'baseClaim';
@@ -6108,7 +6115,7 @@ export class ClaimRequestManagementService {
);
},
);
if (stage === 'baseClaim') {
if (stage === 'baseClaim' && !preservePayload) {
const policyId = parseFanavaranId(payload.PolicyId);
try {
const policy = policyId == null ? null : asObjectRecord(
@@ -6543,6 +6550,8 @@ export class ClaimRequestManagementService {
bodyOverride?: Record<string, unknown>,
): Promise<any> {
try {
const manualSubmission =
bodyOverride != null && Object.keys(bodyOverride).length > 0;
await this.completeFanavaranExpertisePrerequisites(
claimCaseId,
clientKey,
@@ -6557,20 +6566,23 @@ export class ClaimRequestManagementService {
const built = await this.buildFanavaranExpertisePayload({
claimCase,
clientKey,
requireClaimExpertId: !manualSubmission,
});
const payload = applyFanavaranManualPayloadOverrides(
built.payload,
bodyOverride,
FANAVARAN_EXPERTISE_MANUAL_OVERRIDE_KEYS,
);
this.assertFanavaranExpertisePayloadReady(payload, built.warnings, {
...hullExpertiseAssertFields(product),
});
this.assertFanavaranExpertisePayloadReady(
payload,
manualSubmission ? [] : built.warnings,
{ ...hullExpertiseAssertFields(product) },
);
const result = await this.executeFanavaranExpertiseSubmit(
claimCaseId,
clientKey,
payload,
FanavaranAuditSource.SUBMIT,
manualSubmission,
);
// Surface infoWarnings (e.g. factory-bundle fallbacks) on the success
// response so callers are informed, without ever blocking the submit.
@@ -6593,6 +6605,7 @@ export class ClaimRequestManagementService {
clientKey: FanavaranClientKey,
payload: Record<string, unknown>,
auditSource: FanavaranAuditSource,
preservePayload = false,
): Promise<any> {
let claimCase = await this.claimCaseDbService.findById(claimCaseId);
if (!claimCase) throw new NotFoundException('Claim case not found');
@@ -6659,6 +6672,8 @@ export class ClaimRequestManagementService {
payload,
clientKey,
auditSession,
claimCaseId,
preservePayload,
);
const expertiseId = response.data?.Id;
const exchange =
@@ -7024,9 +7039,16 @@ export class ClaimRequestManagementService {
clientKey: FanavaranClientKey,
auditSession?: FanavaranAuditSession,
claimCaseId?: string,
preservePayload = false,
) {
this.fanavaranAuthService.assertNotInBackoff(clientKey);
await this.assertFanavaranHullApiValues(url, payload, clientKey, claimCaseId ?? auditSession?.claimCaseId);
await this.assertFanavaranHullApiValues(
url,
payload,
clientKey,
claimCaseId ?? auditSession?.claimCaseId,
preservePayload,
);
const headers = await this.getFanavaranAuthHeaders(
clientKey,
auditSession,
@@ -7365,18 +7387,19 @@ export class ClaimRequestManagementService {
selectedParts: selectedParts ?? claimCase.damage?.selectedParts ?? [],
clientKey,
defaults: profile.defaults,
registerMissingPerson: true,
registerMissingPerson:
bodyOverride == null || Object.keys(bodyOverride).length === 0,
});
const payload = applyFanavaranManualPayloadOverrides(
builtPayload,
bodyOverride,
FANAVARAN_DAMAGE_MANUAL_OVERRIDE_KEYS,
);
// Guardrail: never send empty/null licence fields even if manual overrides arrive.
payload.LicenceNo = this.ensureFanavaranNonEmptyLicenceNo(
payload.LicenceNo,
);
if (!Object.prototype.hasOwnProperty.call(bodyOverride ?? {}, 'LicenceNo')) {
payload.LicenceNo = this.ensureFanavaranNonEmptyLicenceNo(
payload.LicenceNo,
);
}
this.logger.log(
`[executeFanavaranDamageCaseSubmit] claimCaseId=${claimCaseId} claimId=${claimCase.claimId} payload.DriverId=${payload.DriverId ?? 'NULL'} payload keys=${Object.keys(payload).join(', ')}`,
@@ -7405,6 +7428,8 @@ export class ClaimRequestManagementService {
payload,
clientKey,
auditSession,
claimCaseId,
bodyOverride != null && Object.keys(bodyOverride).length > 0,
);
const dmgCaseId = response.data?.Id;
const exchange =
@@ -8033,6 +8058,8 @@ export class ClaimRequestManagementService {
`${logPrefix} Starting submission for claimCaseId: ${claimCaseId}`,
);
const manualSubmission =
bodyOverride != null && Object.keys(bodyOverride).length > 0;
const existing = await this.claimCaseDbService.findById(claimCaseId);
if (existing?.claimId != null) {
this.logger.log(
@@ -8052,6 +8079,7 @@ export class ClaimRequestManagementService {
? await this.tejaratNotifierAllowedIds(clientKey)
: undefined;
if (
!manualSubmission &&
!hasReviewedTejaratNotifier(
existing.fanavaranFieldReview?.values,
clientKey,
@@ -8073,23 +8101,45 @@ export class ClaimRequestManagementService {
};
const built = await this.previewFanavaranSubmitV2(claimCaseId, clientKey, {
requirePolicyId: true,
registerMissingHullDriver: true,
requirePolicyId: !manualSubmission,
requireClaimExpertId: !manualSubmission,
registerMissingHullDriver: !Object.prototype.hasOwnProperty.call(
bodyOverride ?? {},
'AccidentCulpritId',
),
auditSession,
persistPayload: true,
forceRefreshPolicy: true,
forceRefreshUsedId: true,
...(Object.prototype.hasOwnProperty.call(bodyOverride ?? {}, 'PolicyId')
? { policyIdOverride: bodyOverride.PolicyId }
: {}),
});
const product = await this.resolveFanavaranClaimProduct(claimCaseId);
const generated =
built && typeof built === 'object' && 'payload' in built
? built.payload
: built;
const fanavaranData = applyFanavaranManualPayloadOverrides(
built,
generated,
bodyOverride,
FANAVARAN_BASE_MANUAL_OVERRIDE_KEYS,
);
// Guardrail: never send empty/null licence fields even if manual overrides arrive.
fanavaranData.CulpritLicenceNo = this.ensureFanavaranNonEmptyLicenceNo(
fanavaranData.CulpritLicenceNo,
);
if (
product === 'car-body' &&
!Object.prototype.hasOwnProperty.call(bodyOverride ?? {}, 'IsLicenseReplaced')
) {
fanavaranData.IsLicenseReplaced = resolveFanavaranHullIsLicenseReplaced(
fanavaranData.CulpritLicenceIssuDate,
fanavaranData.AccidentDate,
);
}
if (!Object.prototype.hasOwnProperty.call(bodyOverride ?? {}, 'CulpritLicenceNo')) {
fanavaranData.CulpritLicenceNo = this.ensureFanavaranNonEmptyLicenceNo(
fanavaranData.CulpritLicenceNo,
);
}
this.logger.log(
`${logPrefix} Mapped data prepared:`,
@@ -8116,7 +8166,13 @@ export class ClaimRequestManagementService {
claimCaseId,
);
const submitUrl = await this.fanavaranClaimsUrlForClaim(claimCaseId);
await this.assertFanavaranHullApiValues(submitUrl, fanavaranData, clientKey, claimCaseId);
await this.assertFanavaranHullApiValues(
submitUrl,
fanavaranData,
clientKey,
claimCaseId,
manualSubmission,
);
const requestHeaders = {
...headers,