diff --git a/apps/Frontend/src/pages/claims-page.tsx b/apps/Frontend/src/pages/claims-page.tsx index f58ee752..5528d15e 100755 --- a/apps/Frontend/src/pages/claims-page.tsx +++ b/apps/Frontend/src/pages/claims-page.tsx @@ -143,10 +143,12 @@ export default function ClaimsPage() { // CCA result: pdfFileId is already saved by the processor — open preview directly if (jobResult.pdfFileId) { advanceAiClaimQueue(); - advanceChatbotClaimQueue(); - setPreviewPdfId(jobResult.pdfFileId); - setPreviewFallbackFilename(jobResult.pdfFilename ?? `cca_claim_${jobResult.claimNumber ?? "unknown"}.pdf`); - setPreviewOpen(true); + const advancedToNext = advanceChatbotClaimQueue(); + if (!advancedToNext) { + setPreviewPdfId(jobResult.pdfFileId); + setPreviewFallbackFilename(jobResult.pdfFilename ?? `cca_claim_${jobResult.claimNumber ?? "unknown"}.pdf`); + setPreviewOpen(true); + } dispatch(setTaskStatus({ key: "claimSubmit", status: "success", @@ -336,7 +338,9 @@ export default function ClaimsPage() { // Advance the chatbot_claim_queue / chatbot_check_claim_queue after selenium PDF is saved. // NOT called from closeClaim — called only after the selenium job completes and PDF is downloaded. - const advanceChatbotClaimQueue = () => { + // Returns true if it navigated on to another queued patient (caller should skip the PDF + // preview popup in that case — the form is about to be replaced by the next patient's claim). + const advanceChatbotClaimQueue = (): boolean => { try { const raw = sessionStorage.getItem("chatbot_check_claim_queue"); if (raw) { @@ -369,7 +373,7 @@ export default function ClaimsPage() { window.dispatchEvent(new CustomEvent("chatbot:eligibility-prefill")); setWouterLocation("/insurance-status"); }, 500); - return; + return true; } sessionStorage.removeItem("chatbot_check_claim_queue"); } @@ -403,11 +407,13 @@ export default function ClaimsPage() { setTimeout(() => { setWouterLocation(`/claims?newPatient=${next.patient.id}`); }, 500); - return; + return true; } sessionStorage.removeItem("chatbot_claim_queue"); } } catch {} + + return false; }; // Advance the ai_claim_queue by removing the first item (current patient). @@ -447,10 +453,13 @@ export default function ClaimsPage() { const data = await res.json(); if (!res.ok) throw new Error(data?.message || "Failed to save claim"); queryClient.invalidateQueries({ queryKey: QK_CLAIMS_BASE }); - if (!isDraft) { - advanceAiClaimQueue(); - advanceChatbotClaimQueue(); - } + // NOTE: advanceChatbotClaimQueue() is NOT called here. This function only creates + // the claim DB record; for selenium-based insurances (MH/CCA/DDMA/Tufts/United) the + // actual submission + PDF happens afterward. Advancing the queue here would move to + // the next patient before the current patient's selenium job (and PDF) finishes. + // The queue is advanced later, once the selenium job completes (see + // handleMHSeleniumPdfDownload and the CCA branch in the job-status useEffect). + if (!isDraft) advanceAiClaimQueue(); return data; }; @@ -964,13 +973,15 @@ export default function ClaimsPage() { duration: isPreAuth ? 10000 : 5000, }); + let advancedToNext = false; if (!isPreAuth) { advanceAiClaimQueue(); - advanceChatbotClaimQueue(); + advancedToNext = advanceChatbotClaimQueue(); } // Pop up the final PDF so the user doesn't need to navigate to Documents - if (result.pdfFileId) { + // (skip if we're about to navigate to the next queued patient's claim) + if (result.pdfFileId && !advancedToNext) { setPreviewPdfId(result.pdfFileId); setPreviewFallbackFilename(result.fileName ?? null); setPreviewOpen(true);