forked from Yara724/api
248 lines
8.1 KiB
TypeScript
248 lines
8.1 KiB
TypeScript
import {
|
||
BadRequestException,
|
||
ConflictException,
|
||
ForbiddenException,
|
||
} from "@nestjs/common";
|
||
import { validate } from "class-validator";
|
||
import { Types } from "mongoose";
|
||
import { ClaimRequestManagementService } from "./claim-request-management.service";
|
||
import { savedClaimBankInformation } from "./claim-bank-information";
|
||
import { SelectOtherPartsV2Dto } from "./dto/select-other-parts-v2.dto";
|
||
|
||
const claimId = "650000000000000000000001";
|
||
const userId = "650000000000000000000002";
|
||
const blameId = "650000000000000000000003";
|
||
const sheba = "IR" + "1".repeat(24);
|
||
const ownerCode = "0012345678";
|
||
|
||
function fixture(
|
||
money: any = undefined,
|
||
type = "CAR_BODY",
|
||
step = "SELECT_OTHER_PARTS",
|
||
) {
|
||
const claim: any = {
|
||
_id: new Types.ObjectId(claimId),
|
||
publicId: "TEST",
|
||
blameRequestId: blameId,
|
||
owner: { userId, fullName: "نمونه" },
|
||
money,
|
||
workflow: { currentStep: step },
|
||
};
|
||
const service = Object.create(ClaimRequestManagementService.prototype) as any;
|
||
service.claimCaseDbService = {
|
||
findById: jest.fn().mockResolvedValue(claim),
|
||
findByIdAndUpdate: jest.fn().mockResolvedValue(claim),
|
||
};
|
||
service.blameRequestDbService = {
|
||
findById: jest.fn().mockResolvedValue({ type }),
|
||
};
|
||
service.resolveClaimEffectiveUserId = jest.fn().mockResolvedValue(userId);
|
||
service.assertEffectiveUserIsDamagedPartyOnClaim = jest
|
||
.fn()
|
||
.mockResolvedValue(undefined);
|
||
service.resolveVehicleOwnerNationalCodeForClaim = jest
|
||
.fn()
|
||
.mockReturnValue(ownerCode);
|
||
service.resolveOwnershipPlateForClaim = jest
|
||
.fn()
|
||
.mockReturnValue({ leftDigits: 12 });
|
||
service.sandHubService = {
|
||
getShebaValidation: jest.fn().mockResolvedValue({ ReturnValue: true }),
|
||
};
|
||
service.logger = { log: jest.fn(), error: jest.fn() };
|
||
return { service, claim };
|
||
}
|
||
|
||
describe("single-entry claim bank information", () => {
|
||
it.each(["CAR_BODY", "THIRD_PARTY"])(
|
||
"reuses saved bank information for %s, including resend",
|
||
async (type) => {
|
||
for (const step of ["SELECT_OTHER_PARTS", "USER_EXPERT_RESEND"]) {
|
||
for (const storedSheba of [sheba, sheba.slice(2)]) {
|
||
const { service } = fixture(
|
||
{ sheba: storedSheba, nationalCodeOfInsurer: ownerCode },
|
||
type,
|
||
step,
|
||
);
|
||
const result = await service.selectOtherPartsV2(
|
||
claimId,
|
||
{ otherParts: ["engine"] },
|
||
userId,
|
||
);
|
||
expect(result.currentStep).toBe("CAPTURE_PART_DAMAGES");
|
||
expect(
|
||
service.claimCaseDbService.findByIdAndUpdate,
|
||
).toHaveBeenCalledWith(
|
||
claimId,
|
||
expect.objectContaining({
|
||
"money.sheba": sheba,
|
||
"money.nationalCodeOfInsurer": ownerCode,
|
||
"damage.otherParts": ["engine"],
|
||
}),
|
||
);
|
||
expect(
|
||
service.sandHubService.getShebaValidation,
|
||
).not.toHaveBeenCalled();
|
||
}
|
||
}
|
||
},
|
||
);
|
||
|
||
it.each(["sheba", "shebaNumber"])(
|
||
"collects a new %s once and derives the vehicle owner identity",
|
||
async (field) => {
|
||
const { service } = fixture();
|
||
await service.selectOtherPartsV2(claimId, { [field]: sheba }, userId);
|
||
expect(service.sandHubService.getShebaValidation).toHaveBeenCalledWith(
|
||
ownerCode,
|
||
sheba,
|
||
);
|
||
expect(service.claimCaseDbService.findByIdAndUpdate).toHaveBeenCalledWith(
|
||
claimId,
|
||
expect.objectContaining({
|
||
"money.sheba": sheba,
|
||
"money.nationalCodeOfInsurer": ownerCode,
|
||
}),
|
||
);
|
||
},
|
||
);
|
||
|
||
it("retains a saved Sheba when only the legacy owner identity was missing", async () => {
|
||
const { service } = fixture({ sheba });
|
||
await service.selectOtherPartsV2(claimId, {}, userId);
|
||
expect(service.sandHubService.getShebaValidation).toHaveBeenCalledWith(
|
||
ownerCode,
|
||
sheba,
|
||
);
|
||
});
|
||
|
||
it("accepts the same previously saved number with a different IR/whitespace representation", async () => {
|
||
const { service } = fixture({
|
||
sheba: sheba.slice(2),
|
||
nationalCodeOfInsurer: ownerCode,
|
||
});
|
||
await service.selectOtherPartsV2(
|
||
claimId,
|
||
{ sheba: " ir " + sheba.slice(2) },
|
||
userId,
|
||
);
|
||
expect(service.sandHubService.getShebaValidation).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("still requires and validates bank information when none was saved", async () => {
|
||
const { service } = fixture();
|
||
for (const value of [undefined, "IR", "invalid", "123"]) {
|
||
await expect(
|
||
service.selectOtherPartsV2(claimId, { sheba: value }, userId),
|
||
).rejects.toBeInstanceOf(BadRequestException);
|
||
}
|
||
expect(service.claimCaseDbService.findByIdAndUpdate).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("does not silently replace saved bank information outside an explicit resend", async () => {
|
||
const { service } = fixture({ sheba, nationalCodeOfInsurer: ownerCode });
|
||
await expect(
|
||
service.selectOtherPartsV2(
|
||
claimId,
|
||
{ sheba: "IR" + "2".repeat(24) },
|
||
userId,
|
||
),
|
||
).rejects.toBeInstanceOf(ConflictException);
|
||
expect(service.claimCaseDbService.findByIdAndUpdate).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("does not reuse another owner’s bank information", async () => {
|
||
const { service } = fixture({ sheba, nationalCodeOfInsurer: "0098765432" });
|
||
await expect(
|
||
service.selectOtherPartsV2(claimId, {}, userId),
|
||
).rejects.toBeInstanceOf(BadRequestException);
|
||
expect(service.claimCaseDbService.findByIdAndUpdate).not.toHaveBeenCalled();
|
||
expect(
|
||
savedClaimBankInformation(
|
||
{ sheba, nationalCodeOfInsurer: "0098765432" },
|
||
ownerCode,
|
||
),
|
||
).toMatchObject({
|
||
shebaProvided: false,
|
||
ownerNationalCodeProvided: true,
|
||
});
|
||
});
|
||
|
||
it("rejects a submitted national code that differs from the vehicle owner", async () => {
|
||
const { service } = fixture();
|
||
await expect(
|
||
service.selectOtherPartsV2(
|
||
claimId,
|
||
{ sheba, nationalCodeOfInsurer: "0098765432" },
|
||
userId,
|
||
),
|
||
).rejects.toBeInstanceOf(BadRequestException);
|
||
});
|
||
|
||
it("keeps beneficiary authorization before reusing saved bank information", async () => {
|
||
const { service } = fixture({ sheba, nationalCodeOfInsurer: ownerCode });
|
||
service.assertEffectiveUserIsDamagedPartyOnClaim.mockRejectedValue(
|
||
new ForbiddenException(),
|
||
);
|
||
await expect(
|
||
service.selectOtherPartsV2(claimId, {}, userId),
|
||
).rejects.toBeInstanceOf(ForbiddenException);
|
||
expect(service.claimCaseDbService.findByIdAndUpdate).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it.each([sheba, sheba.slice(2)])(
|
||
"lets the expert continue with saved bank information (%s)",
|
||
async (storedSheba) => {
|
||
const { service } = fixture({
|
||
sheba: storedSheba,
|
||
nationalCodeOfInsurer: ownerCode,
|
||
});
|
||
service.assertFieldExpertInPersonBlameForClaimCase = jest
|
||
.fn()
|
||
.mockResolvedValue(undefined);
|
||
await service.expertCompleteClaimData(
|
||
claimId,
|
||
{ sub: userId },
|
||
{ otherParts: [] },
|
||
);
|
||
expect(service.claimCaseDbService.findByIdAndUpdate).toHaveBeenCalledWith(
|
||
claimId,
|
||
expect.objectContaining({
|
||
"money.sheba": sheba,
|
||
"workflow.currentStep": "CAPTURE_PART_DAMAGES",
|
||
}),
|
||
);
|
||
},
|
||
);
|
||
|
||
it("allows omitted bank fields through DTO validation but rejects malformed field types", async () => {
|
||
expect(
|
||
await validate(
|
||
Object.assign(new SelectOtherPartsV2Dto(), { otherParts: [] }),
|
||
),
|
||
).toHaveLength(0);
|
||
expect(
|
||
await validate(
|
||
Object.assign(new SelectOtherPartsV2Dto(), { sheba: 123 }),
|
||
),
|
||
).not.toHaveLength(0);
|
||
});
|
||
|
||
it("recognizes incomplete and malformed saved details without exposing them", () => {
|
||
expect(savedClaimBankInformation()).toMatchObject({
|
||
shebaProvided: false,
|
||
ownerNationalCodeProvided: false,
|
||
});
|
||
expect(
|
||
savedClaimBankInformation({
|
||
sheba: "IR123",
|
||
nationalCodeOfInsurer: "123",
|
||
}),
|
||
).toMatchObject({ shebaProvided: false, ownerNationalCodeProvided: false });
|
||
expect(savedClaimBankInformation({ sheba })).toMatchObject({
|
||
shebaProvided: true,
|
||
ownerNationalCodeProvided: false,
|
||
});
|
||
});
|
||
});
|