import { ClaimRequestManagementService } from "./claim-request-management.service"; function setup(product = "third-party") { const service = Object.create(ClaimRequestManagementService.prototype) as any; const claim: any = { _id: "claim-1", fanavaranSync: { attachments: { files: [], attempts: [] } }, }; const events: string[] = []; service.logger = { log: jest.fn(), warn: jest.fn(), error: jest.fn() }; service.resolveFanavaranClaimProduct = jest.fn().mockResolvedValue(product); service.claimCaseDbService = { findById: jest.fn().mockImplementation(async () => claim), }; service.executeFanavaranV2Submit = jest.fn().mockImplementation(async () => { events.push("baseClaim"); claim.claimId = 100; }); service.executeFanavaranDamageCaseSubmit = jest .fn() .mockImplementation(async () => { events.push("damageCase"); claim.dmgCaseId = 200; }); service.collectFanavaranAttachmentCandidates = jest .fn() .mockImplementation(async () => { const waived = service.getWaivedFanavaranAttachmentFileNames(claim); const uploaded = claim.fanavaranSync.attachments.files.length > 0; return { claimCase: claim, candidates: [ { fileName: "image.jpg", source: "capture:angle:front", path: "/tmp/image.jpg", alreadySubmitted: uploaded || waived.has("image.jpg"), alreadyWaived: waived.has("image.jpg"), waivedReason: waived.get("image.jpg"), }, ], }; }); service.submitFanavaranAttachmentsV2 = jest .fn() .mockImplementation(async () => { events.push("attachments"); claim.fanavaranSync.attachments.files.push({ fileName: "image.jpg", fileId: 71, }); return [{ submitted: true, fileId: 71, fileName: "image.jpg" }]; }); service.buildFanavaranExpertisePayload = jest .fn() .mockImplementation(async () => ({ payload: { DmgCaseId: claim.dmgCaseId }, warnings: [], })); service.assertFanavaranExpertisePayloadReady = jest.fn(); service.executeFanavaranExpertiseSubmit = jest .fn() .mockImplementation(async () => { events.push("expertise"); claim.expertiseId = 300; return { Id: 300 }; }); return { service, claim, events }; } describe("expertise submit completes earlier Fanavaran stages", () => { it("runs base, damage, attachments, expertise in order for third-party", async () => { const { service, events } = setup(); await service.submitFanavaranExpertiseV2("claim-1", "parsian"); expect(events).toEqual([ "baseClaim", "damageCase", "attachments", "expertise", ]); }); it("runs base, attachments, expertise for hull without creating a damage case", async () => { const { service, events } = setup("car-body"); await service.submitFanavaranExpertiseV2("claim-1", "parsian"); expect(events).toEqual(["baseClaim", "attachments", "expertise"]); expect(service.executeFanavaranDamageCaseSubmit).not.toHaveBeenCalled(); }); it("retries previously skipped images while reusing existing base/damage IDs", async () => { const { service, claim, events } = setup(); Object.assign(claim, { claimId: 100, dmgCaseId: 200 }); claim.fanavaranSync.attachments.status = "skipped"; await service.submitFanavaranExpertiseV2("claim-1", "parsian"); expect(events).toEqual(["attachments", "expertise"]); }); it.each(["failed", "skipped"])( "does not submit expertise if an image remains %s", async (status) => { const { service } = setup(); service.submitFanavaranAttachmentsV2.mockResolvedValue([ { fileName: "image.jpg", submitted: false, skipped: status === "skipped", warning: "Image unavailable", }, ]); await expect( service.submitFanavaranExpertiseV2("claim-1", "parsian"), ).rejects.toMatchObject({ response: expect.objectContaining({ stage: "attachments", pendingFiles: [expect.objectContaining({ fileName: "image.jpg" })], }), }); expect(service.executeFanavaranExpertiseSubmit).not.toHaveBeenCalled(); }, ); it("continues to expertise when an invalid-format image was waived after 2 tries", async () => { const { service, claim, events } = setup(); Object.assign(claim, { claimId: 100, dmgCaseId: 200 }); claim.fanavaranSync.attachments.attempts.push({ fileName: "image.jpg", status: "skipped", error: "FANAVARAN_ATTACHMENT_FORMAT_WAIVED: فرمت فایل معتبر نیست (after 2 attempts); continuing without this file.", }); service.submitFanavaranAttachmentsV2.mockImplementation(async () => { events.push("attachments"); return { attempted: 0, submitted: 0, skipped: 1, results: [], }; }); await service.submitFanavaranExpertiseV2("claim-1", "parsian"); expect(events).toEqual(["expertise"]); expect(service.submitFanavaranAttachmentsV2).not.toHaveBeenCalled(); }); it("stops immediately if the base stage fails", async () => { const { service } = setup(); service.executeFanavaranV2Submit.mockRejectedValue( new Error("Base unavailable"), ); await expect( service.submitFanavaranExpertiseV2("claim-1", "parsian"), ).rejects.toMatchObject({ response: expect.objectContaining({ stage: "baseClaim" }), }); expect(service.executeFanavaranDamageCaseSubmit).not.toHaveBeenCalled(); expect(service.submitFanavaranAttachmentsV2).not.toHaveBeenCalled(); expect(service.executeFanavaranExpertiseSubmit).not.toHaveBeenCalled(); }); it("stops before images and expertise if the damage stage fails", async () => { const { service } = setup(); service.executeFanavaranDamageCaseSubmit.mockRejectedValue( new Error("Damage unavailable"), ); await expect( service.submitFanavaranExpertiseV2("claim-1", "parsian"), ).rejects.toMatchObject({ response: expect.objectContaining({ stage: "damageCase" }), }); expect(service.submitFanavaranAttachmentsV2).not.toHaveBeenCalled(); expect(service.executeFanavaranExpertiseSubmit).not.toHaveBeenCalled(); }); it("requires saved image receipts even if the upload call reports success", async () => { const { service } = setup(); service.submitFanavaranAttachmentsV2.mockResolvedValue([ { submitted: true, fileId: 71 }, ]); await expect( service.submitFanavaranExpertiseV2("claim-1", "parsian"), ).rejects.toMatchObject({ response: expect.objectContaining({ stage: "attachments" }), }); expect(service.executeFanavaranExpertiseSubmit).not.toHaveBeenCalled(); }); it("also enforces attachments on the shared final write used by automatic submission", async () => { const { service, claim } = setup("car-body"); claim.claimId = 100; service.assertFanavaranVehicleReviewApproved = jest.fn(); service.postFanavaranJson = jest.fn(); service.submitFanavaranAttachmentsV2.mockResolvedValue([ { submitted: false, warning: "Image unavailable" }, ]); const execute = (ClaimRequestManagementService.prototype as any) .executeFanavaranExpertiseSubmit; await expect( execute.call(service, "claim-1", "parsian", {}, "AUTO_SUBMIT"), ).rejects.toMatchObject({ response: expect.objectContaining({ stage: "attachments" }), }); expect(service.postFanavaranJson).not.toHaveBeenCalled(); }); it("repairs missing attachments even if expertise already exists, without resubmitting expertise", async () => { const { service, claim, events } = setup(); Object.assign(claim, { claimId: 100, dmgCaseId: 200, expertiseId: 300 }); expect( await service.submitFanavaranExpertiseV2("claim-1", "parsian"), ).toMatchObject({ Id: 300 }); expect(events).toEqual(["attachments"]); expect(service.executeFanavaranExpertiseSubmit).not.toHaveBeenCalled(); }); it("does not repeat any completed stage on the next submit call", async () => { const { service, events } = setup(); await service.submitFanavaranExpertiseV2("claim-1", "parsian"); await service.submitFanavaranExpertiseV2("claim-1", "parsian"); expect(events).toEqual([ "baseClaim", "damageCase", "attachments", "expertise", ]); }); });