forked from Yara724/api
fanavaran car body and third_party lookup codings applied
This commit is contained in:
@@ -19,11 +19,13 @@ import {
|
||||
asObjectRecord,
|
||||
filterPoliciesByLine,
|
||||
insuranceLineIdForProduct,
|
||||
insuranceLineLabel,
|
||||
parseFanavaranId,
|
||||
parseLastCarPolicyInput,
|
||||
pickVehicleId,
|
||||
selectLastAmongCarMatches,
|
||||
sortPoliciesNewestEndDateFirst,
|
||||
toAppPlaque,
|
||||
vehicleMatchesCar,
|
||||
type FanavaranCarPolicyProduct,
|
||||
type HydratedFanavaranCarPolicy,
|
||||
@@ -203,9 +205,11 @@ export class LookupsService {
|
||||
},
|
||||
): Promise<{
|
||||
product: FanavaranCarPolicyProduct;
|
||||
insuranceLine: "CAR_BODY" | "THIRD_PARTY" | null;
|
||||
insuranceLineId: number;
|
||||
policyId: number;
|
||||
policy: any;
|
||||
customer: Record<string, unknown> | null;
|
||||
vehicle: Record<string, unknown> | null;
|
||||
}> {
|
||||
const parsed = parseLastCarPolicyInput(query);
|
||||
@@ -355,10 +359,77 @@ export class LookupsService {
|
||||
|
||||
return {
|
||||
product,
|
||||
insuranceLine: insuranceLineLabel(insuranceLineId),
|
||||
insuranceLineId,
|
||||
policyId: selected.policyId,
|
||||
policy: await this.mapPolicyDetails(selected.policy),
|
||||
vehicle: selected.vehicle,
|
||||
customer: await this.fetchCustomer(
|
||||
clientKey,
|
||||
parseFanavaranId(selected.policy.CustomerId),
|
||||
requestOptions,
|
||||
),
|
||||
vehicle: await this.enrichVehicle(selected.vehicle),
|
||||
};
|
||||
}
|
||||
|
||||
private async fetchCustomer(
|
||||
clientKey: ReturnType<LookupsService["activeClientKey"]>,
|
||||
customerId: number | null,
|
||||
requestOptions: { contractIdOverride?: string },
|
||||
): Promise<Record<string, unknown> | null> {
|
||||
if (customerId === null) return null;
|
||||
try {
|
||||
return asObjectRecord(
|
||||
await this.fanavaranLookupService.customerById(
|
||||
clientKey,
|
||||
customerId,
|
||||
requestOptions,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.warn(
|
||||
`Customer GET failed for CustomerId=${customerId}: ${
|
||||
error instanceof Error ? error.message : error
|
||||
}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async lookupRowById(
|
||||
lookupName: string,
|
||||
id: number | null,
|
||||
): Promise<Record<string, unknown> | null> {
|
||||
if (id === null) return null;
|
||||
try {
|
||||
const rows = await this.getClientRemoteLookup(lookupName);
|
||||
if (!Array.isArray(rows)) return null;
|
||||
const match = rows.find(
|
||||
(row) => parseFanavaranId((row as { Id?: unknown })?.Id) === id,
|
||||
);
|
||||
return asObjectRecord(match);
|
||||
} catch (error) {
|
||||
this.logger.warn(
|
||||
`Failed to resolve ${lookupName} id=${id}: ${
|
||||
error instanceof Error ? error.message : error
|
||||
}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async enrichVehicle(
|
||||
vehicle: Record<string, unknown> | null,
|
||||
): Promise<Record<string, unknown> | null> {
|
||||
if (!vehicle) return null;
|
||||
const vehicleKindId = parseFanavaranId(vehicle.VehicleKindId);
|
||||
const usedId = parseFanavaranId(vehicle.UsedId);
|
||||
return {
|
||||
...vehicle,
|
||||
color: null,
|
||||
vehicleKind: await this.lookupRowById("vehicle-kinds", vehicleKindId),
|
||||
used: await this.lookupRowById("vehicle-use-types", usedId),
|
||||
plaque: toAppPlaque(vehicle),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user