forked from Yara724/api
115 lines
3.4 KiB
TypeScript
115 lines
3.4 KiB
TypeScript
export const FANAVARAN_INSURER_ROLE_ID = 161;
|
|
export const FANAVARAN_DRIVER_ROLE_ID = 166;
|
|
|
|
export type FanavaranPartyInquiryRow = {
|
|
Id?: unknown;
|
|
RoleId?: unknown;
|
|
NationalCode?: unknown;
|
|
Name?: unknown;
|
|
LastName?: unknown;
|
|
};
|
|
|
|
function asPositiveId(value: unknown): number | null {
|
|
if (value === null || value === undefined) return null;
|
|
const id = Number(value);
|
|
return Number.isFinite(id) && id > 0 ? id : null;
|
|
}
|
|
|
|
export function asPartyInquiryRows(value: unknown): FanavaranPartyInquiryRow[] {
|
|
if (Array.isArray(value)) {
|
|
return value.filter(
|
|
(row): row is FanavaranPartyInquiryRow =>
|
|
!!row && typeof row === "object" && !Array.isArray(row),
|
|
);
|
|
}
|
|
if (!value || typeof value !== "object") return [];
|
|
const record = value as Record<string, unknown>;
|
|
for (const key of ["value", "Value", "items", "Items", "data", "Data"]) {
|
|
const nested = record[key];
|
|
if (Array.isArray(nested)) return asPartyInquiryRows(nested);
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* DriverId for GEN.12 زیاندیده. Prefer insurer (161) or driver (166)
|
|
* based on driverIsInsurer, then any returned party id.
|
|
*/
|
|
export function selectFanavaranDriverId(
|
|
rows: unknown,
|
|
driverIsInsurer: boolean | undefined,
|
|
): number | null {
|
|
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 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);
|
|
|
|
return asPositiveId(parties[0].Id);
|
|
}
|
|
|
|
export function pickPersonNationalCode(person: {
|
|
driverIsInsurer?: boolean;
|
|
nationalCodeOfDriver?: unknown;
|
|
nationalCodeOfInsurer?: unknown;
|
|
} | null | undefined): string | null {
|
|
if (!person) return null;
|
|
const driver = String(person.nationalCodeOfDriver ?? "").trim();
|
|
const insurer = String(person.nationalCodeOfInsurer ?? "").trim();
|
|
if (person.driverIsInsurer) {
|
|
return insurer || driver || null;
|
|
}
|
|
return driver || insurer || null;
|
|
}
|
|
|
|
export function pickPersonBirthday(person: {
|
|
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,
|
|
]) {
|
|
if (value == null || String(value).trim() === "") continue;
|
|
return value as string | number;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function parseJalaliDateParts(
|
|
birthday: string | number | null | undefined,
|
|
): { year: number; month: number; day: number } | null {
|
|
if (birthday == null) return null;
|
|
const str = String(birthday).trim();
|
|
if (/^\d{8}$/.test(str)) {
|
|
const year = parseInt(str.slice(0, 4), 10);
|
|
const month = parseInt(str.slice(4, 6), 10);
|
|
const day = parseInt(str.slice(6, 8), 10);
|
|
if (!year || !month || !day) return null;
|
|
return { year, month, day };
|
|
}
|
|
const parts = str.split(/[/\-.]/);
|
|
if (parts.length < 3) return null;
|
|
const year = parseInt(parts[0], 10);
|
|
const month = parseInt(parts[1], 10);
|
|
const day = parseInt(parts[2], 10);
|
|
if (!year || !month || !day) return null;
|
|
return { year, month, day };
|
|
}
|