import { InquiryParticipantRole } from "src/common/dto/inquiry-participants.dto"; import type { InquiryParticipantRoleAssignments } from "src/request-management/inquiry-participant-resolver"; import { participantForStoredPartyRole } from "src/request-management/inquiry-participant-resolver"; import { normalizeNationalCode, parseJalaliDateParts, } from "./fanavaran-driver-inquiry"; import type { FanavaranClaimProduct } from "./fanavaran-claim-product"; /** * Fanavaran dummy licence issue date when `person.driverLicenseDate` is missing. * Set well in the past (> 3 years) so the new IsLicenseReplaced logic does not * force an explicit 0 answer for a missing date, and < 10 years so it stays null. */ export const FANAVARAN_DUMMY_LICENCE_ISSU_DATE = "1400/01/01"; /** Years since issue after which hull/third-party "licence replaced?" becomes yes (1). */ export const FANAVARAN_LICENCE_REPLACED_AFTER_YEARS = 10; /** * Years since issue below which Fanavaran requires an explicit 0/1 answer * for IsLicenseReplaced (rule: GovahiTamdidiMustBeRequiredWhenGovahiSodurDateYearIsLessThreeRule). */ export const FANAVARAN_LICENCE_REPLACED_REQUIRED_WITHIN_YEARS = 3; /** * True when `issueDate` is more than `years` before `referenceDate` (Jalali). * Used for Fanavaran IsLicenseReplaced / IsLicenseReplacement. */ export function isJalaliDateOlderThanYears( issueDate: unknown, referenceDate: unknown, years: number, ): boolean { const issue = parseJalaliDateParts(issueDate); const reference = parseJalaliDateParts(referenceDate); if (!issue || !reference) return false; const anniversary = (issue.year + years) * 10000 + issue.month * 100 + issue.day; const referenceDay = reference.year * 10000 + reference.month * 100 + reference.day; return referenceDay > anniversary; } /** * Hull `IsLicenseReplaced` / ثالث `IsLicenseReplacement`: * - licence issued more than 10 years before accident → `1` (replaced) * - licence issued less than 3 years before accident → `0` (not replaced, but * Fanavaran rule GovahiTamdidiMustBeRequiredWhenGovahiSodurDateYearIsLessThreeRule * requires an explicit answer, never null) * - 3–10 years → `null` (field is optional in this range) */ export function resolveFanavaranIsLicenseReplaced( licenceIssuDate: unknown, accidentDate: unknown, ): 1 | 0 | null { if ( isJalaliDateOlderThanYears( licenceIssuDate, accidentDate, FANAVARAN_LICENCE_REPLACED_AFTER_YEARS, ) ) { return 1; } // Newer than 3 years: Fanavaran requires explicit 0. if ( !isJalaliDateOlderThanYears( licenceIssuDate, accidentDate, FANAVARAN_LICENCE_REPLACED_REQUIRED_WITHIN_YEARS, ) ) { return 0; } return null; } export type FanavaranRoleIdentity = { nationalCode: string | null; birthday: string | number | null; /** Digits-only licence number when known. */ licenseNo: string | null; licenseType: string | null; /** * Jalali `YYYY/MM/DD` from `driverLicenseDate` when present. * Not a persisted model field — read opportunistically from party/person/participant. */ licenseIssuDate: string | null; }; type PartyLike = { participants?: Array>; participantRoles?: Partial; person?: Record | null; }; function nonEmptyText(value: unknown): string | null { if (value == null) return null; const text = String(value).trim(); if (!text || text.toLowerCase() === "null" || text.toLowerCase() === "undefined") { return null; } return text; } /** Digits-only licence number, or null when empty / non-digit. */ export function extractLicenceDigits(value: unknown): string | null { const text = nonEmptyText(value); if (!text) return null; const digits = text.replace(/[^\d]/g, ""); return digits || null; } /** * Normalize a stored `driverLicenseDate` (or similar) to Fanavaran Jalali `YYYY/MM/DD`. * Returns null when missing / unparseable so callers can fall back to the dummy. */ export function formatFanavaranLicenceIssuDate(value: unknown): string | null { const parsed = parseJalaliDateParts(value); if (!parsed) return null; const month = String(parsed.month).padStart(2, "0"); const day = String(parsed.day).padStart(2, "0"); return `${parsed.year}/${month}/${day}`; } function readDriverLicenseDate( ...sources: Array | null | undefined> ): string | null { for (const source of sources) { if (!source) continue; const formatted = formatFanavaranLicenceIssuDate( source.driverLicenseDate ?? source.licenseDate ?? source.licenceIssuDate, ); if (formatted) return formatted; } return null; } function fromParticipant( participant: Record | undefined, ): FanavaranRoleIdentity { if (!participant) { return { nationalCode: null, birthday: null, licenseNo: null, licenseType: null, licenseIssuDate: null, }; } return { nationalCode: normalizeNationalCode(participant.nationalCode), birthday: nonEmptyText(participant.birthday) ?? participant.birthday ?? null, licenseNo: extractLicenceDigits(participant.licenseNumber), licenseType: nonEmptyText(participant.licenseType), licenseIssuDate: readDriverLicenseDate(participant), }; } /** * Damaged/guilty party DRIVER: participants first, then legacy person.* fields. * Licence number prefers real user values; callers fall back to the Fanavaran dummy. */ export function resolveDriverFromParty(party: PartyLike | null | undefined): FanavaranRoleIdentity { const fromRole = fromParticipant( participantForStoredPartyRole( party ?? {}, InquiryParticipantRole.DRIVER, ) as Record | undefined, ); const person = party?.person ?? undefined; return { nationalCode: fromRole.nationalCode ?? normalizeNationalCode(person?.nationalCodeOfDriver) ?? normalizeNationalCode(person?.nationalCode), birthday: (fromRole.birthday != null && String(fromRole.birthday).trim() !== "" ? fromRole.birthday : null) ?? nonEmptyText(person?.driverBirthday) ?? nonEmptyText(person?.birthday) ?? person?.driverBirthday ?? person?.birthday ?? null, licenseNo: fromRole.licenseNo ?? extractLicenceDigits(person?.driverLicense) ?? extractLicenceDigits(person?.insurerLicense), licenseType: fromRole.licenseType ?? nonEmptyText(person?.licenseType), licenseIssuDate: fromRole.licenseIssuDate ?? readDriverLicenseDate(person), }; } /** Vehicle owner for OwnerId / DriverIsOwner / Sheba national-code routing. */ export function resolveOwnerFromParty(party: PartyLike | null | undefined): FanavaranRoleIdentity { const fromRole = fromParticipant( participantForStoredPartyRole( party ?? {}, InquiryParticipantRole.VEHICLE_OWNER, ) as Record | undefined, ); if (fromRole.nationalCode || fromRole.birthday) { return fromRole; } // Legacy parties had no dedicated owner fields. return { nationalCode: null, birthday: null, licenseNo: null, licenseType: null, licenseIssuDate: null, }; } /** * Policyholder national code for GEN.03 PolicyId inquiry: * THIRD_PARTY → third-party policyholder; CAR_BODY → car-body policyholder. */ export function resolvePolicyholderFromParty( party: PartyLike | null | undefined, product: FanavaranClaimProduct, ): FanavaranRoleIdentity { const role = product === "car-body" ? InquiryParticipantRole.CAR_BODY_POLICYHOLDER : InquiryParticipantRole.THIRD_PARTY_POLICYHOLDER; const fromRole = fromParticipant( participantForStoredPartyRole(party ?? {}, role) as | Record | undefined, ); const person = party?.person ?? undefined; return { nationalCode: fromRole.nationalCode ?? normalizeNationalCode(person?.nationalCodeOfInsurer) ?? normalizeNationalCode(person?.nationalCode), birthday: (fromRole.birthday != null && String(fromRole.birthday).trim() !== "" ? fromRole.birthday : null) ?? nonEmptyText(person?.insurerBirthday) ?? person?.insurerBirthday ?? null, licenseNo: fromRole.licenseNo ?? extractLicenceDigits(person?.insurerLicense), licenseType: fromRole.licenseType ?? null, licenseIssuDate: fromRole.licenseIssuDate, }; } /** * GEN.12 `DriverIsOwner`: driver national code equals vehicle-owner national code. * Falls back to legacy `person.driverIsInsurer` when owner role is absent. */ export function resolveDriverIsOwner(party: PartyLike | null | undefined): boolean { const driver = resolveDriverFromParty(party); const owner = resolveOwnerFromParty(party); if (driver.nationalCode && owner.nationalCode) { return driver.nationalCode === owner.nationalCode; } return party?.person?.driverIsInsurer === true; } /** Lookup: `accident-culprit-type` — بیمه‌گذار */ export const FANAVARAN_CULPRIT_TYPE_INSURER = 337; /** Lookup: `accident-culprit-type` — راننده غير از بيمه گذار */ export const FANAVARAN_CULPRIT_TYPE_DRIVER_NOT_INSURER = 338; /** * GEN.03 ثالث `CulpritTypeId` from whether the guilty party's driver is the insurer. * Never uses 728 (نا معلوم). */ export function resolveThirdPartyCulpritTypeId( party: PartyLike | null | undefined, ): number { const driver = resolveDriverFromParty(party); const insurer = resolvePolicyholderFromParty(party, "third-party"); if (driver.nationalCode && insurer.nationalCode) { return driver.nationalCode === insurer.nationalCode ? FANAVARAN_CULPRIT_TYPE_INSURER : FANAVARAN_CULPRIT_TYPE_DRIVER_NOT_INSURER; } if (party?.person?.driverIsInsurer === false) { return FANAVARAN_CULPRIT_TYPE_DRIVER_NOT_INSURER; } return FANAVARAN_CULPRIT_TYPE_INSURER; } /** Licence number for Fanavaran; never empty — uses `dummy` when user value is missing. */ export function resolveFanavaranLicenceNo( identity: Pick, dummy: string, ): string { return identity.licenseNo || dummy; } /** Licence issue date for Fanavaran; uses dummy when `driverLicenseDate` is absent. */ export function resolveFanavaranLicenceIssuDate( identity: Pick, dummy: string = FANAVARAN_DUMMY_LICENCE_ISSU_DATE, ): string { return identity.licenseIssuDate || dummy; }