import { BlameRequestType } from "src/Types&Enums/blame-request-management/blameRequestType.enum"; import { PartyRole } from "./entities/schema/partyRole.enum"; import { RequestManagementService } from "./request-management.service"; describe("RequestManagementService policyholder inquiry routing", () => { const currentPlate = { leftDigits: "44", centerAlphabet: "ب", centerDigits: "111", ir: "22", }; const previousPlate = { leftDigits: "55", centerAlphabet: "ج", centerDigits: "222", ir: "33", }; const vehicle = { registrationState: "RECENTLY_TRANSFERRED", currentPlate, previousPlate, previousPolicyholderNationalCode: "0098765432", vin: "NAAM01E15HK123456", }; function participantInput(caseType: BlameRequestType) { return { driver: { nationalCode: "0011111111", birthday: "1370/01/01", hasDrivingLicense: false, }, vehicleOwner: { sameAs: "DRIVER" }, thirdPartyPolicyholder: { nationalCode: "0022222222", birthday: "1360/02/02", }, ...(caseType === BlameRequestType.CAR_BODY ? { carBodyPolicyholder: { nationalCode: "0033333333", birthday: "1350/03/03", }, } : {}), vehicle, }; } it("does not fall back from the current plate for third-party insurance", async () => { const service = Object.create(RequestManagementService.prototype) as any; service.sandHubService = { getTejaratBlockInquiry: jest .fn() .mockResolvedValueOnce({ raw: {}, mapped: {} }) .mockResolvedValueOnce({ raw: {}, mapped: { CompanyName: "پارسیان", VinNumberField: vehicle.vin }, }), }; const submission = service.normalizeInquiryInput( BlameRequestType.THIRD_PARTY, participantInput(BlameRequestType.THIRD_PARTY), ); await expect(service.getThirdPartyPlateInquiry(submission)).rejects.toThrow( "بیمه‌نامه معتبر و فعالی مطابق مشخصات خودرو و کد ملی واردشده یافت نشد.", ); expect(service.sandHubService.getTejaratBlockInquiry).toHaveBeenCalledTimes( 1, ); expect(service.sandHubService.getTejaratBlockInquiry).toHaveBeenCalledWith( expect.objectContaining({ plate: currentPlate, nationalCodeOfInsurer: "0022222222", }), undefined, ); }); it("propagates the mapped ESG Persian error for third-party insurance", async () => { const service = Object.create(RequestManagementService.prototype) as any; service.sandHubService = { getTejaratBlockInquiry: jest.fn().mockResolvedValue({ raw: { success: false, error: { code: "RECORD_NOT_FOUND", message: "Provider request failed", messageFa: "رکوردی یافت نشد", }, }, mapped: { Error: { Message: "رکوردی یافت نشد" } }, }), }; const submission = service.normalizeInquiryInput( BlameRequestType.THIRD_PARTY, participantInput(BlameRequestType.THIRD_PARTY), ); await expect( service.getThirdPartyPlateInquiry(submission), ).rejects.toThrow("رکوردی یافت نشد"); expect(service.sandHubService.getTejaratBlockInquiry).toHaveBeenCalledTimes( 1, ); }); it("does not fall back from the current plate for car-body insurance", async () => { const service = Object.create(RequestManagementService.prototype) as any; service.sandHubService = { getCarBodyInquiry: jest .fn() .mockResolvedValueOnce({ raw: {}, mapped: {} }) .mockResolvedValueOnce({ raw: {}, mapped: { policyNumber: "BODY-1", VinNumberField: vehicle.vin }, }), }; const submission = service.normalizeInquiryInput( BlameRequestType.CAR_BODY, participantInput(BlameRequestType.CAR_BODY), ); await expect(service.getCarBodyPlateInquiry(submission)).rejects.toThrow( "بیمه‌نامه معتبر و فعالی مطابق مشخصات خودرو و کد ملی واردشده یافت نشد.", ); expect(service.sandHubService.getCarBodyInquiry).toHaveBeenCalledTimes(1); expect(service.sandHubService.getCarBodyInquiry).toHaveBeenCalledWith( expect.objectContaining({ plate: currentPlate, nationalCodeOfInsurer: "0033333333", }), ); }); it("queries VIN with the third-party policyholder", async () => { const service = Object.create(RequestManagementService.prototype) as any; service.sandHubService = { getCarByChassisInquiry: jest.fn().mockResolvedValue({ raw: {}, mapped: { CompanyName: "پارسیان" }, }), }; const submission = service.normalizeInquiryInput( BlameRequestType.THIRD_PARTY, participantInput(BlameRequestType.THIRD_PARTY), ); await service.getThirdPartyVinInquiry(submission); expect( service.sandHubService.getCarByChassisInquiry, ).toHaveBeenCalledTimes(1); expect( service.sandHubService.getCarByChassisInquiry, ).toHaveBeenCalledWith( { nationalCode: "0022222222", chassisNo: vehicle.vin, }, undefined, ); }); it("runs both third-party and car-body inquiries for a car-body VIN", async () => { const service = Object.create(RequestManagementService.prototype) as any; service.getThirdPartyVinInquiry = jest.fn().mockResolvedValue({ raw: { success: true }, mapped: { CompanyCode: "8", CompanyName: "بیمه پارسیان", PrntPlcyCmpDocNo: "TP-1", }, }); service.sandHubService = { getCarBodyInquiry: jest.fn().mockResolvedValue({ source: "ESG_CAR_BODY_VIN_INQUIRY", raw: { policyId: 123 }, mapped: { policyNumber: "BODY-1", companyId: "8", CompanyName: "بیمه پارسیان", VinNumberField: vehicle.vin, }, }), }; service.clientService = { findOrCreateClientByCompanyCode: jest .fn() .mockResolvedValue({ _id: "client-third-party" }), }; service.resolveCarBodyPolicyClientId = jest .fn() .mockResolvedValue("client-car-body"); service.runParticipantPersonalInquiries = jest.fn().mockResolvedValue(undefined); service.runVehicleOwnershipInquiry = jest.fn().mockResolvedValue(undefined); const req: any = { _id: "car-body-vin-request", type: BlameRequestType.CAR_BODY, inquiries: {}, }; const party: any = { person: {}, vehicle: {}, insurance: {} }; await service.runPartyInquiriesVinV3Internal( req, participantInput(BlameRequestType.CAR_BODY), PartyRole.FIRST, party, ); expect(service.getThirdPartyVinInquiry).toHaveBeenCalledTimes(1); expect(service.sandHubService.getCarBodyInquiry).toHaveBeenCalledWith({ nationalCodeOfInsurer: "0033333333", plate: vehicle.vin, }); expect(req.inquiries.thirdParty.has).toBe(true); expect(req.inquiries.carBody.has).toBe(true); expect(party.insurance.policyNumber).toBe("TP-1"); expect(party.insurance.carBodyInsurance.policyNumber).toBe("BODY-1"); }); });