forked from Yara724/api
update the lookups list
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user