forked from Yara724/api
new person registration + car body matching accident date with endDate
This commit is contained in:
@@ -193,7 +193,6 @@ import { fanavaranAttachmentStatus } from './fanavaran-attachment-status';
|
||||
import {
|
||||
actualPremiumFromHullPolicyRecord,
|
||||
authorityCulpritIdFromDetectionRows,
|
||||
customerIdFromHullPolicyRecord,
|
||||
FANAVARAN_HULL_ACCIDENT_TYPE_CAPTION,
|
||||
FANAVARAN_HULL_CULPRIT_TYPE_CAPTION,
|
||||
FANAVARAN_DEFAULT_HULL_CUSTOMER_FAULT_PERCENT,
|
||||
@@ -295,6 +294,7 @@ import {
|
||||
pickPersonBirthday,
|
||||
pickPersonNationalCode,
|
||||
selectFanavaranDriverId,
|
||||
selectFanavaranOtherPersonId,
|
||||
selectFanavaranOwnerId,
|
||||
} from './fanavaran-driver-inquiry';
|
||||
import {
|
||||
@@ -2570,6 +2570,79 @@ export class ClaimRequestManagementService {
|
||||
}
|
||||
}
|
||||
|
||||
/** Resolve the actual hull driver as سایر; policyholder membership alone is insufficient. */
|
||||
private async resolveHullDriverFanavaranId(input: {
|
||||
clientKey: FanavaranClientKey;
|
||||
person: any;
|
||||
claimCaseId?: string;
|
||||
registerMissingDriver?: boolean;
|
||||
cachedRegistration?: {
|
||||
otherPersonId?: number;
|
||||
otherPersonNationalCode?: string;
|
||||
otherPersonBirthday?: string;
|
||||
};
|
||||
}): Promise<number | null> {
|
||||
const nationalCode = pickPersonNationalCode(input.person);
|
||||
const birthday = pickPersonBirthday(input.person);
|
||||
const parsed = parseJalaliDateParts(birthday);
|
||||
if (!nationalCode || !parsed) return null;
|
||||
const birthdayKey = `${parsed.year}/${parsed.month}/${parsed.day}`;
|
||||
const inquire = () =>
|
||||
this.fanavaranLookupService.inquiryByUniqueIdentifier(input.clientKey, {
|
||||
nationalCode,
|
||||
birthYear: parsed.year,
|
||||
birthMonth: parsed.month,
|
||||
birthDay: parsed.day,
|
||||
});
|
||||
// Failed inquiry is not evidence that a role is missing: never create blindly.
|
||||
let rows: unknown;
|
||||
try {
|
||||
rows = await inquire();
|
||||
} catch (error) {
|
||||
this.logger.warn(
|
||||
`[hull driver] Person inquiry unavailable: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
const existingId = selectFanavaranOtherPersonId(rows);
|
||||
if (existingId != null) return existingId;
|
||||
if (!input.registerMissingDriver) return null;
|
||||
|
||||
const cached = input.cachedRegistration;
|
||||
if (
|
||||
cached?.otherPersonId &&
|
||||
cached.otherPersonNationalCode === nationalCode &&
|
||||
cached.otherPersonBirthday === birthdayKey
|
||||
) {
|
||||
// GEN.44 already succeeded. Wait for the role to become visible instead of duplicating it.
|
||||
return null;
|
||||
}
|
||||
const registeredId = await this.registerFanavaranOtherPerson({
|
||||
clientKey: input.clientKey,
|
||||
person: input.person,
|
||||
claimCaseId: input.claimCaseId,
|
||||
requiredRoleId: 166,
|
||||
});
|
||||
if (registeredId == null) return null;
|
||||
if (input.claimCaseId) {
|
||||
await this.claimCaseDbService.findByIdAndUpdate(input.claimCaseId, {
|
||||
$set: {
|
||||
'fanavaranSync.baseClaim.otherPersonId': registeredId,
|
||||
'fanavaranSync.baseClaim.otherPersonNationalCode': nationalCode,
|
||||
'fanavaranSync.baseClaim.otherPersonBirthday': birthdayKey,
|
||||
},
|
||||
});
|
||||
}
|
||||
try {
|
||||
return selectFanavaranOtherPersonId(await inquire());
|
||||
} catch (error) {
|
||||
this.logger.warn(
|
||||
`[hull driver] Registration awaits role-166 verification: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** GEN.03 hull: TotalPremium + parties inquiry (AccidentCulpritId) before hull field whitelist. */
|
||||
private async enrichCarBodyHullBaseClaimPayload(input: {
|
||||
clientKey: FanavaranClientKey;
|
||||
@@ -2579,6 +2652,12 @@ export class ClaimRequestManagementService {
|
||||
firstParty?: { person?: unknown };
|
||||
logPrefix: string;
|
||||
claimCaseId?: string;
|
||||
registerMissingDriver?: boolean;
|
||||
cachedRegistration?: {
|
||||
otherPersonId?: number;
|
||||
otherPersonNationalCode?: string;
|
||||
otherPersonBirthday?: string;
|
||||
};
|
||||
}): Promise<void> {
|
||||
const policyId = parseFanavaranId(input.payload.PolicyId);
|
||||
if (policyId == null) return;
|
||||
@@ -2632,30 +2711,27 @@ export class ClaimRequestManagementService {
|
||||
const driverIdentity = resolveDriverFromParty(input.firstParty as any);
|
||||
const nationalCode =
|
||||
driverIdentity.nationalCode ??
|
||||
pickPersonNationalCode(person) ??
|
||||
pickCarBodyPolicyNationalCode(input.blameCase as { parties?: unknown[] });
|
||||
const birthday = driverIdentity.birthday ?? pickPersonBirthday(person);
|
||||
|
||||
let accidentCulpritId: number | null = null;
|
||||
if (nationalCode && birthday) {
|
||||
accidentCulpritId = await this.resolveDriverFanavaranId(
|
||||
input.clientKey,
|
||||
(person?.driverIsInsurer ? pickPersonNationalCode(person) : null);
|
||||
const birthday =
|
||||
driverIdentity.birthday ??
|
||||
(person?.driverIsInsurer ? pickPersonBirthday(person) : null);
|
||||
// Always use the actual driver identity; CustomerId identifies the policyholder.
|
||||
input.payload.AccidentCulpritId = await this.resolveHullDriverFanavaranId({
|
||||
clientKey: input.clientKey,
|
||||
person: {
|
||||
...person,
|
||||
nationalCodeOfDriver: nationalCode,
|
||||
nationalCodeOfInsurer: undefined,
|
||||
nationalCode,
|
||||
driverBirthday: birthday,
|
||||
insurerBirthday: undefined,
|
||||
birthday,
|
||||
false,
|
||||
);
|
||||
}
|
||||
if (accidentCulpritId == null) {
|
||||
accidentCulpritId = customerIdFromHullPolicyRecord(policyRecord);
|
||||
if (accidentCulpritId != null) {
|
||||
this.logger.log(
|
||||
`${input.logPrefix} AccidentCulpritId from policy CustomerId=${accidentCulpritId}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (accidentCulpritId != null) {
|
||||
input.payload.AccidentCulpritId = accidentCulpritId;
|
||||
}
|
||||
driverIsInsurer: false,
|
||||
},
|
||||
claimCaseId: input.claimCaseId,
|
||||
registerMissingDriver: input.registerMissingDriver,
|
||||
cachedRegistration: input.cachedRegistration,
|
||||
});
|
||||
|
||||
input.payload.CulpritLicenceNo = resolveFanavaranLicenceNo(
|
||||
driverIdentity,
|
||||
@@ -4236,6 +4312,7 @@ export class ClaimRequestManagementService {
|
||||
clientKey: FanavaranClientKey;
|
||||
person: any;
|
||||
claimCaseId?: string;
|
||||
requiredRoleId?: 166;
|
||||
}): Promise<number | null> {
|
||||
const nationalCode = pickPersonNationalCode(input.person);
|
||||
const birthday = pickPersonBirthday(input.person);
|
||||
@@ -4288,6 +4365,10 @@ export class ClaimRequestManagementService {
|
||||
birthDay: parsedBirthday.day,
|
||||
},
|
||||
);
|
||||
if (input.requiredRoleId === 166) {
|
||||
const existingId = selectFanavaranOtherPersonId(inquiryRows);
|
||||
if (existingId != null) return existingId;
|
||||
}
|
||||
const parties = asPartyInquiryRows(inquiryRows);
|
||||
const seedRow =
|
||||
parties.find(
|
||||
@@ -4320,10 +4401,11 @@ export class ClaimRequestManagementService {
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.warn(
|
||||
`[registerFanavaranOtherPerson] pre-create inquiry failed (continuing with local data): ${
|
||||
`[registerFanavaranOtherPerson] pre-create inquiry failed (${input.requiredRoleId === 166 ? 'registration deferred' : 'continuing with local data'}): ${
|
||||
error instanceof Error ? error.message : error
|
||||
}`,
|
||||
);
|
||||
if (input.requiredRoleId === 166) return null;
|
||||
}
|
||||
|
||||
const lookups = await this.loadFanavaranOtherPeopleLookups(input.clientKey);
|
||||
@@ -4417,6 +4499,23 @@ export class ClaimRequestManagementService {
|
||||
);
|
||||
}
|
||||
|
||||
if (input.requiredRoleId === 166) {
|
||||
try {
|
||||
return selectFanavaranOtherPersonId(
|
||||
await this.fanavaranLookupService.inquiryByUniqueIdentifier(
|
||||
input.clientKey,
|
||||
{
|
||||
nationalCode,
|
||||
birthYear: parsedBirthday.year,
|
||||
birthMonth: parsedBirthday.month,
|
||||
birthDay: parsedBirthday.day,
|
||||
},
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return this.resolveDriverFanavaranId(
|
||||
input.clientKey,
|
||||
nationalCode,
|
||||
@@ -4541,6 +4640,8 @@ export class ClaimRequestManagementService {
|
||||
resolvePolicy?: boolean;
|
||||
/** When true (submit), missing PolicyId throws. Preview keeps payload even if inquiry fails. */
|
||||
requirePolicyId?: boolean;
|
||||
/** Only submission may register the hull driver through GEN.44. */
|
||||
registerMissingHullDriver?: boolean;
|
||||
/**
|
||||
* When false (HTTP preview), missing staff ClaimExpertId stays null and is
|
||||
* reported in previewWarnings instead of throwing.
|
||||
@@ -4934,7 +5035,19 @@ export class ClaimRequestManagementService {
|
||||
firstParty,
|
||||
logPrefix,
|
||||
claimCaseId,
|
||||
registerMissingDriver: options?.registerMissingHullDriver === true,
|
||||
cachedRegistration: claimCase.fanavaranSync?.baseClaim,
|
||||
});
|
||||
if (payload.AccidentCulpritId == null) {
|
||||
const warning = {
|
||||
code: 'FANAVARAN_HULL_DRIVER_ROLE_REQUIRED',
|
||||
field: 'AccidentCulpritId',
|
||||
message:
|
||||
'راننده بیمه بدنه باید در فناوران با نقش سایر (166) ثبت و تأیید شود. ثبت شخص هنگام ارسال انجام میشود.',
|
||||
};
|
||||
if (requirePolicyId) throw new BadRequestException(warning);
|
||||
previewWarnings.push(warning);
|
||||
}
|
||||
applyFanavaranStageFieldReview(
|
||||
payload,
|
||||
claimCase,
|
||||
@@ -6988,6 +7101,7 @@ export class ClaimRequestManagementService {
|
||||
|
||||
const built = await this.previewFanavaranSubmitV2(claimCaseId, clientKey, {
|
||||
requirePolicyId: true,
|
||||
registerMissingHullDriver: true,
|
||||
persistPayload: true,
|
||||
});
|
||||
const hull = toFanavaranHullBaseClaimPayload(
|
||||
@@ -7960,6 +8074,7 @@ export class ClaimRequestManagementService {
|
||||
|
||||
const built = await this.previewFanavaranSubmitV2(claimCaseId, clientKey, {
|
||||
requirePolicyId: true,
|
||||
registerMissingHullDriver: true,
|
||||
auditSession,
|
||||
persistPayload: true,
|
||||
forceRefreshPolicy: true,
|
||||
|
||||
Reference in New Issue
Block a user