import { BadRequestException, NotFoundException } from "@nestjs/common"; import { LookupsService } from "./lookups.service"; describe("LookupsService.findLastProcessedCarPolicy", () => { const originalClient = process.env.FANAVARAN_CLIENT; beforeAll(() => { process.env.FANAVARAN_CLIENT = "parsian"; }); afterAll(() => { process.env.FANAVARAN_CLIENT = originalClient; }); function createService(lookup: Record) { return new LookupsService( {} as any, lookup as any, ); } it("returns the last matching third-party policy and its vehicle", async () => { const lookup = { myPolicies: jest.fn().mockResolvedValue([ { PolicyId: 10, InsuranceLineId: 5, BeginDate: "1403/01/01", EndDate: "1404/01/01", }, { PolicyId: 20, InsuranceLineId: 5, BeginDate: "1405/01/01", EndDate: "1406/01/01", }, { PolicyId: 99, InsuranceLineId: 4, BeginDate: "1405/01/01", EndDate: "1406/01/01", }, ]), inquiryByVin: jest.fn().mockResolvedValue([{ Id: 500, VIN: "IRNKAEK4150012345" }]), thirdPartyPolicyById: jest.fn().mockImplementation((_key, policyId) => Promise.resolve({ PolicyId: policyId, VehicleId: policyId === 20 ? 500 : 700, BeginDate: policyId === 20 ? "1405/01/01" : "1403/01/01", EndDate: policyId === 20 ? "1406/01/01" : "1404/01/01", PreviousInsuranceCorpId: 1, }), ), vehicleById: jest.fn().mockImplementation((_key, vehicleId) => Promise.resolve({ Id: vehicleId, VIN: vehicleId === 500 ? "IRNKAEK4150012345" : "OTHERVIN000000000", }), ), getRemoteLookup: jest.fn().mockResolvedValue([]), }; const service = createService(lookup); const result = await service.findLastProcessedCarPolicy("third-party", { nationalCode: "0012345678", vin: "IRNKAEK4150012345", }); expect(result.policyId).toBe(20); expect(result.product).toBe("third-party"); expect(result.insuranceLineId).toBe(5); expect(result.vehicle).toEqual({ Id: 500, VIN: "IRNKAEK4150012345", }); expect(lookup.thirdPartyPolicyById).toHaveBeenCalled(); expect(lookup.myPolicies.mock.calls[0][2]).toBe(5); }); it("uses the hull policy GET for car-body", async () => { const lookup = { myPolicies: jest.fn().mockResolvedValue([ { PolicyId: 44, InsuranceLineId: 4, BeginDate: "1405/01/01", EndDate: "1406/01/01", }, ]), inquiryByVin: jest.fn(), bodyPolicyById: jest.fn().mockResolvedValue({ PolicyId: 44, VehicleId: 8, BeginDate: "1405/01/01", EndDate: "1406/01/01", }), thirdPartyPolicyById: jest.fn(), vehicleById: jest.fn().mockResolvedValue({ Id: 8, PlaqueLeftNo: "12", PlaqueMiddleCodeId: 2, PlaqueRightNo: "345", PlaqueSerial: "67", }), getRemoteLookup: jest.fn().mockResolvedValue([]), }; const service = createService(lookup); const result = await service.findLastProcessedCarPolicy("car-body", { nationalCode: "0012345678", plaqueLeft: "12", plaqueLetter: "ب", plaqueRight: "345", plaqueSerial: "67", }); expect(result.policyId).toBe(44); expect(result.product).toBe("car-body"); expect(lookup.bodyPolicyById).toHaveBeenCalled(); expect(lookup.thirdPartyPolicyById).not.toHaveBeenCalled(); expect(lookup.inquiryByVin).not.toHaveBeenCalled(); }); it("accepts the policy when VIN inquiry VehicleId matches and GEN.15 VIN is empty", async () => { const lookup = { myPolicies: jest.fn().mockResolvedValue([ { PolicyId: 15292336, InsuranceLineId: 4, BeginDate: "1405/01/01", EndDate: "1406/01/01", }, ]), inquiryByVin: jest.fn().mockResolvedValue([ { Id: 4118289, VIN: "NAAR03HFFRDE07024" }, ]), bodyPolicyById: jest.fn().mockResolvedValue({ PolicyId: 15292336, VehicleId: 4118289, BeginDate: "1405/01/01", EndDate: "1406/01/01", }), thirdPartyPolicyById: jest.fn(), vehicleById: jest.fn().mockResolvedValue({ Id: 4118289, VIN: null, ChassisNo: "RDE07024", }), getRemoteLookup: jest.fn().mockResolvedValue([]), }; const service = createService(lookup); const result = await service.findLastProcessedCarPolicy("car-body", { nationalCode: "4311402422", vin: "NAAR03HFFRDE07024", }); expect(result.policyId).toBe(15292336); expect(lookup.vehicleById.mock.calls[0][2]).toBeUndefined(); }); it("does not invent a search when the car does not match", async () => { const lookup = { myPolicies: jest.fn().mockResolvedValue([ { PolicyId: 10, InsuranceLineId: 5, BeginDate: "1405/01/01", EndDate: "1406/01/01", }, ]), inquiryByVin: jest.fn().mockResolvedValue([{ Id: 99, VIN: "IRNKAEK4150012345" }]), thirdPartyPolicyById: jest.fn().mockResolvedValue({ PolicyId: 10, VehicleId: 1, BeginDate: "1405/01/01", EndDate: "1406/01/01", }), vehicleById: jest.fn().mockResolvedValue({ Id: 1, VIN: "DIFFERENTVIN00001", }), getRemoteLookup: jest.fn().mockResolvedValue([]), }; const service = createService(lookup); await expect( service.findLastProcessedCarPolicy("third-party", { nationalCode: "0012345678", vin: "IRNKAEK4150012345", }), ).rejects.toBeInstanceOf(NotFoundException); }); it("rejects missing car identifiers", async () => { const service = createService({}); await expect( service.findLastProcessedCarPolicy("third-party", { nationalCode: "0012345678", }), ).rejects.toBeInstanceOf(BadRequestException); }); });