forked from Yara724/api
car body implemented
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { parseFanavaranId } from "src/lookups/fanavaran-last-car-policy";
|
||||
|
||||
export const FANAVARAN_HULL_DMG_KIND_CAPTION = "تخریب";
|
||||
export const FANAVARAN_HULL_DMG_COST_KIND_CAPTION = "مجموع لوازم";
|
||||
|
||||
/** Fallback when lookup fetch fails (Parsian `vehicle-hull-dmg-kind`). */
|
||||
export const FANAVARAN_DEFAULT_HULL_DMG_KIND_ID = 5485;
|
||||
/** Fallback when lookup fetch fails (`vehicle-hull-dmg-cost-kinds` Id). */
|
||||
export const FANAVARAN_DEFAULT_HULL_DMG_COST_KIND_ID = 1;
|
||||
|
||||
export type FanavaranHullDmgAccessoryRow = {
|
||||
Id?: unknown;
|
||||
AccessoryKindId?: unknown;
|
||||
AccessoryDesc?: unknown;
|
||||
};
|
||||
|
||||
export function asLookupRows(value: unknown): Record<string, unknown>[] {
|
||||
if (!Array.isArray(value)) return [];
|
||||
return value.filter(
|
||||
(row): row is Record<string, unknown> =>
|
||||
!!row && typeof row === "object" && !Array.isArray(row),
|
||||
);
|
||||
}
|
||||
|
||||
export function asHullDmgAccessoryRows(value: unknown): FanavaranHullDmgAccessoryRow[] {
|
||||
return asLookupRows(value) as FanavaranHullDmgAccessoryRow[];
|
||||
}
|
||||
|
||||
export function findLookupIdByCaption(
|
||||
rows: unknown,
|
||||
caption: string,
|
||||
): number | null {
|
||||
const target = caption.trim();
|
||||
for (const row of asLookupRows(rows)) {
|
||||
if (String(row.Caption ?? "").trim() !== target) continue;
|
||||
const id = parseFanavaranId(row.Id);
|
||||
if (id != null) return id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function isFactoryDefaultAccessoryRow(row: FanavaranHullDmgAccessoryRow): boolean {
|
||||
const desc = String(row.AccessoryDesc ?? "").trim();
|
||||
return (
|
||||
desc.includes("کليه قطعات فابريک") ||
|
||||
desc.includes("کلیه قطعات فابریک") ||
|
||||
desc.includes("کليه قطعات") ||
|
||||
desc.includes("فابريک")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* GEN.06 VehicleHullAccessoryId from policy dmg-accessories.
|
||||
* 1) Match AccessoryKindId to car-components part id.
|
||||
* 2) Else factory bundle row (app default outer parts map here on Parsian).
|
||||
*/
|
||||
export function resolveVehicleHullAccessoryId(
|
||||
accessories: FanavaranHullDmgAccessoryRow[],
|
||||
accessoryKindId: number | null,
|
||||
): number | null {
|
||||
if (accessories.length === 0) return null;
|
||||
|
||||
if (accessoryKindId != null) {
|
||||
const exact = accessories.find(
|
||||
(row) => parseFanavaranId(row.AccessoryKindId) === accessoryKindId,
|
||||
);
|
||||
const exactId = parseFanavaranId(exact?.Id);
|
||||
if (exactId != null) return exactId;
|
||||
}
|
||||
|
||||
const factory = accessories.find(isFactoryDefaultAccessoryRow);
|
||||
const factoryId = parseFanavaranId(factory?.Id);
|
||||
if (factoryId != null) return factoryId;
|
||||
|
||||
return parseFanavaranId(accessories[0]?.Id);
|
||||
}
|
||||
Reference in New Issue
Block a user