forked from Yara724/api
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
export type FanavaranVehicleKindRow = {
|
|
Id?: unknown;
|
|
VehicleGroupId?: unknown;
|
|
IsActive?: unknown;
|
|
Caption?: unknown;
|
|
};
|
|
|
|
export type FanavaranVehicleUseTypeRow = {
|
|
Id?: unknown;
|
|
VehicleGroupId?: unknown;
|
|
IsActive?: unknown;
|
|
Caption?: 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 vehicleGroupIdForKind(
|
|
kinds: unknown,
|
|
vehicleKindId: number,
|
|
): number | null {
|
|
if (!Array.isArray(kinds)) return null;
|
|
const match = kinds.find(
|
|
(row) => asPositiveId((row as FanavaranVehicleKindRow)?.Id) === vehicleKindId,
|
|
) as FanavaranVehicleKindRow | undefined;
|
|
return asPositiveId(match?.VehicleGroupId);
|
|
}
|
|
|
|
/**
|
|
* Fanavaran filters AccidentVehicleUsedId by the policy car's VehicleGroupId.
|
|
* `preferredId` is the vehicle UsedId from VIN inquiry / vehicle GET — never a Mongo tenant default.
|
|
*/
|
|
export function selectAccidentVehicleUsedId(
|
|
useTypes: unknown,
|
|
vehicleGroupId: number | null,
|
|
preferredId: number | null,
|
|
): number | null {
|
|
if (!Array.isArray(useTypes) || vehicleGroupId == null) {
|
|
return preferredId;
|
|
}
|
|
|
|
const active = useTypes.filter((row) => {
|
|
const item = row as FanavaranVehicleUseTypeRow;
|
|
return (
|
|
item?.IsActive === 1 &&
|
|
asPositiveId(item.VehicleGroupId) === vehicleGroupId &&
|
|
asPositiveId(item.Id) != null
|
|
);
|
|
}) as FanavaranVehicleUseTypeRow[];
|
|
|
|
if (active.length === 0) {
|
|
return preferredId;
|
|
}
|
|
|
|
const preferred = active.find((row) => asPositiveId(row.Id) === preferredId);
|
|
if (preferred && preferredId != null) {
|
|
return preferredId;
|
|
}
|
|
|
|
const personal = active.find((row) =>
|
|
String(row.Caption ?? "").includes("شخص"),
|
|
);
|
|
if (personal) {
|
|
return asPositiveId(personal.Id) ?? preferredId;
|
|
}
|
|
|
|
return asPositiveId(active[0].Id) ?? preferredId;
|
|
}
|