forked from Yara724/api
feat(claims): add insurer review referrals
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
import {
|
||||
assertReferredReviewer,
|
||||
isOriginalFileMaker,
|
||||
isReferredReviewer,
|
||||
reviewReferralActorFilter,
|
||||
} from "./review-referral/review-referral";
|
||||
|
||||
import { hasReviewedTejaratNotifier } from "src/fanavaran/tejarat-notifier";
|
||||
import { resolveFanavaranClientKey } from "src/core/config/fanavaran-client.config";
|
||||
|
||||
@@ -322,6 +329,10 @@ export class ExpertClaimService {
|
||||
claim: any,
|
||||
actor: any,
|
||||
): Promise<void> {
|
||||
if (claim.reviewReferral) {
|
||||
assertReferredReviewer(claim, actor);
|
||||
return;
|
||||
}
|
||||
const blame = await this.loadBlameForClaim(claim);
|
||||
// FILE_REVIEWER: by the time we reach this method the caller has already
|
||||
// verified that actor.sub === blame.assignedFileReviewerId (Phase 2 of
|
||||
@@ -400,6 +411,8 @@ export class ExpertClaimService {
|
||||
? String((damageExpert as any).branchId)
|
||||
: undefined;
|
||||
if (!actorBranchId) {
|
||||
const referred = claims.filter((claim) => isReferredReviewer(claim, actor));
|
||||
if (referred.length) return referred;
|
||||
throw new ForbiddenException(
|
||||
"DamageExpert account is not assigned to a branch.",
|
||||
);
|
||||
@@ -450,6 +463,7 @@ export class ExpertClaimService {
|
||||
);
|
||||
|
||||
return claims.filter((claim) => {
|
||||
if (claim.reviewReferral) return isReferredReviewer(claim, actor);
|
||||
let caseBranchId = claim?.branchId ? String(claim.branchId) : undefined;
|
||||
if (!caseBranchId && claim?.blameRequestId) {
|
||||
const blame = blameById.get(String(claim.blameRequestId));
|
||||
@@ -1389,7 +1403,7 @@ export class ExpertClaimService {
|
||||
// so they can perform damage assessment. Fall through to the
|
||||
// damage-expert path below; assertExpertActorOnClaim will verify
|
||||
// tenant scope via the reviewer's clientKey.
|
||||
if ((actor as any).role === RoleEnum.FILE_REVIEWER) {
|
||||
if (actor.role === RoleEnum.FILE_REVIEWER && !claim.reviewReferral) {
|
||||
if (!claim.blameRequestId) {
|
||||
throw new BadRequestException({
|
||||
success: false,
|
||||
@@ -1470,7 +1484,8 @@ export class ExpertClaimService {
|
||||
|
||||
const isFieldExpertOwner =
|
||||
(actor as any).role === RoleEnum.FIELD_EXPERT &&
|
||||
(await this.fieldExpertOwnsClaim(claim, actor));
|
||||
(isReferredReviewer(claim, actor) ||
|
||||
(await this.fieldExpertOwnsClaim(claim, actor)));
|
||||
const isFactorValidationLock =
|
||||
claimIsAwaitingExpertFactorValidationV2(claim);
|
||||
const isDamageReviewLock =
|
||||
@@ -1485,6 +1500,7 @@ export class ExpertClaimService {
|
||||
claim.status === ClaimCaseStatus.CANCELLED;
|
||||
const isFieldExpertPortfolioLock =
|
||||
isFieldExpertOwner &&
|
||||
!claim.reviewReferral &&
|
||||
!isTerminalClaim &&
|
||||
!isDamageReviewLock &&
|
||||
!isFactorValidationLock &&
|
||||
@@ -1633,6 +1649,7 @@ export class ExpertClaimService {
|
||||
|
||||
const assignFilter: Record<string, unknown> = {
|
||||
_id: new Types.ObjectId(claimRequestId),
|
||||
"reviewReferral.referredAt": claim.reviewReferral?.referredAt ?? null,
|
||||
$and: [
|
||||
{
|
||||
$or: [
|
||||
@@ -2488,11 +2505,17 @@ export class ExpertClaimService {
|
||||
async getStatusReportBucketsV2(actor: any): Promise<Record<string, number>> {
|
||||
if (actor.role === RoleEnum.FIELD_EXPERT) {
|
||||
const rows = (await this.claimCaseDbService.find(
|
||||
{ initiatedByFieldExpertId: new Types.ObjectId(actor.sub) },
|
||||
{
|
||||
$or: [
|
||||
{ initiatedByFieldExpertId: new Types.ObjectId(actor.sub) },
|
||||
reviewReferralActorFilter(actor),
|
||||
],
|
||||
},
|
||||
{ lean: true },
|
||||
)) as any[];
|
||||
const buckets = initialClaimExpertReportBuckets();
|
||||
for (const doc of rows) {
|
||||
if (doc.reviewReferral && !isReferredReviewer(doc, actor)) continue;
|
||||
const key = claimCaseStatusToReportBucket(String(doc.status ?? ""));
|
||||
buckets.all++;
|
||||
buckets[key] = (buckets[key] ?? 0) + 1;
|
||||
@@ -2520,6 +2543,7 @@ export class ExpertClaimService {
|
||||
expertPortfolioFileIdsFromActivityEvents(activityEvents);
|
||||
|
||||
const orClauses: Record<string, unknown>[] = [
|
||||
reviewReferralActorFilter(actor),
|
||||
{ "workflow.lockedBy.actorId": expertOid },
|
||||
{ "evaluation.damageExpertReply.actorDetail.actorId": expertOid },
|
||||
{ "evaluation.damageExpertReplyFinal.actorDetail.actorId": expertOid },
|
||||
@@ -2544,7 +2568,9 @@ export class ExpertClaimService {
|
||||
const id = String(doc._id ?? "");
|
||||
if (seen.has(id)) continue;
|
||||
if (
|
||||
!claimDocInExpertPortfolio(doc, expertId, activityFileIds, clientKey)
|
||||
doc.reviewReferral && actor.role !== RoleEnum.FILE_MAKER
|
||||
? !isReferredReviewer(doc, actor)
|
||||
: !claimDocInExpertPortfolio(doc, expertId, activityFileIds, clientKey)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
@@ -2630,6 +2656,7 @@ export class ExpertClaimService {
|
||||
)
|
||||
.then((docs) => docs.map((d) => (d as { _id: unknown })._id));
|
||||
const claimOr: Record<string, unknown>[] = [
|
||||
reviewReferralActorFilter(actor),
|
||||
{
|
||||
initiatedByFieldExpertId: expertOid,
|
||||
requiresFileMakerApproval: { $ne: true },
|
||||
@@ -2638,7 +2665,8 @@ export class ExpertClaimService {
|
||||
if (expertBlameIds.length > 0) {
|
||||
claimOr.push({ blameRequestId: { $in: expertBlameIds } });
|
||||
}
|
||||
claims = (await this.claimCaseDbService.find({ $or: claimOr })) as any[];
|
||||
claims = (await this.claimCaseDbService.find({ $or: claimOr }))
|
||||
.filter((c) => !c.reviewReferral || isReferredReviewer(c, actor));
|
||||
} else {
|
||||
const clientKey = requireActorClientKey(actor);
|
||||
const expertId = String(actor.sub);
|
||||
@@ -2651,6 +2679,7 @@ export class ExpertClaimService {
|
||||
const activityFileIds =
|
||||
expertPortfolioFileIdsFromActivityEvents(activityEvents);
|
||||
const orClauses: Record<string, unknown>[] = [
|
||||
reviewReferralActorFilter(actor),
|
||||
{ "workflow.lockedBy.actorId": expertOid },
|
||||
{ "evaluation.damageExpertReply.actorDetail.actorId": expertOid },
|
||||
{ "evaluation.damageExpertReplyFinal.actorDetail.actorId": expertOid },
|
||||
@@ -2672,7 +2701,9 @@ export class ExpertClaimService {
|
||||
const id = String(doc._id ?? "");
|
||||
if (seen.has(id)) continue;
|
||||
if (
|
||||
!claimDocInExpertPortfolio(doc, expertId, activityFileIds, clientKey)
|
||||
doc.reviewReferral && actor.role !== RoleEnum.FILE_MAKER
|
||||
? !isReferredReviewer(doc, actor)
|
||||
: !claimDocInExpertPortfolio(doc, expertId, activityFileIds, clientKey)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
@@ -2866,6 +2897,7 @@ export class ExpertClaimService {
|
||||
|
||||
// Post-filter: remove files that belong to another expert after reconciliation
|
||||
const finalFiltered = filtered.filter((c) => {
|
||||
if (c.reviewReferral) return isReferredReviewer(c, actor);
|
||||
const assignedId = String(c.workflow?.assignedForReviewBy?.actorId ?? "");
|
||||
const lockEnforced =
|
||||
c.workflow?.locked && this.isClaimV2WorkflowLockCurrentlyEnforced(c);
|
||||
@@ -2989,6 +3021,7 @@ export class ExpertClaimService {
|
||||
.then((docs) => docs.map((d) => (d as { _id: unknown })._id));
|
||||
|
||||
const claimOr: Record<string, unknown>[] = [
|
||||
reviewReferralActorFilter(actor),
|
||||
// Direct claim link: exclude V5 claims that require FileMaker approval
|
||||
{
|
||||
initiatedByFieldExpertId: expertOid,
|
||||
@@ -3025,7 +3058,10 @@ export class ExpertClaimService {
|
||||
blames.map((b) => [String(b._id), b]),
|
||||
);
|
||||
|
||||
const list = claims.map((c) => {
|
||||
const visibleClaims = claims.filter(
|
||||
(claim) => !claim.reviewReferral || isReferredReviewer(claim, actor),
|
||||
);
|
||||
const list = visibleClaims.map((c) => {
|
||||
const v = this.vehicleForExpertFromClaimAndBlameMap(c, blameById);
|
||||
const blame = c.blameRequestId
|
||||
? blameById.get(c.blameRequestId.toString())
|
||||
@@ -3093,6 +3129,15 @@ export class ExpertClaimService {
|
||||
const clientKey = requireActorClientKey(actor);
|
||||
const reviewerOid = new Types.ObjectId(actor.sub);
|
||||
|
||||
const referred = await this.claimCaseDbService.find(reviewReferralActorFilter(actor), {
|
||||
lean: true,
|
||||
select: "blameRequestId owner reviewReferral",
|
||||
});
|
||||
const referredBlameIds = referred
|
||||
.filter((claim) => isReferredReviewer(claim, actor) && claim.blameRequestId)
|
||||
.map((claim) => claim.blameRequestId);
|
||||
const referredBlames = new Set(referredBlameIds.map(String));
|
||||
|
||||
// Find V4 and V5 blame files visible to this reviewer
|
||||
const blames = (await this.blameRequestDbService.find(
|
||||
{
|
||||
@@ -3100,6 +3145,7 @@ export class ExpertClaimService {
|
||||
expertInitiated: true,
|
||||
creationMethod: "IN_PERSON",
|
||||
$or: [
|
||||
{ _id: { $in: referredBlameIds } },
|
||||
// Open: sealed by FileMaker, not yet taken by any reviewer
|
||||
{
|
||||
status: "WAITING_FOR_FILE_REVIEWER",
|
||||
@@ -3125,11 +3171,12 @@ export class ExpertClaimService {
|
||||
|
||||
// Scope to this reviewer's insurer via blame party clientId
|
||||
const tenantScopedBlames = blames.filter((b) =>
|
||||
blameCaseTouchesClient(b, clientKey),
|
||||
referredBlames.has(String(b._id)) || blameCaseTouchesClient(b, clientKey),
|
||||
);
|
||||
const scopedBlames = (
|
||||
await Promise.all(
|
||||
tenantScopedBlames.map(async (blame) => {
|
||||
if (referredBlames.has(String(blame._id))) return blame;
|
||||
try {
|
||||
await this.fanavaranLocationService.assertMakerReviewerBranchCompatible(
|
||||
{
|
||||
@@ -3161,7 +3208,10 @@ export class ExpertClaimService {
|
||||
scopedBlames.map((b) => [String(b._id), b]),
|
||||
);
|
||||
|
||||
const list = claims.map((c) => {
|
||||
const visibleClaims = claims.filter(
|
||||
(claim) => !claim.reviewReferral || isReferredReviewer(claim, actor),
|
||||
);
|
||||
const list = visibleClaims.map((c) => {
|
||||
const blame = c.blameRequestId
|
||||
? blameById.get(c.blameRequestId.toString())
|
||||
: undefined;
|
||||
@@ -3171,8 +3221,9 @@ export class ExpertClaimService {
|
||||
c.workflow?.locked && this.isClaimV2WorkflowLockCurrentlyEnforced(c)
|
||||
);
|
||||
const assignedToMe =
|
||||
blame?.assignedFileReviewerId &&
|
||||
String(blame.assignedFileReviewerId) === actor.sub;
|
||||
c.reviewReferral
|
||||
? isReferredReviewer(c, actor)
|
||||
: String(blame?.assignedFileReviewerId ?? "") === actor.sub;
|
||||
return {
|
||||
claimRequestId: c._id.toString(),
|
||||
publicId: c.publicId,
|
||||
@@ -3202,9 +3253,10 @@ export class ExpertClaimService {
|
||||
createdAt: c.createdAt,
|
||||
awaitingFactorValidation: claimIsAwaitingExpertFactorValidationV2(c),
|
||||
needsFileReviewerCompletion:
|
||||
String(blame?.status ?? "") === "WAITING_FOR_FILE_REVIEWER" ||
|
||||
(!!blame?.isMadeByFileMaker &&
|
||||
String(blame?.status ?? "") === "WAITING_FOR_EXPERT"),
|
||||
!c.reviewReferral &&
|
||||
(String(blame?.status ?? "") === "WAITING_FOR_FILE_REVIEWER" ||
|
||||
(!!blame?.isMadeByFileMaker &&
|
||||
String(blame?.status ?? "") === "WAITING_FOR_EXPERT")),
|
||||
assignedToMe: !!assignedToMe,
|
||||
requiresFileMakerApproval: !!(c as any).requiresFileMakerApproval,
|
||||
fileMakerRejectionCount: (c as any).fileMakerRejectionCount ?? 0,
|
||||
@@ -3602,10 +3654,9 @@ export class ExpertClaimService {
|
||||
"workflow.preLockQueueSnapshot": "",
|
||||
};
|
||||
|
||||
const assigneeToPersist = claimWorkflowAssigneeToPersistOnLockExpiry(
|
||||
claim.workflow,
|
||||
claim,
|
||||
);
|
||||
const assigneeToPersist = claim.reviewReferral
|
||||
? claim.workflow?.assignedForReviewBy
|
||||
: claimWorkflowAssigneeToPersistOnLockExpiry(claim.workflow, claim);
|
||||
const expireLockSet: Record<string, unknown> = { "workflow.locked": false };
|
||||
if (assigneeToPersist) {
|
||||
expireLockSet["workflow.assignedForReviewBy"] = assigneeToPersist;
|
||||
@@ -3676,10 +3727,12 @@ export class ExpertClaimService {
|
||||
// FILE_REVIEWER access is validated by the role-specific gate below
|
||||
// (isAssignedToMe || isOpen). Skip the generic tenant-scope assert which
|
||||
// would incorrectly reject them via the initiatedByFieldExpertId path.
|
||||
if (actor.role !== RoleEnum.FILE_REVIEWER) {
|
||||
assertClaimCaseForExpertActor(claim, actor, linkedBlame);
|
||||
if (!claim.reviewReferral) {
|
||||
if (actor.role !== RoleEnum.FILE_REVIEWER) {
|
||||
assertClaimCaseForExpertActor(claim, actor, linkedBlame);
|
||||
}
|
||||
await this.assertActorBranchAccess(claim, actor, linkedBlame);
|
||||
}
|
||||
await this.assertActorBranchAccess(claim, actor, linkedBlame);
|
||||
|
||||
// Variables used both in the gate block and in the detail-build section below
|
||||
const isDamageExpertPhase =
|
||||
@@ -3690,7 +3743,9 @@ export class ExpertClaimService {
|
||||
|
||||
// FileMaker: can view any file they created (both during filing and approval phase).
|
||||
// The list endpoint already filters to V5-only; detail access is scoped by ownership.
|
||||
if (actor.role === RoleEnum.FILE_MAKER) {
|
||||
if (claim.reviewReferral) {
|
||||
if (!isOriginalFileMaker(claim, actor)) assertReferredReviewer(claim, actor);
|
||||
} else if (actor.role === RoleEnum.FILE_MAKER) {
|
||||
const isOwn = claimCaseInitiatedByFieldExpert(claim, actor, linkedBlame);
|
||||
if (!isOwn) {
|
||||
throw new ForbiddenException(
|
||||
|
||||
Reference in New Issue
Block a user