forked from Yara724/api
Added registrar + fixed sign bug in car-body flow
This commit is contained in:
@@ -2996,25 +2996,130 @@ export class ClaimRequestManagementService {
|
||||
currentUserId: string,
|
||||
actorRole?: string,
|
||||
): Promise<string> {
|
||||
if (actorRole !== RoleEnum.FIELD_EXPERT || !claimCase?.blameRequestId) {
|
||||
if (
|
||||
![RoleEnum.FIELD_EXPERT, RoleEnum.REGISTRAR].includes(actorRole as any) ||
|
||||
!claimCase?.blameRequestId
|
||||
) {
|
||||
return currentUserId;
|
||||
}
|
||||
const blameRequest = await this.blameRequestDbService.findById(
|
||||
claimCase.blameRequestId.toString(),
|
||||
);
|
||||
if (
|
||||
!blameRequest?.expertInitiated ||
|
||||
blameRequest.creationMethod !== "IN_PERSON" ||
|
||||
!blameRequest.initiatedByFieldExpertId
|
||||
) {
|
||||
return currentUserId;
|
||||
}
|
||||
if (String(blameRequest.initiatedByFieldExpertId) !== currentUserId) {
|
||||
return currentUserId;
|
||||
if (!blameRequest || blameRequest.creationMethod !== "IN_PERSON") return currentUserId;
|
||||
if (actorRole === RoleEnum.FIELD_EXPERT) {
|
||||
if (
|
||||
!blameRequest.expertInitiated ||
|
||||
!blameRequest.initiatedByFieldExpertId ||
|
||||
String(blameRequest.initiatedByFieldExpertId) !== currentUserId
|
||||
) {
|
||||
return currentUserId;
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
!blameRequest.registrarInitiated ||
|
||||
!blameRequest.initiatedByRegistrarId ||
|
||||
String(blameRequest.initiatedByRegistrarId) !== currentUserId
|
||||
) {
|
||||
return currentUserId;
|
||||
}
|
||||
}
|
||||
return claimCase.owner?.userId ? String(claimCase.owner.userId) : currentUserId;
|
||||
}
|
||||
|
||||
async createClaimFromBlameForRegistrarV1(
|
||||
blameRequestId: string,
|
||||
registrar: { sub: string; firstName?: string; lastName?: string },
|
||||
): Promise<CreateClaimFromBlameResponseDto> {
|
||||
const blameRequest = await this.blameRequestDbService.findById(blameRequestId);
|
||||
if (!blameRequest) throw new NotFoundException("Blame request not found");
|
||||
if (blameRequest.status !== BlameCaseStatus.COMPLETED) {
|
||||
throw new BadRequestException(
|
||||
`Blame request must be COMPLETED. Current status: ${blameRequest.status}`,
|
||||
);
|
||||
}
|
||||
if (
|
||||
!blameRequest.registrarInitiated ||
|
||||
blameRequest.creationMethod !== "IN_PERSON" ||
|
||||
!blameRequest.initiatedByRegistrarId
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
"This endpoint is only for registrar-initiated IN_PERSON blame files.",
|
||||
);
|
||||
}
|
||||
if (String(blameRequest.initiatedByRegistrarId) !== String(registrar.sub)) {
|
||||
throw new ForbiddenException(
|
||||
"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,
|
||||
);
|
||||
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),
|
||||
});
|
||||
if (existingClaim) throw new ConflictException("A claim for this blame case already exists");
|
||||
const claimNo = await this.generateUniqueClaimNumber();
|
||||
const damagedParty = parties.find(
|
||||
(p) => p.person?.userId && String(p.person.userId) === damagedUserId,
|
||||
);
|
||||
const newClaim = await this.claimCaseDbService.create({
|
||||
requestNo: claimNo,
|
||||
publicId: blameRequest.publicId,
|
||||
blameRequestId: new Types.ObjectId(blameRequestId),
|
||||
blameRequestNo: blameRequest.requestNo,
|
||||
status: ClaimCaseStatus.SELECTING_OUTER_PARTS,
|
||||
claimStatus: ClaimStatus.PENDING,
|
||||
workflow: {
|
||||
currentStep: ClaimWorkflowStep.SELECT_OUTER_PARTS,
|
||||
nextStep: ClaimWorkflowStep.SELECT_OTHER_PARTS,
|
||||
completedSteps: [ClaimWorkflowStep.CLAIM_CREATED],
|
||||
locked: false,
|
||||
},
|
||||
owner: {
|
||||
userId: new Types.ObjectId(damagedUserId),
|
||||
userRole: damagedParty?.role as any,
|
||||
},
|
||||
createdByRegistrarId: new Types.ObjectId(registrar.sub),
|
||||
history: [
|
||||
{
|
||||
type: "CLAIM_CREATED",
|
||||
actor: {
|
||||
actorId: new Types.ObjectId(registrar.sub),
|
||||
actorName: `${registrar.firstName || ""} ${registrar.lastName || ""}`.trim(),
|
||||
actorType: "registrar",
|
||||
},
|
||||
timestamp: new Date(),
|
||||
metadata: {
|
||||
blameRequestId,
|
||||
blamePublicId: blameRequest.publicId,
|
||||
createdByRegistrar: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
} as any);
|
||||
return {
|
||||
claimRequestId: String(newClaim._id),
|
||||
publicId: blameRequest.publicId,
|
||||
status: ClaimCaseStatus.SELECTING_OUTER_PARTS,
|
||||
message: "Claim created successfully. Registrar can now fill claim data.",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* V2 API: Select damaged outer car parts for claim (Step 2 of claim workflow)
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user