merge upstream

This commit is contained in:
2026-11-21 12:48:06 +03:30
12 changed files with 202 additions and 29 deletions
@@ -9435,7 +9435,8 @@ export class ClaimRequestManagementService {
];
const requiredDocuments = requiredDocsDefinition.map((doc) => {
const docData = (claimCase.requiredDocuments as any)?.get?.(doc.key);
const docData = (claimCase.requiredDocuments as any)?.get?.(doc.key) ??
(claimCase.requiredDocuments as any)?.[doc.key];
return {
key: doc.key,
label_fa: doc.label_fa,
@@ -9673,9 +9674,31 @@ export class ClaimRequestManagementService {
};
}
/**
* V2 API: Upload required document
*/
/** Reuse a confirmed stored document, never an uploaded flag without file evidence. */
private existingDocumentUploadResult(
claim: any,
key: string,
): UploadRequiredDocumentV2ResponseDto | undefined {
const doc =
claim.requiredDocuments?.get?.(key) ?? claim.requiredDocuments?.[key];
if (!doc?.uploaded) return undefined;
const path = typeof doc.filePath === 'string' ? doc.filePath.trim() : '';
const url = typeof doc.fileUrl === 'string' ? doc.fileUrl.trim() : '';
if (!path && !url) return undefined;
return {
claimRequestId: String(claim._id),
documentKey: key,
fileUrl: path ? buildFileLink(path) : url,
alreadyUploaded: true,
allDocumentsUploaded: (claim.workflow?.completedSteps ?? []).includes(
ClaimWorkflowStep.UPLOAD_REQUIRED_DOCUMENTS,
),
currentStep: claim.workflow?.currentStep ?? '',
message: 'تصویر این مدرک قبلاً ثبت شده است؛ می‌توانید ادامه دهید.',
};
}
/** V2 API: Upload required document. Initial-upload retries reuse stored evidence. */
async uploadRequiredDocumentV2(
claimRequestId: string,
body: UploadRequiredDocumentV2Dto,
@@ -9711,6 +9734,14 @@ export class ClaimRequestManagementService {
const step = claimCase.workflow?.currentStep;
const isResendUpload = step === ClaimWorkflowStep.USER_EXPERT_RESEND;
if (
!isResendUpload &&
claimCase.status !== ClaimCaseStatus.WAITING_FOR_USER_RESEND &&
!options?.v3InPersonFlow
) {
const existing = this.existingDocumentUploadResult(claimCase, body.documentKey);
if (existing) return existing;
}
const isOptionalDocument =
body.documentKey === ClaimRequiredDocumentType.ACCIDENT_SKETCH;
const isCapturePhaseDocUpload =
@@ -9805,17 +9836,8 @@ export class ClaimRequestManagementService {
);
}
// Initial flow: each document once. Expert resend and v3 in-person: allow replacement.
if (!isResendUpload && !options?.v3InPersonFlow) {
const existingDoc =
(claimCase.requiredDocuments as any)?.get?.(body.documentKey) ??
(claimCase.requiredDocuments as any)?.[body.documentKey];
if (existingDoc?.uploaded) {
throw new ConflictException(
`Document ${body.documentKey} has already been uploaded`,
);
}
}
// A confirmed initial upload was returned above. Incomplete references may
// be repaired here; expert-requested resends and in-person replacements remain allowed.
const fileUrl = buildFileLink(file.path);
@@ -12166,6 +12188,27 @@ export class ClaimRequestManagementService {
isCapturePhaseDamagedPartyDocKey(body.documentKey) ||
isGuiltyDamageAreaDoc;
// A response may have been lost after the last capture document advanced
// the workflow. Reuse that saved evidence after the usual owner/tenant checks.
if (
isCaptureDoc &&
claimCase.status !== ClaimCaseStatus.WAITING_FOR_USER_RESEND &&
claimCase.workflow?.currentStep !== ClaimWorkflowStep.CAPTURE_PART_DAMAGES &&
this.claimV3StepCompleted(claimCase, ClaimWorkflowStep.CAPTURE_PART_DAMAGES)
) {
const existing = this.existingDocumentUploadResult(claimCase, body.documentKey);
if (existing) {
const effectiveUserId = await this.resolveClaimEffectiveUserId(
claimCase, currentUserId, actor?.role,
);
await this.assertEffectiveUserIsDamagedPartyOnClaim(
claimCase, effectiveUserId, actor as { username?: string },
'Only the claim owner can upload documents',
);
return existing;
}
}
if (isCaptureDoc) {
this.assertV3ClaimPartsPhase(blame);
this.assertV3ClaimWorkflowStep(