From 41a337222523e6f2a2b5b910019b4735debad0d4 Mon Sep 17 00:00:00 2001 From: "s.hajizadeh" Date: Sun, 22 Nov 2026 15:23:11 +0330 Subject: [PATCH] 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. --- docs/fanavaran-vehicle-review.md | 9 ++++ .../claim-request-management.service.ts | 11 ++-- src/expert-claim/expert-claim.service.ts | 15 ++++-- .../fanavaran-stage-fields.spec.ts | 6 ++- .../fanavaran-vehicle-review.spec.ts | 50 +++++++++++++++++++ 5 files changed, 82 insertions(+), 9 deletions(-) diff --git a/docs/fanavaran-vehicle-review.md b/docs/fanavaran-vehicle-review.md index f3c30f3..bfd8763 100644 --- a/docs/fanavaran-vehicle-review.md +++ b/docs/fanavaran-vehicle-review.md @@ -43,3 +43,12 @@ The same tab also supports [expert editing of accident and assessment fields](fa Base claim, damage case, attachments, and expertise writes enforce the persisted approval. Automatic stages are deferred with a skip reason; manual stages return `409` with code `FANAVARAN_VEHICLE_REVIEW_REQUIRED`. Read-only previews remain available for diagnosis. Once Fanavaran has created the base claim, the review is immutable in the panel because changing it would make the local snapshot disagree with data already written remotely. + + +## Recovery for previously submitted base claims + +New base submissions (automatic and manual) require vehicle approval. Existing base claims are reused without another POST. + +A case whose base was already submitted without vehicle approval may receive its first approval while `dmgCaseId` and `expertiseId` are absent. This uses the normal authorized review endpoint and active lookup/relationship validation. Approval preserves the existing base IDs and resumes damage submission when selected parts exist. The update compares the current review and stage IDs atomically so a competing approval or submission requires refresh. + +An approved classification stays locked after base submission. Recovery does not rewrite the remote base payload. Base and damage use-type derivation can refer to different parties, so recovery does not impose equality between their use-type fields. diff --git a/src/claim-request-management/claim-request-management.service.ts b/src/claim-request-management/claim-request-management.service.ts index 17b442f..4770e18 100644 --- a/src/claim-request-management/claim-request-management.service.ts +++ b/src/claim-request-management/claim-request-management.service.ts @@ -6875,7 +6875,7 @@ export class ClaimRequestManagementService { throw new ConflictException({ code: 'FANAVARAN_VEHICLE_REVIEW_REQUIRED', message: - 'An expert must review and approve the vehicle group, use type, system, and kind before any data can be sent to Fanavaran.', + 'کارشناس باید پیش از ارسال به فناوران، نوع خودرو، نوع استفاده، سیستم و نام خودرو را بررسی و تأیید کند.', }); } } @@ -6887,8 +6887,11 @@ export class ClaimRequestManagementService { return 'Already submitted to Fanavaran'; } - // GEN.03 base claim is sent as soon as the local claim exists. - // Vehicle review remains required for damage / attachments / expertise only. + try { + this.assertFanavaranVehicleReviewApproved(claimCase); + } catch { + return 'کارشناس باید پیش از ارسال پرونده به فناوران، مشخصات خودرو را بررسی و تأیید کند.'; + } const clientKey = resolveFanavaranClientKey(); const notifierAllowed = @@ -8071,7 +8074,7 @@ export class ClaimRequestManagementService { ); } if (!existing) throw new NotFoundException('Claim case not found'); - // Vehicle review is not required for GEN.03 base — only for later stages. + this.assertFanavaranVehicleReviewApproved(existing); const notifierAllowed = clientKey === 'tejaratno' ? await this.tejaratNotifierAllowedIds(clientKey) diff --git a/src/expert-claim/expert-claim.service.ts b/src/expert-claim/expert-claim.service.ts index 85619fd..6ebc141 100644 --- a/src/expert-claim/expert-claim.service.ts +++ b/src/expert-claim/expert-claim.service.ts @@ -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 ?? {}), }, { diff --git a/src/expert-claim/fanavaran-stage-fields.spec.ts b/src/expert-claim/fanavaran-stage-fields.spec.ts index d9d945e..ce76de8 100644 --- a/src/expert-claim/fanavaran-stage-fields.spec.ts +++ b/src/expert-claim/fanavaran-stage-fields.spec.ts @@ -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" }), diff --git a/src/expert-claim/fanavaran-vehicle-review.spec.ts b/src/expert-claim/fanavaran-vehicle-review.spec.ts index 8d8259b..313992c 100644 --- a/src/expert-claim/fanavaran-vehicle-review.spec.ts +++ b/src/expert-claim/fanavaran-vehicle-review.spec.ts @@ -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,