forked from Yara724/api
Added refactored version of car-body
This commit is contained in:
@@ -49,6 +49,7 @@ import { ClaimRequiredDocumentType, CarAngle } from "src/Types&Enums/claim-reque
|
||||
import { ClaimStatus } from "src/Types&Enums/claim-request-management/claimStatus.enum";
|
||||
import { ClaimWorkflowStep } from "src/Types&Enums/claim-request-management/claim-workflow-steps.enum";
|
||||
import { CaseStatus as BlameCaseStatus } from "src/Types&Enums/blame-request-management/caseStatus.enum";
|
||||
import { BlameRequestType } from "src/Types&Enums/blame-request-management/blameRequestType.enum";
|
||||
import { ClaimSignDbService } from "./entites/db-service/claim-sign.db.service";
|
||||
import { DamageImageDbService } from "./entites/db-service/damage-image.db.service";
|
||||
import { ClaimFactorsImageDbService } from "./entites/db-service/factor-image.db.service";
|
||||
@@ -556,11 +557,10 @@ export class ClaimRequestManagementService {
|
||||
const isCarBody = claimRequest.blameFile?.type === "CAR_BODY";
|
||||
|
||||
if (isCarBody) {
|
||||
// Check if accident is with another object (not a car)
|
||||
// This can be determined by:
|
||||
// 1. carBodyForm.carBodyForm.car === false (explicitly set to false)
|
||||
// 2. OR no secondPartyCarDetail exists (no second party car)
|
||||
const isAccidentWithOtherObject =
|
||||
// Accident with object (not another car): v2 uses parties[0].carBodyFirstForm.object, v1 uses carBodyForm.carBodyForm.car === false
|
||||
const firstPartyCarBody = claimRequest.blameFile?.parties?.[0]?.carBodyFirstForm;
|
||||
const isAccidentWithOtherObject =
|
||||
(firstPartyCarBody && firstPartyCarBody.object === true) ||
|
||||
claimRequest.blameFile?.carBodyForm?.carBodyForm?.car === false ||
|
||||
!claimRequest.blameFile?.secondPartyDetails?.secondPartyCarDetail;
|
||||
|
||||
@@ -2734,44 +2734,68 @@ export class ClaimRequestManagementService {
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Validate expert decision exists
|
||||
if (!blameRequest.expert?.decision) {
|
||||
throw new BadRequestException(
|
||||
"Cannot create claim until expert has made a decision",
|
||||
);
|
||||
}
|
||||
|
||||
const guiltyPartyId = String(blameRequest.expert.decision.guiltyPartyId);
|
||||
|
||||
// 4. Validate both parties have signed and accepted
|
||||
const parties = blameRequest.parties || [];
|
||||
if (parties.length < 2) {
|
||||
throw new BadRequestException(
|
||||
"Both parties must be present in the blame request",
|
||||
const isCarBody = blameRequest.type === BlameRequestType.CAR_BODY;
|
||||
|
||||
if (isCarBody) {
|
||||
// CAR_BODY: single party, no expert; first party is the damaged one who creates the claim
|
||||
if (parties.length < 1) {
|
||||
throw new BadRequestException("Blame request has no party");
|
||||
}
|
||||
const firstParty = parties.find((p) => p.role === "FIRST");
|
||||
if (!firstParty || String(firstParty.person?.userId) !== currentUserId) {
|
||||
throw new ForbiddenException(
|
||||
"Only the first party (damaged) can create a claim from this CAR_BODY blame request",
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// THIRD_PARTY: require expert decision and both parties signed
|
||||
if (!blameRequest.expert?.decision) {
|
||||
throw new BadRequestException(
|
||||
"Cannot create claim until expert has made a decision",
|
||||
);
|
||||
}
|
||||
|
||||
const guiltyPartyId = String(blameRequest.expert.decision.guiltyPartyId);
|
||||
|
||||
if (parties.length < 2) {
|
||||
throw new BadRequestException(
|
||||
"Both parties must be present in the blame request",
|
||||
);
|
||||
}
|
||||
|
||||
const allPartiesSigned = parties.every(
|
||||
(p) => p.confirmation !== undefined && p.confirmation !== null,
|
||||
);
|
||||
|
||||
if (!allPartiesSigned) {
|
||||
throw new BadRequestException(
|
||||
"Both parties must sign the expert decision before creating a claim",
|
||||
);
|
||||
}
|
||||
|
||||
const allPartiesAccepted = parties.every(
|
||||
(p) => p.confirmation?.accepted === true,
|
||||
);
|
||||
|
||||
if (!allPartiesAccepted) {
|
||||
throw new BadRequestException(
|
||||
"Both parties must accept the expert decision. If rejected, in-person resolution is required.",
|
||||
);
|
||||
}
|
||||
|
||||
const guiltyParty = parties.find((p) => {
|
||||
return String(p.person?.userId) === guiltyPartyId;
|
||||
});
|
||||
|
||||
if (guiltyParty && String(guiltyParty.person?.userId) === currentUserId) {
|
||||
throw new ForbiddenException(
|
||||
"You cannot create a claim as you are the guilty party",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const allPartiesSigned = parties.every(
|
||||
(p) => p.confirmation !== undefined && p.confirmation !== null,
|
||||
);
|
||||
|
||||
if (!allPartiesSigned) {
|
||||
throw new BadRequestException(
|
||||
"Both parties must sign the expert decision before creating a claim",
|
||||
);
|
||||
}
|
||||
|
||||
const allPartiesAccepted = parties.every(
|
||||
(p) => p.confirmation?.accepted === true,
|
||||
);
|
||||
|
||||
if (!allPartiesAccepted) {
|
||||
throw new BadRequestException(
|
||||
"Both parties must accept the expert decision. If rejected, in-person resolution is required.",
|
||||
);
|
||||
}
|
||||
|
||||
// 5. Validate current user is the DAMAGED party (NOT the guilty party)
|
||||
// 5. Validate current user is a party (and for THIRD_PARTY not the guilty one)
|
||||
const currentUserParty = parties.find(
|
||||
(p) => String(p.person?.userId) === currentUserId,
|
||||
);
|
||||
@@ -2782,24 +2806,6 @@ export class ClaimRequestManagementService {
|
||||
);
|
||||
}
|
||||
|
||||
const currentUserIsGuilty = parties.some(
|
||||
(p) =>
|
||||
String(p.person?.userId) === currentUserId &&
|
||||
String((p as any)._id || p.person?.userId) === guiltyPartyId,
|
||||
);
|
||||
|
||||
// Better check: compare guiltyPartyId with person.userId of each party
|
||||
const guiltyParty = parties.find((p) => {
|
||||
// guiltyPartyId could be userId or party identifier
|
||||
return String(p.person?.userId) === guiltyPartyId;
|
||||
});
|
||||
|
||||
if (guiltyParty && String(guiltyParty.person?.userId) === currentUserId) {
|
||||
throw new ForbiddenException(
|
||||
"You cannot create a claim as you are the guilty party",
|
||||
);
|
||||
}
|
||||
|
||||
// 6. Validate no existing claim for this blame
|
||||
const existingClaim = await this.claimCaseDbService.findOne({
|
||||
blameRequestId: new Types.ObjectId(blameRequestId),
|
||||
|
||||
Reference in New Issue
Block a user