forked from Yara724/api
Complete inquiry participant flow coverage
This commit is contained in:
@@ -391,12 +391,31 @@ export function assertPreviousPlateInquiryMatchesVin(
|
||||
}
|
||||
}
|
||||
|
||||
export function isPolicyNotFoundError(error: unknown): boolean {
|
||||
const candidate = error as Record<string, any> | null;
|
||||
const status = candidate?.status ?? candidate?.response?.status;
|
||||
if (Number(status) === 404) return true;
|
||||
const code = String(
|
||||
candidate?.code ?? candidate?.response?.data?.code ?? "",
|
||||
).toUpperCase();
|
||||
if (["NOT_FOUND", "POLICY_NOT_FOUND", "NO_POLICY"].includes(code)) {
|
||||
return true;
|
||||
}
|
||||
const message = String(
|
||||
candidate?.message ?? candidate?.response?.data?.message ?? error ?? "",
|
||||
);
|
||||
return /\bnot[ -]?found\b|\bno (?:relevant )?policy\b|یافت نشد|فاقد بیمه(?:نامه)?/i.test(
|
||||
message,
|
||||
);
|
||||
}
|
||||
|
||||
export async function runPlateInquiryWithFallback<T>(options: {
|
||||
vehicle?: ResolvedInquiryVehicle;
|
||||
fallbackCurrentPlate: InquiryVehicleInputDto["currentPlate"];
|
||||
query: (plate: InquiryVehicleInputDto["currentPlate"]) => Promise<T>;
|
||||
isUsable: (value: T) => boolean;
|
||||
mappedValue: (value: T) => Record<string, any>;
|
||||
shouldFallbackOnError?: (error: unknown) => boolean;
|
||||
}): Promise<{
|
||||
value: T;
|
||||
plateKind: "CURRENT" | "PREVIOUS";
|
||||
@@ -462,6 +481,15 @@ export async function runPlateInquiryWithFallback<T>(options: {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
}
|
||||
if (
|
||||
!isLast &&
|
||||
!(options.shouldFallbackOnError ?? isPolicyNotFoundError)(error)
|
||||
) {
|
||||
if (typeof error === "object" && error != null) {
|
||||
(error as { attempts?: typeof attempts }).attempts = attempts;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (isLast) {
|
||||
if (typeof error === "object" && error != null) {
|
||||
(error as { attempts?: typeof attempts }).attempts = attempts;
|
||||
|
||||
Reference in New Issue
Block a user