Fixed insurer

This commit is contained in:
SepehrYahyaee
2026-06-07 16:10:42 +03:30
parent 34142942e5
commit 249c359898
6 changed files with 184 additions and 121 deletions

View File

@@ -5156,7 +5156,6 @@ export class ClaimRequestManagementService {
): Promise<UploadRequiredDocumentV2ResponseDto> {
try {
const claimCase = await this.claimCaseDbService.findById(claimRequestId);
if (!claimCase) {
throw new NotFoundException(
`Claim case with ID ${claimRequestId} not found`,
@@ -5214,6 +5213,7 @@ export class ClaimRequestManagementService {
);
}
}
if (isResendUpload) {
if (claimCase.status !== ClaimCaseStatus.WAITING_FOR_USER_RESEND) {
throw new BadRequestException(
@@ -5262,7 +5262,6 @@ export class ClaimRequestManagementService {
const fileUrl = buildFileLink(file.path);
// Create document reference in claim-required-documents collection
const docRef = await this.claimRequiredDocumentDbService.create({
path: file.path,
fileName: file.filename,
@@ -5271,8 +5270,24 @@ export class ClaimRequestManagementService {
uploadedAt: new Date(),
});
// Update claim case
const updateData: any = {
// Base history entry for this upload
const uploadHistoryEntry = {
type: "DOCUMENT_UPLOADED",
actor: {
actorId: new Types.ObjectId(currentUserId),
actorName: claimCase.owner?.fullName || "User",
actorType: "user",
},
timestamp: new Date(),
metadata: {
documentKey: body.documentKey,
fileName: file.filename,
fileId: docRef._id.toString(),
},
};
// Base $set — always applied
const $set: Record<string, unknown> = {
[`requiredDocuments.${body.documentKey}`]: {
fileId: docRef._id,
filePath: file.path,
@@ -5280,33 +5295,16 @@ export class ClaimRequestManagementService {
uploaded: true,
uploadedAt: new Date(),
},
$push: {
history: {
type: "DOCUMENT_UPLOADED",
actor: {
actorId: new Types.ObjectId(currentUserId),
actorName: claimCase.owner?.fullName || "User",
actorType: "user",
},
timestamp: new Date(),
metadata: {
documentKey: body.documentKey,
fileName: file.filename,
fileId: docRef._id.toString(),
},
},
},
};
// History entries to push — always at least the upload entry
const historyEntries: unknown[] = [uploadHistoryEntry];
// completedSteps entries to push
const completedStepsEntries: string[] = [];
let allDocumentsUploaded = false;
let remaining = 0;
// Will be true when this upload also completes capture-phase docs AND
// every angle + every selected damaged part has already been captured.
// In that case we advance the workflow exactly like `capturePartV2` does
// when the user finishes the last capture — otherwise the user would be
// stuck in CAPTURE_PART_DAMAGES with all captures done but no more
// captures to upload to trigger the transition.
let captureStepCompletedOnThisUpload = false;
if (!isResendUpload) {
@@ -5317,7 +5315,6 @@ export class ClaimRequestManagementService {
remaining = CAPTURE_PHASE_DAMAGED_PARTY_DOC_KEYS.filter(
(k) => !afterThis(k),
).length;
allDocumentsUploaded = false;
if (remaining === 0) {
const progressAfterDoc = getClaimCaptureProgress(claimCase, {
@@ -5330,31 +5327,29 @@ export class ClaimRequestManagementService {
progressAfterDoc.capturePhaseDocsComplete
) {
captureStepCompletedOnThisUpload = true;
updateData["status"] =
ClaimCaseStatus.UPLOADING_REQUIRED_DOCUMENTS;
updateData["workflow.currentStep"] =
$set["status"] = ClaimCaseStatus.UPLOADING_REQUIRED_DOCUMENTS;
$set["workflow.currentStep"] =
ClaimWorkflowStep.UPLOAD_REQUIRED_DOCUMENTS;
updateData["workflow.nextStep"] =
$set["workflow.nextStep"] =
ClaimWorkflowStep.USER_SUBMISSION_COMPLETE;
updateData.$push.history = [
updateData.$push.history,
{
type: "STEP_COMPLETED",
actor: {
actorId: new Types.ObjectId(currentUserId),
actorName: claimCase.owner?.fullName || "User",
actorType: "user",
},
timestamp: new Date(),
metadata: {
stepKey: ClaimWorkflowStep.CAPTURE_PART_DAMAGES,
description:
"Angles, damaged parts, and capture-phase vehicle evidence (chassis, engine, metal plate) are complete. Please upload remaining required documents.",
},
completedStepsEntries.push(
ClaimWorkflowStep.CAPTURE_PART_DAMAGES,
);
historyEntries.push({
type: "STEP_COMPLETED",
actor: {
actorId: new Types.ObjectId(currentUserId),
actorName: claimCase.owner?.fullName || "User",
actorType: "user",
},
];
updateData.$push["workflow.completedSteps"] =
ClaimWorkflowStep.CAPTURE_PART_DAMAGES;
timestamp: new Date(),
metadata: {
stepKey: ClaimWorkflowStep.CAPTURE_PART_DAMAGES,
description:
"Angles, damaged parts, and capture-phase vehicle evidence (chassis, engine, metal plate) are complete. Please upload remaining required documents.",
},
});
}
}
} else {
@@ -5370,14 +5365,17 @@ export class ClaimRequestManagementService {
);
if (allDocumentsUploaded) {
updateData["status"] = ClaimCaseStatus.WAITING_FOR_DAMAGE_EXPERT;
updateData["claimStatus"] = ClaimStatus.PENDING;
updateData["workflow.currentStep"] =
$set["status"] = ClaimCaseStatus.WAITING_FOR_DAMAGE_EXPERT;
$set["claimStatus"] = ClaimStatus.PENDING;
$set["workflow.currentStep"] =
ClaimWorkflowStep.USER_SUBMISSION_COMPLETE;
updateData["workflow.nextStep"] =
$set["workflow.nextStep"] =
ClaimWorkflowStep.EXPERT_DAMAGE_ASSESSMENT;
updateData.$push.history = [
updateData.$push.history,
completedStepsEntries.push(
ClaimWorkflowStep.UPLOAD_REQUIRED_DOCUMENTS,
);
historyEntries.push(
{
type: "STEP_COMPLETED",
actor: {
@@ -5405,16 +5403,30 @@ export class ClaimRequestManagementService {
"User submission complete. Claim ready for damage expert review.",
},
},
];
updateData.$push["workflow.completedSteps"] =
ClaimWorkflowStep.UPLOAD_REQUIRED_DOCUMENTS;
);
}
}
}
// Build the final MongoDB update — all operators explicit and clean
const updatePayload: Record<string, unknown> = { $set };
const $push: Record<string, unknown> = {
history:
historyEntries.length === 1
? historyEntries[0]
: { $each: historyEntries },
};
if (completedStepsEntries.length === 1) {
$push["workflow.completedSteps"] = completedStepsEntries[0];
} else if (completedStepsEntries.length > 1) {
$push["workflow.completedSteps"] = { $each: completedStepsEntries };
}
updatePayload.$push = $push;
await this.claimCaseDbService.findByIdAndUpdate(
claimRequestId,
updateData,
updatePayload,
);
if (isResendUpload) {