automatiacally previous stages submitted

This commit is contained in:
2026-11-11 18:36:10 +03:30
parent 84b92eb55a
commit 8b9dabc9f2
8 changed files with 437 additions and 23 deletions
@@ -3382,7 +3382,12 @@ export class ClaimRequestManagementService {
actor: { sub: string; role?: string; clientKey?: string; username?: string },
) {
if (!Types.ObjectId.isValid(claimCaseId)) {
throw new BadRequestException('Invalid claim case ID');
throw new BadRequestException({
statusCode: 400,
code: 'FANAVARAN_LOCAL_CLAIM_ID_INVALID',
field: 'claimCaseId',
message: 'شناسه پرونده باید شناسه داخلی ۲۴ کاراکتری باشد؛ کد عددی پرونده فناوران را در این بخش وارد نکنید.',
});
}
const claim = await this.claimCaseDbService.findById(claimCaseId);
if (!claim) throw new NotFoundException('Claim case not found');
@@ -3398,14 +3403,23 @@ export class ClaimRequestManagementService {
} else if (actor.role !== RoleEnum.SUPER_ADMIN) {
assertClaimCaseForTenant(claim, actor);
}
if ((await this.resolveFanavaranClaimProduct(claimCaseId)) !== 'car-body') {
throw new BadRequestException('This endpoint only retrieves vehicle-hull claims');
const product = await this.resolveFanavaranClaimProduct(claimCaseId);
if (product !== 'car-body') {
throw new BadRequestException({
statusCode: 400, code: 'FANAVARAN_HULL_CLAIM_REQUIRED',
actualProduct: product, expectedProduct: 'car-body', claimCaseId,
message: 'این پرونده به‌عنوان بیمه شخص ثالث شناسایی شده است؛ مسیر بازیابی انتخاب‌شده فقط برای پرونده‌های بیمه بدنه است.',
});
}
const claimId = parseFanavaranId(
claim.claimId ?? claim.fanavaranSync?.baseClaim?.claimId,
);
if (claimId == null) {
throw new BadRequestException('No submitted Fanavaran claimId is stored for this claim');
throw new BadRequestException({
statusCode: 400, code: 'FANAVARAN_HULL_CLAIM_NOT_SUBMITTED',
field: 'claimId',
message: 'کد پرونده فناوران در این پرونده ثبت نشده است؛ ابتدا ارسال اولیه پرونده را تکمیل کنید.',
});
}
return {
claimId,
@@ -6107,19 +6121,103 @@ export class ClaimRequestManagementService {
};
}
/** Complete applicable stages in order; do not treat skipped images as done. */
private async completeFanavaranExpertisePrerequisites(
claimCaseId: string,
clientKey: FanavaranClientKey,
auditSource: FanavaranAuditSource,
): Promise<void> {
let stage: 'baseClaim' | 'damageCase' | 'attachments' = 'baseClaim';
try {
await this.ensureFanavaranBaseClaim(claimCaseId, clientKey, auditSource);
const product = await this.resolveFanavaranClaimProduct(claimCaseId);
if (fanavaranHasDamageCaseStage(product)) {
stage = 'damageCase';
await this.ensureFanavaranDamageCase(claimCaseId, clientKey, auditSource);
}
stage = 'attachments';
await this.ensureFanavaranAttachmentsComplete(claimCaseId, clientKey);
} catch (error) {
const exception = toFanavaranHttpException(error);
const body = exception.getResponse();
const details = typeof body === 'object'
? body as Record<string, unknown>
: {};
const messages = {
baseClaim: 'ارسال پرونده اولیه به فناوران تکمیل نشد؛ ارسال کارشناسی متوقف شد.',
damageCase: 'ارسال مرحله خسارت به فناوران تکمیل نشد؛ ارسال کارشناسی متوقف شد.',
attachments: 'ارسال همه تصاویر به فناوران تکمیل نشده است؛ پس از رفع خطای تصاویر، ارسال کارشناسی را دوباره انجام دهید.',
};
throw new HttpException(
{
...details,
statusCode: exception.getStatus(),
code: details.code ?? 'FANAVARAN_PREVIOUS_STAGE_FAILED',
stage,
message: messages[stage],
upstreamMessage: this.extractFanavaranErrorMessage(error),
},
exception.getStatus(),
);
}
}
private async ensureFanavaranAttachmentsComplete(
claimCaseId: string,
clientKey: FanavaranClientKey,
): Promise<void> {
const initial = await this.collectFanavaranAttachmentCandidates(
claimCaseId, clientKey,
);
if (initial.candidates.every(candidate => candidate.alreadySubmitted)) return;
const upload = await this.submitFanavaranAttachmentsV2(
claimCaseId, clientKey,
);
// Re-read saved receipts: a success response or a stage-level status alone is insufficient.
const refreshed = await this.collectFanavaranAttachmentCandidates(
claimCaseId, clientKey,
);
const pending = refreshed.candidates.filter(candidate => !candidate.alreadySubmitted);
if (!pending.length) return;
const results = Array.isArray(upload) ? upload : upload?.results ?? [];
const attempts = fanavaranAttachmentStatus(
refreshed.claimCase.fanavaranSync?.attachments,
)?.attempts ?? [];
throw new BadRequestException({
statusCode: 400,
code: 'FANAVARAN_ATTACHMENTS_INCOMPLETE',
stage: 'attachments',
message: 'ارسال تصاویر به فناوران کامل نشده است.',
pendingFiles: pending.map(candidate => {
const result = results.find((item: { fileName?: string }) => item.fileName === candidate.fileName);
const attempt = attempts.filter(item => item.fileName === candidate.fileName).at(-1);
return {
fileName: candidate.fileName, source: candidate.source,
error: result?.warning ?? result?.skipReason ?? attempt?.error ?? upload?.warning ?? 'No confirmed Fanavaran file ID is stored.',
skipped: result?.skipped ?? (attempt?.status === 'skipped'),
};
}),
});
}
async submitFanavaranExpertiseV2(
claimCaseId: string,
clientKey: FanavaranClientKey,
bodyOverride?: Record<string, unknown>,
): Promise<any> {
try {
await this.ensureFanavaranDamageCase(
await this.completeFanavaranExpertisePrerequisites(
claimCaseId,
clientKey,
FanavaranAuditSource.SUBMIT,
);
const claimCase = await this.claimCaseDbService.findById(claimCaseId);
if (!claimCase) throw new NotFoundException('Claim case not found');
if (claimCase.expertiseId != null) {
return claimCase.fanavaranSync?.expertise?.response ?? { Id: claimCase.expertiseId };
}
const product = await this.resolveFanavaranClaimProduct(claimCaseId);
const built = await this.buildFanavaranExpertisePayload({
claimCase,
@@ -6173,6 +6271,8 @@ export class ClaimRequestManagementService {
'Fanavaran claimId is required before submitting expertise',
);
}
// The automatic path also uses this method; keep the same attachment prerequisite.
await this.ensureFanavaranAttachmentsComplete(claimCaseId, clientKey);
if (claimCase.expertiseId != null) {
this.logger.log(
`[executeFanavaranExpertiseSubmit] Already have expertiseId=${claimCase.expertiseId}; skipping Fanavaran POST`,