fix: stop premature chatbot claim queue advance + skip preview popup mid-batch

handleClaimSubmit advanced the chatbot batch claim queue right after creating
the DB claim record, before the Selenium job even ran. This caused only the
last patient in a multi-patient claim batch to get its PDF saved to
Documents — earlier patients' job tracking got overwritten by the next
patient's submission before their PDF download could complete.

Also make advanceChatbotClaimQueue() report whether it navigated to another
queued patient, and skip the auto PDF preview popup in that case so it only
opens for the last/standalone claim.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-29 23:44:21 -04:00
parent 26da3394aa
commit 15a1091f35

View File

@@ -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);