feat: route user claims by branch and align licence issue dates

Require the damaged party in user flows to select an active insurer branch and preserve branch access rules. Accept older licence issue dates, correct anniversary boundaries, and update Fanavaran audit documentation and regression coverage.
This commit is contained in:
SepehrYahyaee
2026-11-14 12:24:14 +03:30
parent f021793b00
commit a15c54d017
28 changed files with 1403 additions and 42 deletions
@@ -1,3 +1,4 @@
import { UserClaimBranchService, userClaimBranchContext } from "src/client/user-claim-branch.service";
import {
BadRequestException,
ConflictException,
@@ -1144,6 +1145,7 @@ export class RequestManagementService {
private readonly fileMakerDbService: FileMakerDbService,
private readonly fileReviewerDbService: FileReviewerDbService,
private readonly lookupsService: LookupsService,
private readonly userClaimBranchService: UserClaimBranchService,
) {}
async assertFileReviewerBranchAccess(
@@ -4861,6 +4863,7 @@ export class RequestManagementService {
carBodyInsuranceDetail: plain.carBodyInsuranceDetail,
claimCreation,
userSide: viewerParty?.role ?? null,
claimBranch: userClaimBranchContext(plain, String(user?.sub ?? "")),
nextAction: resolveBlameUserNextAction(
plain.status,
viewerParty,
@@ -6341,6 +6344,41 @@ export class RequestManagementService {
};
}
/** Same access boundary as the user case detail; never accept an insurer id from the browser. */
async getUserClaimBranches(requestId: string, user: any) {
await this.getBlameRequestV2(requestId, user);
const req = await this.blameRequestDbService.findById(requestId);
if (!req) throw new NotFoundException("پرونده پیدا نشد.");
const context = userClaimBranchContext(req, String(user.sub));
if (!context.required) return { ...context, branches: [], locked: true };
const claim = await this.claimCaseDbService.findOne({ blameRequestId: req._id });
return {
...context,
branchId: claim?.branchId ? String(claim.branchId) : context.branchId,
locked: !!claim,
branches: await this.userClaimBranchService.options(req),
};
}
/** Also supports already-signed legacy cases, before their first claim is created. */
async selectUserClaimBranch(requestId: string, user: any, branchId: unknown) {
await this.getBlameRequestV2(requestId, user);
const req = await this.blameRequestDbService.findById(requestId);
if (!req) throw new NotFoundException("پرونده پیدا نشد.");
if (!userClaimBranchContext(req, String(user.sub)).required) {
throw new ForbiddenException("فقط طرف زیان‌دیده می‌تواند شعبه رسیدگی به خسارت را انتخاب کند.");
}
if (![CaseStatus.WAITING_FOR_SIGNATURES, CaseStatus.COMPLETED].includes(req.status)) {
throw new BadRequestException("انتخاب شعبه در مرحله امضا یا پیش از ثبت درخواست خسارت امکان‌پذیر است.");
}
if (await this.claimCaseDbService.findOne({ blameRequestId: req._id })) {
throw new ConflictException("درخواست خسارت ایجاد شده است و شعبه آن قابل تغییر نیست.");
}
const id = await this.userClaimBranchService.validate(req, branchId);
await this.blameRequestDbService.findByIdAndUpdate(requestId, { $set: { branchId: new Types.ObjectId(id) } });
return { branchId: id };
}
/**
* V2: User signs and accepts/rejects (expert decision for THIRD_PARTY, or final acknowledgment for CAR_BODY).
*/
@@ -6349,6 +6387,7 @@ export class RequestManagementService {
userId: string,
isAccept: boolean,
signFile: Express.Multer.File,
branchId?: string,
): Promise<UserSignatureResponseDto> {
if (!signFile) {
throw new BadRequestException("A signature file is required");
@@ -6393,6 +6432,17 @@ export class RequestManagementService {
throw new BadRequestException("You have already signed this decision");
}
const branchContext = userClaimBranchContext(request, userId);
let selectedBranchId: string | undefined;
if (branchId && !branchContext.required) {
throw new ForbiddenException("انتخاب شعبه فقط برای طرف زیان‌دیده در پرونده‌های کاربرمحور مجاز است.");
}
if (isAccept && branchContext.required) {
selectedBranchId = await this.userClaimBranchService.validate(
request, branchId ?? branchContext.branchId,
);
}
// Create signature document
const signDoc = await this.userSign.create({
fileName: signFile.filename,
@@ -6449,6 +6499,7 @@ export class RequestManagementService {
await this.blameRequestDbService.findByIdAndUpdate(requestId, {
$set: {
[`parties.${partyIndex}.confirmation`]: confirmationPayload,
...(selectedBranchId ? { branchId: new Types.ObjectId(selectedBranchId) } : {}),
},
});