From 37a0568b5e0f41a44ed6961742c42efa5caa6302 Mon Sep 17 00:00:00 2001 From: Gitead Date: Sat, 18 Apr 2026 09:53:41 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20MH=20claim=20submit=20=E2=80=94=20wait?= =?UTF-8?q?=20for=20socket=20job:update=20before=20fetching=20PDF=20and=20?= =?UTF-8?q?claim=20number?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/Frontend/src/pages/claims-page.tsx | 63 +++++++++++++++---------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/apps/Frontend/src/pages/claims-page.tsx b/apps/Frontend/src/pages/claims-page.tsx index f9db03a5..c2195db2 100755 --- a/apps/Frontend/src/pages/claims-page.tsx +++ b/apps/Frontend/src/pages/claims-page.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useMemo } from "react"; +import { useState, useEffect, useMemo, useRef } from "react"; import { useMutation } from "@tanstack/react-query"; import { Card, @@ -18,6 +18,7 @@ import { clearTaskStatus, } from "@/redux/slices/seleniumTaskSlice"; import { SeleniumTaskBanner } from "@/components/ui/selenium-task-banner"; +import { useJobStatus } from "@/hooks/use-job-status"; import ClaimsRecentTable, { QK_CLAIMS_BASE, } from "@/components/claims/claims-recent-table"; @@ -49,6 +50,14 @@ export default function ClaimsPage() { const { status, message, show } = useAppSelector( (state) => state.seleniumTasks.claimSubmit ); + + // Track pending selenium jobs so we can react to completion via socket + const [pendingClaimJobId, setPendingClaimJobId] = useState(null); + const pendingClaimMeta = useRef<{ + patientId: number | null; + groupKey: "INSURANCE_CLAIM" | "INSURANCE_CLAIM_PREAUTH"; + }>({ patientId: null, groupKey: "INSURANCE_CLAIM" }); + const { status: jobStatus, result: jobResult } = useJobStatus(pendingClaimJobId); const { toast } = useToast(); const { user } = useAuth(); @@ -130,6 +139,24 @@ export default function ClaimsPage() { }, }); + // React to selenium job completing via socket + useEffect(() => { + if (!pendingClaimJobId) return; + if (jobStatus === "completed" && jobResult) { + setPendingClaimJobId(null); + handleMHSeleniumPdfDownload( + jobResult, + pendingClaimMeta.current.patientId, + pendingClaimMeta.current.groupKey + ); + queryClient.invalidateQueries({ queryKey: ["claims-recent"] }); + } else if (jobStatus === "failed") { + setPendingClaimJobId(null); + dispatch(setTaskStatus({ key: "claimSubmit", status: "error", message: "Selenium job failed" })); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [jobStatus, jobResult, pendingClaimJobId]); + // small helper: remove given query params from the current URL (silent, no reload) const clearUrlParams = (params: string[]) => { try { @@ -331,31 +358,23 @@ export default function ClaimsPage() { const result1 = await response.json(); if (result1.error) throw new Error(result1.error); - if (result1.claimNumber) { - await queryClient.invalidateQueries({ queryKey: ["claims-recent"] }); - } + // Job is queued — wait for socket completion before downloading PDF + pendingClaimMeta.current = { patientId: selectedPatientId, groupKey: "INSURANCE_CLAIM" }; + setPendingClaimJobId(result1.jobId); dispatch( setTaskStatus({ key: "claimSubmit", status: "pending", - message: "Submitted to Selenium. Awaiting PDF...", + message: "Submitted to Selenium. Awaiting result...", }) ); toast({ title: "Selenium service notified", - description: - "Your claim data was successfully sent to Selenium, Waitinig for its response.", + description: "Claim submitted to Selenium. Waiting for confirmation...", variant: "default", }); - - const result2 = await handleMHSeleniumPdfDownload( - result1, - selectedPatientId, - "INSURANCE_CLAIM" - ); - return result2; } catch (error: any) { dispatch( setTaskStatus({ @@ -475,27 +494,23 @@ export default function ClaimsPage() { const result1 = await response.json(); if (result1.error) throw new Error(result1.error); + // Job is queued — wait for socket completion before downloading PDF + pendingClaimMeta.current = { patientId: selectedPatientId, groupKey: "INSURANCE_CLAIM_PREAUTH" }; + setPendingClaimJobId(result1.jobId); + dispatch( setTaskStatus({ key: "claimSubmit", status: "pending", - message: "Submitted to Selenium. Awaiting PDF...", + message: "Submitted to Selenium. Awaiting result...", }) ); toast({ title: "Selenium service notified", - description: - "Your claim pre auth data was successfully sent to Selenium, Waitinig for its response.", + description: "Pre-auth submitted to Selenium. Waiting for confirmation...", variant: "default", }); - - const result2 = await handleMHSeleniumPdfDownload( - result1, - selectedPatientId, - "INSURANCE_CLAIM_PREAUTH" - ); - return result2; } catch (error: any) { dispatch( setTaskStatus({