forked from Yara724/api
Skip creation-time submission for Flow 1 while preserving branch validation, later staged submission, and other flows.
319 lines
12 KiB
TypeScript
319 lines
12 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
ConflictException,
|
|
ForbiddenException,
|
|
} from "@nestjs/common";
|
|
import { Types } from "mongoose";
|
|
import { ClaimRequestManagementService } from "./claim-request-management.service";
|
|
import {
|
|
canUserCreateClaim,
|
|
resolveClaimBeneficiary,
|
|
} from "src/helpers/blame-claim-eligibility";
|
|
import { RoleEnum } from "src/Types&Enums/role.enum";
|
|
|
|
const firstId = "650000000000000000000001";
|
|
const secondId = "650000000000000000000002";
|
|
const blameId = "650000000000000000000003";
|
|
const claimId = "650000000000000000000004";
|
|
const payerId = "650000000000000000000005";
|
|
const outsiderId = "650000000000000000000006";
|
|
const signed = { accepted: true, signature: { fileId: "signed" } };
|
|
|
|
function source(guiltyId = firstId): any {
|
|
return {
|
|
_id: new Types.ObjectId(blameId),
|
|
type: "THIRD_PARTY",
|
|
status: "COMPLETED",
|
|
publicId: "A100",
|
|
requestNo: "A100",
|
|
expert: { decision: { guiltyPartyId: guiltyId } },
|
|
parties: [
|
|
{
|
|
role: "FIRST",
|
|
person: {
|
|
userId: firstId,
|
|
clientId: payerId,
|
|
phoneNumber: "09120000001",
|
|
},
|
|
confirmation: signed,
|
|
},
|
|
{
|
|
role: "SECOND",
|
|
person: { userId: secondId, phoneNumber: "09120000002" },
|
|
confirmation: signed,
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
function serviceFor(blame: any): any {
|
|
const service = Object.create(ClaimRequestManagementService.prototype);
|
|
service.blameRequestDbService = {
|
|
findById: jest.fn().mockResolvedValue(blame),
|
|
};
|
|
service.claimCaseDbService = {
|
|
findOne: jest.fn().mockResolvedValue(null),
|
|
create: jest.fn().mockResolvedValue({ _id: claimId }),
|
|
};
|
|
service.userClaimBranchService = { validate: jest.fn().mockResolvedValue("650000000000000000000099") };
|
|
service.userDbService = { findAllByPhone: jest.fn().mockResolvedValue([]) };
|
|
service.generateUniqueClaimNumber = jest.fn().mockResolvedValue("C100");
|
|
service.autoSubmitToFanavaranV2OnClaimCreated = jest
|
|
.fn()
|
|
.mockResolvedValue({});
|
|
service.withFanavaranAutoSubmitMessage = (message: string) => message;
|
|
service.logger = { log: jest.fn(), error: jest.fn() };
|
|
return service;
|
|
}
|
|
|
|
describe("claim beneficiary authorization across shared user flows", () => {
|
|
it.each([firstId, secondId])(
|
|
"allows only the other party when guilt belongs to %s",
|
|
async (guiltyId) => {
|
|
const beneficiaryId = guiltyId === firstId ? secondId : firstId;
|
|
const blame = source(guiltyId);
|
|
const service = serviceFor(blame);
|
|
for (const actorId of [guiltyId, outsiderId]) {
|
|
await expect(
|
|
service.createClaimFromBlameV2(blameId, actorId),
|
|
).rejects.toBeInstanceOf(ForbiddenException);
|
|
}
|
|
expect(service.claimCaseDbService.create).not.toHaveBeenCalled();
|
|
await expect(
|
|
service.createClaimFromBlameV2(blameId, beneficiaryId),
|
|
).resolves.toMatchObject({ claimRequestId: claimId });
|
|
const saved = service.claimCaseDbService.create.mock.calls[0][0];
|
|
expect(String(saved.owner.userId)).toBe(beneficiaryId);
|
|
expect(String(saved.damagedPartyUserId)).toBe(beneficiaryId);
|
|
const payer = blame.parties.find((p: any) => p.person.userId === guiltyId)
|
|
.person.clientId;
|
|
expect(saved.owner.userClientKey).toBe(payer);
|
|
expect(
|
|
service.autoSubmitToFanavaranV2OnClaimCreated,
|
|
).not.toHaveBeenCalled();
|
|
},
|
|
);
|
|
|
|
it.each(["THIRD_PARTY", "CAR_BODY"])(
|
|
"defers Flow 1 %s submission at creation while retaining the selected branch",
|
|
async (type) => {
|
|
const blame = source();
|
|
blame.type = type;
|
|
blame.branchId = "650000000000000000000099";
|
|
if (type === "CAR_BODY") blame.parties = [blame.parties[0]];
|
|
const service = serviceFor(blame);
|
|
|
|
const result = await service.createClaimFromBlameV2(
|
|
blameId,
|
|
type === "CAR_BODY" ? firstId : secondId,
|
|
);
|
|
|
|
expect(result.fanavaran).toMatchObject({
|
|
attempted: false,
|
|
submitted: false,
|
|
skipped: true,
|
|
});
|
|
expect(service.autoSubmitToFanavaranV2OnClaimCreated).not.toHaveBeenCalled();
|
|
expect(service.userClaimBranchService.validate).toHaveBeenCalledWith(blame, blame.branchId);
|
|
expect(String(service.claimCaseDbService.create.mock.calls[0][0].branchId)).toBe(blame.branchId);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
{ callCenterInitiated: true },
|
|
{ expertInitiated: true, creationMethod: "LINK" },
|
|
{ expertInitiated: true, creationMethod: "IN_PERSON" },
|
|
{ registrarInitiated: true },
|
|
{ isMadeByFileMaker: true },
|
|
])("preserves early submission for other flows: %j", async (flags) => {
|
|
const service = serviceFor({ ...source(), ...flags });
|
|
|
|
await service.createClaimFromBlameV2(blameId, secondId);
|
|
|
|
expect(service.autoSubmitToFanavaranV2OnClaimCreated).toHaveBeenCalledTimes(1);
|
|
expect(service.autoSubmitToFanavaranV2OnClaimCreated).toHaveBeenCalledWith(claimId);
|
|
});
|
|
|
|
it("blocks incomplete, unsigned, rejected, and unresolved cases without creating or submitting a claim", async () => {
|
|
const invalid = [
|
|
{ ...source(), status: "WAITING_FOR_SIGNATURES" },
|
|
{ ...source(), expert: { decision: { guiltyPartyId: outsiderId } } },
|
|
{ ...source(), expert: {} },
|
|
];
|
|
for (const confirmation of [
|
|
{ accepted: true },
|
|
{ accepted: false, signature: { fileId: "signed" } },
|
|
]) {
|
|
const blame = source();
|
|
blame.parties[0].confirmation = confirmation;
|
|
invalid.push(blame);
|
|
}
|
|
const duplicated = source();
|
|
duplicated.parties[1].person.userId = firstId;
|
|
invalid.push(duplicated);
|
|
for (const blame of invalid) {
|
|
const service = serviceFor(blame);
|
|
expect(canUserCreateClaim(blame, secondId)).toBe(false);
|
|
await expect(
|
|
service.createClaimFromBlameV2(blameId, secondId),
|
|
).rejects.toBeInstanceOf(BadRequestException);
|
|
expect(service.claimCaseDbService.create).not.toHaveBeenCalled();
|
|
expect(
|
|
service.autoSubmitToFanavaranV2OnClaimCreated,
|
|
).not.toHaveBeenCalled();
|
|
}
|
|
});
|
|
|
|
it("never grants eligibility to an empty identity or an unknown case type", () => {
|
|
expect(canUserCreateClaim({ status: "COMPLETED", parties: [] }, "")).toBe(
|
|
false,
|
|
);
|
|
expect(canUserCreateClaim({ ...source(), type: "UNKNOWN" }, secondId)).toBe(
|
|
false,
|
|
);
|
|
});
|
|
|
|
it("retains existing-claim conflict handling for its legitimate beneficiary", async () => {
|
|
const service = serviceFor(source());
|
|
service.claimCaseDbService.findOne.mockResolvedValue({ _id: claimId });
|
|
await expect(
|
|
service.createClaimFromBlameV2(blameId, secondId),
|
|
).rejects.toBeInstanceOf(ConflictException);
|
|
expect(service.claimCaseDbService.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps body-insurance creation restricted to the first party and persists its beneficiary", async () => {
|
|
const blame = source();
|
|
blame.type = "CAR_BODY";
|
|
blame.parties = [blame.parties[0]];
|
|
const service = serviceFor(blame);
|
|
expect(resolveClaimBeneficiary(blame)?.person?.userId).toBe(firstId);
|
|
await expect(
|
|
service.createClaimFromBlameV2(blameId, secondId),
|
|
).rejects.toBeInstanceOf(ForbiddenException);
|
|
await service.createClaimFromBlameV2(blameId, firstId);
|
|
const saved = service.claimCaseDbService.create.mock.calls[0][0];
|
|
expect(String(saved.damagedPartyUserId)).toBe(firstId);
|
|
expect(String(saved.owner.clientId)).toBe(payerId);
|
|
});
|
|
|
|
it("denies historical guilty owners while allowing the damaged party and its phone-linked identity", async () => {
|
|
const service = serviceFor(source());
|
|
const claim = {
|
|
blameRequestId: blameId,
|
|
owner: { userId: firstId },
|
|
damagedPartyUserId: secondId,
|
|
};
|
|
const check = (id: string, actor?: { username?: string }) =>
|
|
service.assertEffectiveUserIsDamagedPartyOnClaim(claim, id, actor);
|
|
await expect(check(firstId)).rejects.toBeInstanceOf(ForbiddenException);
|
|
await expect(check(outsiderId)).rejects.toBeInstanceOf(ForbiddenException);
|
|
await expect(check(secondId)).resolves.toBeUndefined();
|
|
service.userDbService.findAllByPhone.mockResolvedValue([{ _id: secondId }]);
|
|
await expect(
|
|
check(outsiderId, { username: "989120000002" }),
|
|
).resolves.toBeUndefined();
|
|
service.blameRequestDbService.findById.mockResolvedValue({
|
|
...source(),
|
|
expert: {},
|
|
});
|
|
await expect(check(firstId)).rejects.toBeInstanceOf(ForbiddenException);
|
|
});
|
|
|
|
it("returns read-only details for the guilty party and actionable guidance for the beneficiary", async () => {
|
|
const service = serviceFor(source());
|
|
const claim = {
|
|
_id: new Types.ObjectId(claimId),
|
|
blameRequestId: new Types.ObjectId(blameId),
|
|
publicId: "A100",
|
|
requestNo: "C100",
|
|
owner: { userId: firstId },
|
|
damagedPartyUserId: secondId,
|
|
status: "SELECTING_OUTER_PARTS",
|
|
};
|
|
service.claimCaseDbService.findById = jest.fn().mockResolvedValue(claim);
|
|
service.blameRequestDbService.find = jest
|
|
.fn()
|
|
.mockResolvedValue([source()]);
|
|
const guilty = await service.getClaimDetailsV2(claimId, firstId, {
|
|
sub: firstId,
|
|
role: RoleEnum.USER,
|
|
});
|
|
expect(guilty.canAct).toBe(false);
|
|
expect(guilty).not.toHaveProperty("ownerGuidance");
|
|
const damaged = await service.getClaimDetailsV2(claimId, secondId, {
|
|
sub: secondId,
|
|
role: RoleEnum.USER,
|
|
});
|
|
expect(damaged.canAct).toBe(true);
|
|
expect(damaged.ownerGuidance).toBeDefined();
|
|
});
|
|
|
|
it("uses a persisted beneficiary when the source is unavailable, never guesses from snapshot non-ownership", async () => {
|
|
const service = serviceFor(null);
|
|
const claim = {
|
|
blameRequestId: blameId,
|
|
owner: { userId: firstId },
|
|
damagedPartyUserId: secondId,
|
|
};
|
|
await expect(
|
|
service.assertEffectiveUserIsDamagedPartyOnClaim(claim, firstId),
|
|
).rejects.toBeInstanceOf(ForbiddenException);
|
|
await expect(
|
|
service.assertEffectiveUserIsDamagedPartyOnClaim(claim, secondId),
|
|
).resolves.toBeUndefined();
|
|
const ambiguous = {
|
|
blameRequestId: blameId,
|
|
snapshot: { parties: source().parties },
|
|
owner: { userId: secondId },
|
|
};
|
|
await expect(
|
|
service.assertEffectiveUserIsDamagedPartyOnClaim(ambiguous, firstId),
|
|
).rejects.toBeInstanceOf(ForbiddenException);
|
|
await expect(
|
|
service.assertEffectiveUserIsDamagedPartyOnClaim(
|
|
{ owner: { userId: secondId } },
|
|
secondId,
|
|
),
|
|
).resolves.toBeUndefined();
|
|
});
|
|
|
|
it("keeps the authorized in-person expert acting for the beneficiary in reorganized flows", async () => {
|
|
const blame = {
|
|
...source(),
|
|
creationMethod: "IN_PERSON",
|
|
expertInitiated: true,
|
|
initiatedByFieldExpertId: outsiderId,
|
|
};
|
|
const service = serviceFor(blame);
|
|
const claim = {
|
|
blameRequestId: blameId,
|
|
owner: { userId: secondId },
|
|
damagedPartyUserId: secondId,
|
|
};
|
|
const effectiveId = await service.resolveClaimEffectiveUserId(
|
|
claim,
|
|
outsiderId,
|
|
RoleEnum.FIELD_EXPERT,
|
|
);
|
|
expect(effectiveId).toBe(secondId);
|
|
await expect(
|
|
service.assertEffectiveUserIsDamagedPartyOnClaim(claim, effectiveId),
|
|
).resolves.toBeUndefined();
|
|
});
|
|
it("copies the validated branch into the claim and blocks creation when branch validation fails", async () => {
|
|
const blame = source();
|
|
blame.branchId = "650000000000000000000099";
|
|
const service = serviceFor(blame);
|
|
await service.createClaimFromBlameV2(blameId, secondId);
|
|
expect(service.userClaimBranchService.validate).toHaveBeenCalledWith(blame, blame.branchId);
|
|
expect(String(service.claimCaseDbService.create.mock.calls[0][0].branchId)).toBe(blame.branchId);
|
|
const blocked = serviceFor(source());
|
|
blocked.userClaimBranchService.validate.mockRejectedValue(new BadRequestException("شعبه را انتخاب کنید"));
|
|
await expect(blocked.createClaimFromBlameV2(blameId, secondId)).rejects.toBeInstanceOf(BadRequestException);
|
|
expect(blocked.claimCaseDbService.create).not.toHaveBeenCalled();
|
|
expect(blocked.autoSubmitToFanavaranV2OnClaimCreated).not.toHaveBeenCalled();
|
|
});
|
|
|
|
});
|