fix(fanavaran): require vehicle review and recover submitted base claims

Require approved vehicle classification before new base submissions. Allow a first review for existing base claims without damage or expertise, preserve remote IDs, and resume deferred damage submission. Keep approved submitted classifications locked and compare stage IDs and review snapshots when saving.
This commit is contained in:
2026-11-22 15:23:11 +03:30
parent d2676d65d1
commit 41a3372225
5 changed files with 82 additions and 9 deletions
+11 -4
View File
@@ -4120,9 +4120,13 @@ export class ExpertClaimService {
await this.getClaimDetailV2(claimRequestId, actor);
const claim = await this.claimCaseDbService.findById(claimRequestId);
if (!claim) throw new NotFoundException("Claim request not found");
if (claim.claimId != null || claim.claimNo != null) {
const baseSent = claim.claimId != null || claim.claimNo != null;
// Recover cases created before base submission required vehicle approval.
// Never change an approved snapshot or classification already sent downstream.
if (baseSent && ((claim as any).fanavaranVehicleReview?.approved === true ||
claim.dmgCaseId != null || claim.expertiseId != null)) {
throw new ConflictException(
"Vehicle classification cannot be changed after the claim was sent to Fanavaran.",
"مشخصات تأییدشده خودرو پس از ارسال به فناوران قابل تغییر نیست.",
);
}
const fields =
@@ -4209,8 +4213,11 @@ export class ExpertClaimService {
const updated = await this.claimCaseDbService.findOneAndUpdate(
{
_id: claimRequestId,
claimId: null,
claimNo: null,
claimId: claim.claimId ?? null,
claimNo: claim.claimNo ?? null,
dmgCaseId: claim.dmgCaseId ?? null,
expertiseId: claim.expertiseId ?? null,
fanavaranVehicleReview: (claim as any).fanavaranVehicleReview ?? null,
...(fields?.filter ?? {}),
},
{
@@ -748,7 +748,11 @@ describe("Expert Fanavaran stage fields", () => {
it("soft-skips automatic TejaratNo submission until both choices are saved", async () => {
process.env.FANAVARAN_CLIENT = "tejaratno";
const { management, claim } = setup("third-party", {
fanavaranVehicleReview: { approved: true },
fanavaranVehicleReview: {
approved: true,
vehicleGroup: { id: 1 }, vehicleUseType: { id: 1 },
vehicleSystem: { id: 14 }, vehicleKind: { id: 7456 },
},
});
management.blameRequestDbService = {
findById: jest.fn().mockResolvedValue({ type: "THIRD_PARTY" }),
@@ -114,6 +114,56 @@ describe("Fanavaran vehicle review", () => {
expect(service.claimCaseDbService.findOneAndUpdate).not.toHaveBeenCalled();
});
it("recovers an unreviewed base claim and resumes damage submission", async () => {
const service = makeService();
const claim = { _id: "507f1f77bcf86cd799439012", claimId: 12378660,
damage: { selectedParts: ["front-wing"] } };
service.claimCaseDbService.findById.mockResolvedValue(claim);
service.claimCaseDbService.findOneAndUpdate.mockImplementation((_filter, update) =>
Promise.resolve({ ...claim, fanavaranVehicleReview: update.$set.fanavaranVehicleReview }));
const result = await service.approveFanavaranVehicleReviewV2(claim._id, {
vehicleGroupId: 1, vehicleUseTypeId: 3, vehicleSystemId: 14,
vehicleKindId: 7456, confirmed: true,
}, actor);
expect(result.review.approved).toBe(true);
expect(service.claimCaseDbService.findOneAndUpdate.mock.calls[0][0]).toMatchObject({
claimId: 12378660, dmgCaseId: null, fanavaranVehicleReview: null,
});
expect(service.claimRequestManagementService.autoSubmitFanavaranDamageCaseOnOuterPartsSelected)
.toHaveBeenCalledWith(claim._id, ["front-wing"]);
});
it.each([
{ fanavaranVehicleReview: { approved: true } },
{ dmgCaseId: 42 },
{ expertiseId: 43 },
])("locks submitted classification: %j", async (state) => {
const service = makeService();
service.claimCaseDbService.findById.mockResolvedValue({ claimId: 12378660, ...state });
await expect(service.approveFanavaranVehicleReviewV2("case", {}, actor))
.rejects.toBeInstanceOf(ConflictException);
expect(service.claimCaseDbService.findOneAndUpdate).not.toHaveBeenCalled();
});
it("blocks a new manual base submission before building or posting its payload", async () => {
const service = Object.create(ClaimRequestManagementService.prototype) as any;
service.logger = { log: jest.fn() };
service.claimCaseDbService = { findById: jest.fn().mockResolvedValue({}) };
service.previewFanavaranSubmitV2 = jest.fn();
await expect(service.executeFanavaranV2Submit("case", "moallem", { PolicyId: 1 }))
.rejects.toMatchObject({ response: { code: "FANAVARAN_VEHICLE_REVIEW_REQUIRED" } });
expect(service.previewFanavaranSubmitV2).not.toHaveBeenCalled();
await expect(service.getFanavaranAutoSubmitSkipReason({})).resolves.toContain("تأیید");
});
it("reuses the existing base without requiring review or creating another base", async () => {
const service = Object.create(ClaimRequestManagementService.prototype) as any;
service.logger = { log: jest.fn() };
service.claimCaseDbService = { findById: jest.fn().mockResolvedValue({ claimId: 12378660 }) };
await expect(service.executeFanavaranV2Submit("case", "moallem"))
.resolves.toMatchObject({ Id: 12378660 });
});
it("blocks outbound stages until all four values are approved", () => {
const service = Object.create(
ClaimRequestManagementService.prototype,