updated the fucking mf fanavaran

This commit is contained in:
2026-09-07 16:33:15 +03:30
parent 395e3dbe63
commit b9bffdccc1
5 changed files with 423 additions and 114 deletions

View File

@@ -1,4 +1,7 @@
import { toEnglishDigits } from "src/lookups/fanavaran-last-car-policy";
export const FANAVARAN_INSURER_ROLE_ID = 161;
export const FANAVARAN_PLAQUE_OWNER_ROLE_ID = 163;
export const FANAVARAN_DRIVER_ROLE_ID = 166;
export type FanavaranPartyInquiryRow = {
@@ -42,61 +45,109 @@ export function selectFanavaranDriverId(
const parties = asPartyInquiryRows(rows);
if (parties.length === 0) return null;
const preferredRole = driverIsInsurer
? FANAVARAN_INSURER_ROLE_ID
: FANAVARAN_DRIVER_ROLE_ID;
const preferred = parties.find(
(row) => asPositiveId(row.RoleId) === preferredRole,
);
if (preferred) return asPositiveId(preferred.Id);
const preferredRoles = driverIsInsurer
? [FANAVARAN_INSURER_ROLE_ID, FANAVARAN_DRIVER_ROLE_ID, FANAVARAN_PLAQUE_OWNER_ROLE_ID]
: [FANAVARAN_DRIVER_ROLE_ID, FANAVARAN_INSURER_ROLE_ID, FANAVARAN_PLAQUE_OWNER_ROLE_ID];
const fallbackRole = driverIsInsurer
? FANAVARAN_DRIVER_ROLE_ID
: FANAVARAN_INSURER_ROLE_ID;
const fallback = parties.find(
(row) => asPositiveId(row.RoleId) === fallbackRole,
);
if (fallback) return asPositiveId(fallback.Id);
for (const roleId of preferredRoles) {
const match = parties.find((row) => asPositiveId(row.RoleId) === roleId);
if (match) return asPositiveId(match.Id);
}
return asPositiveId(parties[0].Id);
}
export function normalizeNationalCode(value: unknown): string | null {
const digits = toEnglishDigits(value).replace(/\D/g, "");
if (digits.length === 10) return digits;
if (digits.length === 8 || digits.length === 9) return digits.padStart(10, "0");
return digits.length > 0 ? digits : null;
}
export function pickPersonNationalCode(person: {
driverIsInsurer?: boolean;
nationalCodeOfDriver?: unknown;
nationalCodeOfInsurer?: unknown;
nationalCode?: unknown;
} | null | undefined): string | null {
if (!person) return null;
const driver = String(person.nationalCodeOfDriver ?? "").trim();
const insurer = String(person.nationalCodeOfInsurer ?? "").trim();
const driver = normalizeNationalCode(person.nationalCodeOfDriver);
const insurer = normalizeNationalCode(person.nationalCodeOfInsurer);
const generic = normalizeNationalCode(person.nationalCode);
if (person.driverIsInsurer) {
return insurer || driver || null;
return insurer || driver || generic;
}
return driver || insurer || null;
return driver || insurer || generic;
}
export function pickPersonBirthday(person: {
driverIsInsurer?: boolean;
driverBirthday?: unknown;
birthday?: unknown;
insurerBirthday?: unknown;
} | null | undefined): string | number | null {
if (!person) return null;
for (const value of [
person.driverBirthday,
person.birthday,
person.insurerBirthday,
]) {
const ordered = person.driverIsInsurer
? [person.insurerBirthday, person.birthday, person.driverBirthday]
: [person.driverBirthday, person.birthday, person.insurerBirthday];
for (const value of ordered) {
if (value == null || String(value).trim() === "") continue;
return value as string | number;
if (parseJalaliDateParts(value)) return value as string | number;
}
return null;
}
export function collectPersonNationalCodes(
person: {
nationalCodeOfDriver?: unknown;
nationalCodeOfInsurer?: unknown;
nationalCode?: unknown;
} | null | undefined,
): string[] {
const codes = [
normalizeNationalCode(person?.nationalCodeOfDriver),
normalizeNationalCode(person?.nationalCodeOfInsurer),
normalizeNationalCode(person?.nationalCode),
].filter((value): value is string => !!value);
return [...new Set(codes)];
}
export function collectPersonBirthdays(person: {
driverBirthday?: unknown;
birthday?: unknown;
insurerBirthday?: unknown;
} | null | undefined): Array<string | number> {
const values = [
person?.insurerBirthday,
person?.birthday,
person?.driverBirthday,
].filter((value) => value != null && String(value).trim() !== "");
const unique: Array<string | number> = [];
const seen = new Set<string>();
for (const value of values) {
const parsed = parseJalaliDateParts(value);
const key = parsed
? `${parsed.year}-${parsed.month}-${parsed.day}`
: String(value);
if (seen.has(key)) continue;
seen.add(key);
unique.push(value as string | number);
}
return unique;
}
export function parseJalaliDateParts(
birthday: string | number | null | undefined,
birthday: unknown,
): { year: number; month: number; day: number } | null {
if (birthday == null) return null;
const str = String(birthday).trim();
if (typeof birthday === "object") {
const year = Number(toEnglishDigits((birthday as any).year ?? (birthday as any).BirthYear));
const month = Number(toEnglishDigits((birthday as any).month ?? (birthday as any).BirthMonth));
const day = Number(toEnglishDigits((birthday as any).day ?? (birthday as any).BirthDay));
if (year > 0 && month > 0 && day > 0) return { year, month, day };
return null;
}
const str = toEnglishDigits(birthday).trim();
if (/^\d{8}$/.test(str)) {
const year = parseInt(str.slice(0, 4), 10);
const month = parseInt(str.slice(4, 6), 10);