fix: reuse completed operator inquiries throughout v6 workflows

This commit is contained in:
SepehrYahyaee
2026-11-09 19:28:04 +03:30
parent cc0d69dcb2
commit d53922a1bc
3 changed files with 567 additions and 21 deletions
@@ -796,16 +796,58 @@ export class RequestManagementService {
}
}
/** Only a successfully stored operator inquiry can replace FIRST_INITIAL_FORM. */
private hasCompletedCallCenterInquiry(req: any): boolean {
const first = req.parties?.find((party: any) => party.role === PartyRole.FIRST);
return Boolean(
req.callCenterInitiated && first?.person?.clientId && (
req.workflow?.completedSteps?.includes(WorkflowStep.FIRST_INITIAL_FORM) ||
req.history?.some((event: any) =>
event.type === "CALL_CENTER_INQUIRY_COMPLETED" &&
event.metadata?.partyRole === PartyRole.FIRST,
)
),
);
}
/** Repair old V6 records without skipping video or touching the second party. */
private reconcileCallCenterInquiryWorkflow(req: any): boolean {
if (
!this.hasCompletedCallCenterInquiry(req) || !req.workflow ||
req.workflow.locked || ["STOPPED", "CANCELLED", "COMPLETED"].includes(req.status)
) return false;
let changed = false;
if (!req.workflow.completedSteps?.includes(WorkflowStep.FIRST_INITIAL_FORM)) {
this.pushWorkflowSteps(req, [WorkflowStep.FIRST_INITIAL_FORM]);
changed = true;
}
if (req.workflow.currentStep === WorkflowStep.FIRST_INITIAL_FORM) {
const first = req.parties?.find((party: any) => party.role === PartyRole.FIRST);
const videoCompleted = !!first?.evidence?.videoId ||
req.workflow.completedSteps.includes(WorkflowStep.FIRST_VIDEO);
req.workflow.currentStep = videoCompleted
? WorkflowStep.FIRST_LOCATION : WorkflowStep.FIRST_VIDEO;
req.workflow.nextStep = videoCompleted
? WorkflowStep.FIRST_VOICE : WorkflowStep.FIRST_LOCATION;
changed = true;
} else if (
req.workflow.currentStep === WorkflowStep.FIRST_VIDEO &&
req.workflow.nextStep === WorkflowStep.FIRST_INITIAL_FORM
) {
req.workflow.nextStep = WorkflowStep.FIRST_LOCATION;
changed = true;
}
return changed;
}
private async advanceWorkflowToNext(
req: any,
submittedStepKey: WorkflowStep,
) {
// V6 call-center initiated THIRD_PARTY: skip FIRST_INITIAL_FORM because the
// call-center agent already ran the inquiry during run-inquiry.
// After FIRST_VIDEO, jump straight to FIRST_LOCATION.
// V6, both case types: the operator already completed the first inquiry.
if (
req.callCenterInitiated &&
req.type === BlameRequestType.THIRD_PARTY &&
this.hasCompletedCallCenterInquiry(req) &&
submittedStepKey === WorkflowStep.FIRST_VIDEO
) {
const completed = Array.isArray(req.workflow.completedSteps)
@@ -1572,6 +1614,7 @@ export class RequestManagementService {
await this.ensureCarBodyAccidentTypeStepExists();
await this.advanceWorkflowToNext(req, WorkflowStep.CAR_BODY_ACCIDENT_TYPE);
this.reconcileCallCenterInquiryWorkflow(req);
if (!Array.isArray(req.history)) req.history = [];
req.history.push({
@@ -1668,15 +1711,9 @@ export class RequestManagementService {
completed.push(step3Key);
}
// V6 call-center initiated THIRD_PARTY: inquiry already done by agent — skip
// FIRST_INITIAL_FORM and go straight to FIRST_LOCATION.
if (req.callCenterInitiated && req.type === BlameRequestType.THIRD_PARTY) {
if (!completed.includes(WorkflowStep.FIRST_INITIAL_FORM)) {
completed.push(WorkflowStep.FIRST_INITIAL_FORM);
}
req.workflow.completedSteps = completed;
req.workflow.currentStep = WorkflowStep.FIRST_LOCATION;
req.workflow.nextStep = WorkflowStep.FIRST_VOICE;
// Use the same V6 transition for plate/VIN and third-party/car-body files.
if (this.hasCompletedCallCenterInquiry(req)) {
await this.advanceWorkflowToNext(req, step3Key);
} else {
req.workflow.completedSteps = completed;
@@ -2893,7 +2930,7 @@ export class RequestManagementService {
link: this.smsOrchestrationService.buildBlamePartyLink(
token,
"FIRST",
"v1",
req.callCenterInitiated ? "v6" : "v1",
),
}
: null,
@@ -2903,7 +2940,7 @@ export class RequestManagementService {
link: this.smsOrchestrationService.buildBlamePartyLink(
token,
"SECOND",
"v1",
req.callCenterInitiated ? "v6" : "v1",
),
}
: null,
@@ -3079,7 +3116,7 @@ export class RequestManagementService {
const url = this.smsOrchestrationService.buildInviteLink(
frontendRoute,
requestId,
"v1",
req.callCenterInitiated ? "v6" : "v1",
);
await this.smsOrchestrationService.sendInviteLink(
secondPartyPhone,
@@ -3133,7 +3170,7 @@ export class RequestManagementService {
const url = this.smsOrchestrationService.buildInviteLink(
frontendRoute,
requestId,
"v1",
req.callCenterInitiated ? "v6" : "v1",
);
await this.smsOrchestrationService.sendInviteLink(
phoneNumber,
@@ -4744,6 +4781,10 @@ export class RequestManagementService {
if (!isFieldExpertOwner && !isRegistrarOwner && !isParty) {
throw new ForbiddenException("You do not have access to this request");
}
if (this.reconcileCallCenterInquiryWorkflow(req)) {
await (req as any).save();
}
// Optionally bind userId to party when user opens via LINK (phone match, no userId yet)
if (
!isFieldExpertOwner &&
@@ -6469,7 +6510,7 @@ export class RequestManagementService {
targetPhone === request?.parties[0]?.person.phoneNumber
? "FIRST"
: "SECOND",
"v1",
request.callCenterInitiated ? "v6" : "v1",
);
await this.smsOrchestrationService.sendThirdPartyDamagedPartyClaimLinkNotice(
{
@@ -9092,6 +9133,7 @@ export class RequestManagementService {
},
metadata: { partyRole: PartyRole.FIRST },
});
this.reconcileCallCenterInquiryWorkflow(req);
await (req as any).save();
// Return the inquiry-populated data so the agent can review before sending the link.
@@ -9212,6 +9254,7 @@ export class RequestManagementService {
},
metadata: { partyRole: PartyRole.FIRST, inquiryType: "VIN" },
});
this.reconcileCallCenterInquiryWorkflow(req);
await (req as any).save();
const firstPartyAfter = req.parties[firstIdx];
@@ -9316,7 +9359,7 @@ export class RequestManagementService {
// Safeguard: verify that run-inquiry was completed and that the resolved
// insurance company belongs to this deployment before proceeding.
const storedClientId = req.parties[firstIdx]?.person?.clientId;
if (!storedClientId) {
if (!storedClientId || !this.hasCompletedCallCenterInquiry(req)) {
throw new BadRequestException(
"استعلام بیمه طرف مقصر انجام نشده است. ابتدا run-inquiry را فراخوانی کنید.",
);
@@ -9402,6 +9445,8 @@ export class RequestManagementService {
});
}
this.reconcileCallCenterInquiryWorkflow(req);
const expertName = `${agent?.lastName || ""}`.trim() || "اپراتور";
const firstLink = this.smsOrchestrationService.buildBlamePartyLink(
requestId,
@@ -9461,6 +9506,7 @@ export class RequestManagementService {
"You can only access files that you have initiated.",
);
}
if (this.reconcileCallCenterInquiryWorkflow(req)) await (req as any).save();
return {
_id: (req as any)._id,
publicId: (req as any).publicId,
@@ -9510,7 +9556,9 @@ export class RequestManagementService {
type: p.vehicle.type,
}
: undefined,
inquiryComplete: !!p.person?.inquiriesCompleted,
inquiryComplete: p.role === PartyRole.FIRST
? this.hasCompletedCallCenterInquiry(req)
: this.hasPartyInquiriesComplete(req, p.role),
})),
createdAt: (req as any).createdAt,
updatedAt: (req as any).updatedAt,