1
0
forked from Yara724/api

Fixed mismatched userId on field expert for claim

This commit is contained in:
SepehrYahyaee
2026-06-20 11:53:18 +03:30
parent d355771518
commit 5a89a0ff16
26 changed files with 144 additions and 414 deletions

View File

@@ -71,6 +71,7 @@ import {
} from "src/helpers/user-access-resolver";
import {
blameDamagedPartyMatchesUser,
resolveClaimOwnerFieldsFromBlame,
resolveClaimOwnerParty,
resolveDamagedPartyUserId,
} from "src/helpers/blame-damaged-party";
@@ -4672,7 +4673,7 @@ export class ClaimRequestManagementService {
/**
* V2: Field expert creates a claim from an expert-initiated IN_PERSON completed blame.
* The claim owner is set to the damaged party (first for CAR_BODY, non-guilty for THIRD_PARTY).
* Owner: damaged party `userId` (SMS / user actions), guilty party `clientId` (insurer queue).
*/
async createClaimFromBlameForExpertV2(
blameRequestId: string,
@@ -4702,29 +4703,11 @@ export class ClaimRequestManagementService {
"Only the field expert who created this blame file can create the claim.",
);
}
const parties = blameRequest.parties || [];
const isCarBody = blameRequest.type === BlameRequestType.CAR_BODY;
let damagedUserId: string;
if (isCarBody) {
if (parties.length < 1)
throw new BadRequestException("Blame request has no party");
const first = parties.find((p) => p.role === "FIRST");
if (!first?.person?.userId)
throw new BadRequestException("First party has no userId");
damagedUserId = String(first.person.userId);
} else {
const guiltyPartyId = blameRequest.expert?.decision?.guiltyPartyId
? String(blameRequest.expert.decision.guiltyPartyId)
: null;
if (!guiltyPartyId)
throw new BadRequestException("Blame request has no guilty party set");
const damagedParty = parties.find(
(p) => p.person?.userId && String(p.person.userId) !== guiltyPartyId,
const ownerFields = resolveClaimOwnerFieldsFromBlame(blameRequest);
if (!ownerFields) {
throw new BadRequestException(
"Could not resolve claim owner (damaged party) from blame",
);
if (!damagedParty?.person?.userId) {
throw new BadRequestException("Could not determine damaged party");
}
damagedUserId = String(damagedParty.person.userId);
}
const existingClaim = await this.claimCaseDbService.findOne({
blameRequestId: new Types.ObjectId(blameRequestId),
@@ -4733,12 +4716,6 @@ export class ClaimRequestManagementService {
throw new ConflictException("A claim for this blame case already exists");
}
const claimNo = await this.generateUniqueClaimNumber();
const ownerParty = resolveClaimOwnerParty(blameRequest);
if (!ownerParty?.person?.userId) {
throw new BadRequestException(
"Could not resolve claim owner (guilty party) from blame",
);
}
const newClaim = await this.claimCaseDbService.create({
requestNo: claimNo,
publicId: blameRequest.publicId,
@@ -4754,15 +4731,14 @@ export class ClaimRequestManagementService {
completedSteps: [ClaimWorkflowStep.CLAIM_CREATED],
locked: false,
},
damagedPartyUserId: new Types.ObjectId(damagedUserId),
damagedPartyUserId: new Types.ObjectId(ownerFields.userId),
owner: {
userId: new Types.ObjectId(String(ownerParty.person.userId)),
userRole: ownerParty.role as any,
...(ownerParty.person.clientId
userId: new Types.ObjectId(ownerFields.userId),
userRole: ownerFields.userRole as any,
...(ownerFields.clientId
? {
clientId: new Types.ObjectId(
String(ownerParty.person.clientId),
),
clientId: new Types.ObjectId(ownerFields.clientId),
userClientKey: ownerFields.clientId,
}
: {}),
},
@@ -5020,26 +4996,11 @@ export class ClaimRequestManagementService {
"Only the registrar who created this blame file can create the claim.",
);
}
const parties = blameRequest.parties || [];
const isCarBody = blameRequest.type === BlameRequestType.CAR_BODY;
let damagedUserId: string;
if (isCarBody) {
const first = parties.find((p) => p.role === "FIRST");
if (!first?.person?.userId)
throw new BadRequestException("First party has no userId");
damagedUserId = String(first.person.userId);
} else {
const guiltyPartyId = blameRequest.expert?.decision?.guiltyPartyId
? String(blameRequest.expert.decision.guiltyPartyId)
: null;
if (!guiltyPartyId)
throw new BadRequestException("Blame request has no guilty party set");
const damagedParty = parties.find(
(p) => p.person?.userId && String(p.person.userId) !== guiltyPartyId,
const ownerFields = resolveClaimOwnerFieldsFromBlame(blameRequest);
if (!ownerFields) {
throw new BadRequestException(
"Could not resolve claim owner (damaged party) from blame",
);
if (!damagedParty?.person?.userId)
throw new BadRequestException("Could not determine damaged party");
damagedUserId = String(damagedParty.person.userId);
}
const existingClaim = await this.claimCaseDbService.findOne({
blameRequestId: new Types.ObjectId(blameRequestId),
@@ -5047,12 +5008,6 @@ export class ClaimRequestManagementService {
if (existingClaim)
throw new ConflictException("A claim for this blame case already exists");
const claimNo = await this.generateUniqueClaimNumber();
const ownerParty = resolveClaimOwnerParty(blameRequest);
if (!ownerParty?.person?.userId) {
throw new BadRequestException(
"Could not resolve claim owner (guilty party) from blame",
);
}
const newClaim = await this.claimCaseDbService.create({
requestNo: claimNo,
publicId: blameRequest.publicId,
@@ -5067,15 +5022,14 @@ export class ClaimRequestManagementService {
completedSteps: [ClaimWorkflowStep.CLAIM_CREATED],
locked: false,
},
damagedPartyUserId: new Types.ObjectId(damagedUserId),
damagedPartyUserId: new Types.ObjectId(ownerFields.userId),
owner: {
userId: new Types.ObjectId(String(ownerParty.person.userId)),
userRole: ownerParty.role as any,
...(ownerParty.person.clientId
userId: new Types.ObjectId(ownerFields.userId),
userRole: ownerFields.userRole as any,
...(ownerFields.clientId
? {
clientId: new Types.ObjectId(
String(ownerParty.person.clientId),
),
clientId: new Types.ObjectId(ownerFields.clientId),
userClientKey: ownerFields.clientId,
}
: {}),
},