fix(licence): validate driving licence IDs and preserve verified captions

This commit is contained in:
SepehrYahyaee
2026-10-03 15:08:16 +03:30
parent b20250b4b3
commit d9a8ec4b2f
11 changed files with 319 additions and 7 deletions
+70
View File
@@ -0,0 +1,70 @@
import {
BadRequestException,
ServiceUnavailableException,
} from "@nestjs/common";
import {
normalizeDrivingLicenceTypes,
selectedDrivingLicenceType,
} from "./driving-licence-types";
import { LookupsService } from "./lookups.service";
const types = [
{ Id: 3, Caption: "ریلی", IsActive: 1 },
{ Id: 10, Caption: "پایه ۳", IsActive: 1 },
{ Id: 20, Caption: "غیرفعال", IsActive: 0 },
];
describe("canonical driving licence types", () => {
it.each([types, { data: types }, { data: { data: types } }, { Data: types }])(
"preserves provider IDs and captions across lookup envelopes",
(payload) => {
const normalized = normalizeDrivingLicenceTypes(payload);
expect(normalized).toEqual(types);
expect(selectedDrivingLicenceType("10", normalized).Caption).toBe(
"پایه ۳",
);
expect(selectedDrivingLicenceType("3", normalized).Caption).toBe("ریلی");
},
);
it.each(["پایه ۳", "BASE_3", "999", "20", "undefined"])(
"rejects unverified or inactive selection %s",
(value) => {
expect(() => selectedDrivingLicenceType(value, types)).toThrow(
BadRequestException,
);
},
);
it.each([
[],
{},
[{ Id: 3, Caption: "" }],
[{ Id: "invalid", Caption: "پایه ۳" }],
])(
"fails rather than inventing options for an unusable lookup",
(payload) => {
expect(() => normalizeDrivingLicenceTypes(payload)).toThrow(
ServiceUnavailableException,
);
},
);
it("rejects contradictory captions for the same ID", () => {
expect(() =>
normalizeDrivingLicenceTypes([
types[0],
{ ...types[0], Caption: "پایه ۳" },
]),
).toThrow(ServiceUnavailableException);
});
it("uses the existing tenant-specific remote/cache lookup", async () => {
const service = new LookupsService({} as any, {} as any, {} as any);
const lookup = jest
.spyOn(service, "getClientRemoteLookup")
.mockResolvedValue({ data: types });
expect(await service.getDrivingLicenceTypes()).toEqual(types);
expect(lookup).toHaveBeenCalledWith("driving-licence-types");
});
});
+60
View File
@@ -0,0 +1,60 @@
import {
BadRequestException,
ServiceUnavailableException,
} from "@nestjs/common";
export interface DrivingLicenceType {
Id: number;
Caption: string;
IsActive: number;
}
export function normalizeDrivingLicenceTypes(
payload: unknown,
): DrivingLicenceType[] {
let rows = payload;
for (let depth = 0; depth < 4 && !Array.isArray(rows); depth++) {
if (!rows || typeof rows !== "object") break;
const record = rows as Record<string, unknown>;
rows = record.data ?? record.Data;
}
const byId = new Map<number, DrivingLicenceType>();
for (const row of Array.isArray(rows) ? rows : []) {
if (!row || typeof row !== "object") continue;
const Id = Number(row.Id);
const Caption = typeof row.Caption === "string" ? row.Caption.trim() : "";
if (!Number.isSafeInteger(Id) || Id <= 0 || !Caption) continue;
if (byId.has(Id) && byId.get(Id)!.Caption !== Caption) {
throw new ServiceUnavailableException(
"فهرست انواع گواهینامه دارای کدهای متناقض است.",
);
}
byId.set(Id, {
Id,
Caption,
IsActive: row.IsActive == null ? 1 : Number(row.IsActive) === 0 ? 0 : 1,
});
}
if (!byId.size) {
throw new ServiceUnavailableException(
"فهرست انواع گواهینامه در دسترس نیست؛ دوباره تلاش کنید.",
);
}
return [...byId.values()];
}
export function selectedDrivingLicenceType(
value: unknown,
types: DrivingLicenceType[],
): DrivingLicenceType {
const code = String(value ?? "").trim();
const selected = types.find(
(item) => String(item.Id) === code && item.IsActive !== 0,
);
if (!selected) {
throw new BadRequestException(
"نوع گواهینامه را از فهرست استعلام‌شده انتخاب کنید.",
);
}
return selected;
}
+5 -2
View File
@@ -45,6 +45,7 @@ import {
VEHICLE_KINDS_REFRESH_MS,
} from "./vehicle-kind-search";
import { gregorianDateInIran } from "src/helpers/iran-datetime";
import { normalizeDrivingLicenceTypes, type DrivingLicenceType } from "./driving-licence-types";
type TejaratAccidentReasonRow = {
id: number;
@@ -188,8 +189,10 @@ export class LookupsService implements OnModuleInit, OnModuleDestroy {
return await this.getClientRemoteLookup("dmg-pay-method");
}
async getDrivingLicenceTypes(): Promise<any> {
return await this.getClientRemoteLookup("driving-licence-types");
async getDrivingLicenceTypes(): Promise<DrivingLicenceType[]> {
return normalizeDrivingLicenceTypes(
await this.getClientRemoteLookup("driving-licence-types"),
);
}
async getAccidentCulpritType(): Promise<any> {