import { of } from "rxjs"; import { ClaimRequestManagementService } from "./claim-request-management.service"; function setup() { const service = Object.create(ClaimRequestManagementService.prototype) as any; service.logger = { warn: jest.fn() }; service.fanavaranLookupService = { fetchFromFanavaran: jest .fn() .mockResolvedValue([{ Id: 63, Caption: "File" }]), getRemoteLookup: jest.fn().mockResolvedValue([{ Id: 999 }]), }; service.fanavaranAuthService = { assertNotInBackoff: jest.fn(), clearBackoff: jest.fn(), }; service.getFanavaranAuthHeaders = jest.fn().mockResolvedValue({}); service.httpService = { post: jest.fn().mockReturnValue(of({ data: {} })) }; return service; } describe("hull live lookup submission checks", () => { it("bypasses file/database lookup caches when fresh responses are required", async () => { const service = setup(); expect( await service.getFanavaranLookupRows("parsian", "file-types", true), ).toEqual([{ Id: 63, Caption: "File" }]); expect( service.fanavaranLookupService.fetchFromFanavaran, ).toHaveBeenCalled(); expect( service.fanavaranLookupService.getRemoteLookup, ).not.toHaveBeenCalled(); }); it("prevents a hull write with an attachment ID absent from the API response", async () => { const service = setup(); await expect( service.postFanavaranJson( "https://example.test/car/vehicle-hull-claims/123/files", { FileTypeId: 999 }, "parsian", ), ).rejects.toMatchObject({ response: expect.objectContaining({ code: "FANAVARAN_HULL_LOOKUP_INVALID", }), }); expect(service.httpService.post).not.toHaveBeenCalled(); }); it("sends a valid attachment ID obtained from the current API response", async () => { const service = setup(); const url = "https://example.test/car/vehicle-hull-claims/123/files"; await service.postFanavaranJson(url, { FileTypeId: 63 }, "parsian"); expect( service.fanavaranLookupService.fetchFromFanavaran, ).toHaveBeenCalled(); expect(service.httpService.post).toHaveBeenCalledWith( url, { FileTypeId: 63 }, expect.anything(), ); }); it("validates base-claim selections against live responses and refreshes the premium from the policy", async () => { const service = setup(); const rows = { cities: 801, "accident-report-type": 802, "accident-causes": 803, "vehicle-hull-accident-types": 804, "vehicle-hull-accident-culprit-type": 805, "detection-accident-culprits": 806, "driving-licence-types": 807, "vehicle-use-types": 808, ans: 0, }; service.fanavaranLookupService.fetchFromFanavaran.mockImplementation( async (_client: string, url: string) => ({ data: [ { Id: rows[url.split("/").at(-1)], Caption: "Valid", IsActive: 1 }, ], }), ); service.fanavaranLookupService.bodyPolicyById = jest .fn() .mockResolvedValue({ TotalPremium: 123456 }); const payload = { PolicyId: 14231020, ActualPremium: 111, AccidentCityId: 801, AccidentReportTypeId: 802, AccidentCauseId: 803, AccidentTypeId: 804, CulpritTypeId: 805, AuthorityCulpritId: 806, CulpritLicenceTypeId: 807, AccidentVehicleUsedId: 808, IsLicenseReplaced: null, }; const url = "https://example.test/car/vehicle-hull-claims"; await service.postFanavaranJson(url, payload, "parsian"); expect(payload.ActualPremium).toBe(123456); expect( service.fanavaranLookupService.getRemoteLookup, ).not.toHaveBeenCalled(); service.httpService.post.mockClear(); await expect( service.postFanavaranJson( url, { ...payload, AccidentTypeId: 2 }, "parsian", ), ).rejects.toMatchObject({ response: expect.objectContaining({ warnings: expect.arrayContaining([ expect.stringContaining("AccidentTypeId=2"), ]), }), }); expect(service.httpService.post).not.toHaveBeenCalled(); }); it("rejects inactive attachment types even when their IDs match", async () => { const service = setup(); service.fanavaranLookupService.fetchFromFanavaran.mockResolvedValue([ { Id: 63, IsActive: 0 }, ]); await expect( service.postFanavaranJson( "https://example.test/car/vehicle-hull-claims/123/files", { FileTypeId: 63 }, "parsian", ), ).rejects.toThrow(); expect(service.httpService.post).not.toHaveBeenCalled(); }); it("blocks invalid lookup values on hull PUTs and multipart uploads too", async () => { const service = setup(); service.httpService.put = jest.fn(); service.fanavaranLookupService.fetchFromFanavaran.mockResolvedValue([]); const url = "https://example.test/car/vehicle-hull-claims/123/files"; await expect( service.putFanavaranJson(url, { FileTypeId: 63 }, "parsian"), ).rejects.toThrow(); await expect( service.postFanavaranMultipart(url, { FileTypeId: 63 }, [], "parsian"), ).rejects.toThrow(); expect(service.httpService.post).not.toHaveBeenCalled(); expect(service.httpService.put).not.toHaveBeenCalled(); }); it("resolves default hull base selections from changed lookup IDs", async () => { const service = setup(); service.logger.log = jest.fn(); service.getFanavaranLookupRows = jest.fn().mockImplementation( async (_client: string, name: string) => ({ "vehicle-hull-accident-types": [ { Id: 804, Caption: "تصادف (حادثه)" }, ], "vehicle-hull-accident-culprit-type": [ { Id: 805, Caption: "بیمه گذار" }, ], "detection-accident-culprits": [{ Id: 806, Caption: "بیمه گذار" }], })[name], ); service.fanavaranLookupService.bodyPolicyById = jest .fn() .mockResolvedValue({ TotalPremium: 123456 }); const payload: Record = { PolicyId: 14231020, CulpritTypeId: 337, }; await service.enrichCarBodyHullBaseClaimPayload({ clientKey: "parsian", payload, blameCase: {}, logPrefix: "test", }); expect(payload).toMatchObject({ AccidentTypeId: 804, CulpritTypeId: 805, AuthorityCulpritId: 806, ActualPremium: 123456, }); }); it("validates a manual payload without normalizing values or refreshing the supplied premium", async () => { const service = setup(); const ids = { cities: 801, "accident-report-type": 802, "accident-causes": 803, "vehicle-hull-accident-types": 804, "vehicle-hull-accident-culprit-type": 805, "detection-accident-culprits": 806, "driving-licence-types": 807, "vehicle-use-types": 808, }; service.fanavaranLookupService.fetchFromFanavaran.mockImplementation(async (_client, url) => [ { Id: ids[url.split("/").at(-1)], IsActive: 1 }, ]); service.fanavaranLookupService.bodyPolicyById = jest.fn().mockResolvedValue({ TotalPremium: 123456 }); const payload = { PolicyId: 1, ActualPremium: 12, AccidentCityId: "801", AccidentReportTypeId: 802, AccidentCauseId: 803, AccidentTypeId: 804, CulpritTypeId: 805, AuthorityCulpritId: 806, CulpritLicenceTypeId: 807, AccidentVehicleUsedId: 808, IsLicenseReplaced: null, }; const original = JSON.parse(JSON.stringify(payload)); await service.postFanavaranJson("https://example.test/car/vehicle-hull-claims", payload, "parsian", undefined, undefined, true); expect(payload).toEqual(original); expect(service.httpService.post).toHaveBeenCalledWith(expect.any(String), original, expect.any(Object)); expect(service.fanavaranLookupService.bodyPolicyById).not.toHaveBeenCalled(); }); });