import { Types } from "mongoose"; import { RequestManagementService } from "./request-management.service"; import { resolveBlameUserNextAction } from "src/helpers/blame-user-progress"; import { RoleEnum } from "src/Types&Enums/role.enum"; const guiltyId = "650000000000000000000001"; const damagedId = "650000000000000000000002"; const blameId = "650000000000000000000003"; const signed = { accepted: true, signature: { fileId: "signature" } }; const none = { hasClaim: false, shouldGuideToCreateClaim: false }; function blame(overrides: Record = {}) { return { _id: new Types.ObjectId(blameId), requestNo: "A100", publicId: "A100", type: "THIRD_PARTY", status: "COMPLETED", blameStatus: "AGREED", createdAt: new Date(), expert: { decision: { guiltyPartyId: guiltyId } }, parties: [ { role: "FIRST", person: { userId: guiltyId, phoneNumber: "09120000001" }, confirmation: signed, }, { role: "SECOND", person: { userId: damagedId, phoneNumber: "09120000002", nationalCode: "1234567890", }, confirmation: signed, }, ], ...overrides, }; } function subject() { const service = Object.create(RequestManagementService.prototype) as any; service.claimCaseDbService = { findOne: jest.fn().mockResolvedValue(null), find: jest.fn().mockResolvedValue([]), }; service.logger = { error: jest.fn() }; return service; } describe("user blame next action and claim eligibility", () => { it("distinguishes an unsigned viewer from a viewer waiting for the other signature", () => { expect(resolveBlameUserNextAction("WAITING_FOR_SIGNATURES", {}, none)).toBe( "SIGN", ); expect( resolveBlameUserNextAction( "WAITING_FOR_SIGNATURES", { confirmation: signed }, none, ), ).toBe("WAIT_FOR_OTHER_SIGNATURE"); expect( resolveBlameUserNextAction("WAITING_FOR_SIGNATURES", undefined, none), ).toBeNull(); expect( resolveBlameUserNextAction("WAITING_FOR_EXPERT", {}, none), ).toBeNull(); expect(resolveBlameUserNextAction("STOPPED", {}, none)).toBe("VIEW_RESULT"); }); it("permits a new third-party claim only for the damaged party after both accept", async () => { const service = subject(); for (const [viewer, eligible] of [ [damagedId, true], [guiltyId, false], [blameId, false], ] as const) { const hint = await service.computeClaimCreationHintForViewer( blame(), { sub: viewer }, null, ); expect(hint.shouldGuideToCreateClaim).toBe(eligible); } for (const status of [ "WAITING_FOR_SIGNATURES", "WAITING_FOR_EXPERT", "STOPPED", ]) { expect( ( await service.computeClaimCreationHintForViewer( blame({ status }), { sub: damagedId }, null, ) ).shouldGuideToCreateClaim, ).toBe(false); } const missingSignature = blame(); delete (missingSignature.parties[0] as any).confirmation; expect( ( await service.computeClaimCreationHintForViewer( missingSignature, { sub: damagedId }, null, ) ).shouldGuideToCreateClaim, ).toBe(false); }); it("does not guide rejected or existing claims to duplicate creation", async () => { const service = subject(); const rejected = blame(); rejected.parties[0].confirmation = { ...signed, accepted: false }; expect( ( await service.computeClaimCreationHintForViewer( rejected, { sub: damagedId }, null, ) ).shouldGuideToCreateClaim, ).toBe(false); const hint = await service.computeClaimCreationHintForViewer( blame(), { sub: damagedId }, { _id: blameId }, ); expect(hint).toEqual({ hasClaim: true, shouldGuideToCreateClaim: false, claimRequestId: blameId, }); expect(resolveBlameUserNextAction("COMPLETED", {}, hint)).toBe( "CONTINUE_CLAIM", ); expect(service.claimCaseDbService.findOne).not.toHaveBeenCalled(); }); it("shows an existing claim as actionable only to its beneficiary", async () => { const service = subject(); for (const [viewer, nextAction] of [ [guiltyId, "VIEW_RESULT"], [damagedId, "CONTINUE_CLAIM"], ]) { const hint = await service.computeClaimCreationHintForViewer( blame(), { sub: viewer }, { _id: blameId }, ); expect(resolveBlameUserNextAction("COMPLETED", {}, hint)).toBe( nextAction, ); } const unknown = blame({ expert: { decision: { guiltyPartyId: blameId } } }); const hint = await service.computeClaimCreationHintForViewer( unknown, { sub: damagedId }, null, ); expect(hint.shouldGuideToCreateClaim).toBe(false); }); it("permits a completed body-insurance claim only for its own party", async () => { const service = subject(); const body = blame({ type: "CAR_BODY", parties: [ { role: "FIRST", person: { userId: damagedId }, confirmation: signed }, ], }); expect( ( await service.computeClaimCreationHintForViewer( body, { sub: damagedId }, null, ) ).shouldGuideToCreateClaim, ).toBe(true); expect( ( await service.computeClaimCreationHintForViewer( body, { sub: guiltyId }, null, ) ).shouldGuideToCreateClaim, ).toBe(false); }); it("keeps identity filters working but omits private/search-only fields in list responses", async () => { const service = subject(); service.blameRequestDbService = { find: jest.fn().mockResolvedValue([ blame({ __v: 42, inquiries: { raw: "private" }, initiatedByFieldExpertId: guiltyId, }), ]), }; const response = await service.getAllBlameRequestsV2( { sub: damagedId, role: RoleEnum.USER }, { nationalCode: "۱۲۳۴۵۶۷۸۹۰", page: 1, limit: 10 }, ); expect(response.total).toBe(1); expect(response.list[0]).toMatchObject({ _id: blameId, nextAction: "CREATE_CLAIM", userSide: "SECOND", }); for (const key of [ "parties", "partyIdentities", "__v", "inquiries", "expert", "initiatedByFieldExpertId", ]) { expect(response.list[0]).not.toHaveProperty(key); } expect(JSON.stringify(response)).not.toContain("1234567890"); expect(service.claimCaseDbService.findOne).not.toHaveBeenCalled(); expect(service.claimCaseDbService.find).toHaveBeenCalledTimes(2); }); it("provides signing guidance in detail without returning the other party", async () => { const service = subject(); const record = blame({ status: "WAITING_FOR_SIGNATURES", workflow: { currentStep: "WAITING_FOR_SIGNATURES" }, }); delete (record.parties[1] as any).confirmation; service.blameRequestDbService = { findById: jest.fn().mockResolvedValue(record), }; const response = await service.getBlameRequestV2(blameId, { sub: damagedId, role: RoleEnum.USER, }); expect(response.nextAction).toBe("SIGN"); expect(response.userSide).toBe("SECOND"); expect(response.parties).toHaveLength(1); expect(response.parties[0].role).toBe("SECOND"); expect(JSON.stringify(response)).not.toContain("09120000001"); }); });