From 599c88d34ad1cb9cb5935121c5304424161e358f Mon Sep 17 00:00:00 2001 From: Gitead Date: Tue, 1 Dec 2026 10:26:24 -0500 Subject: [PATCH] fix: Claim Again updates the original claim so the new claim number replaces the old one Resubmitting via Claim Again now PUTs to the source claim (old number kept in remarks, unpaid payment billed amount resynced) instead of creating a duplicate claim and payment. Preauth sources and preauth requests still create a new record. Co-Authored-By: Claude Opus 5.5 --- apps/Backend/src/routes/claims.ts | 30 +++++++++++++++++++++++-- apps/Frontend/src/pages/claims-page.tsx | 19 ++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/apps/Backend/src/routes/claims.ts b/apps/Backend/src/routes/claims.ts index ff6d029d..f485dcdf 100755 --- a/apps/Backend/src/routes/claims.ts +++ b/apps/Backend/src/routes/claims.ts @@ -1614,10 +1614,36 @@ router.put("/:id", async (req: Request, res: Response): Promise => { ...(req.body.claimNumber != null ? { claimNumber: req.body.claimNumber } : {}), ...(req.body.npiProviderId ? { npiProviderId: Number(req.body.npiProviderId) } : {}), }); + // Claim Again resubmits this claim: the submit worker writes the new claim number here, + // so keep the old number in the remarks for reference. + const isClaimAgain = req.query.claimAgain === "true"; + const oldClaimNumber = String(existingClaim.claimNumber ?? "").trim(); + if (isClaimAgain && oldClaimNumber) { + const note = `Previous claim #: ${oldClaimNumber}`; + const remarks = String(claimData.remarks ?? ""); + if (!remarks.includes(note)) claimData.remarks = remarks ? `${remarks}\n${note}` : note; + } + const updatedClaim = await storage.updateClaim(claimId, claimData); - // Link any AppointmentFile records saved via procedures form (dedup by filename) - await autoLinkAppointmentFiles(claimId, updatedClaim.appointmentId); + // Claim Again: keep the unpaid payment's billed amount in line with the resubmitted lines + if (isClaimAgain && Array.isArray(req.body.serviceLines) && req.body.serviceLines.length > 0) { + const totalBilled = req.body.serviceLines.reduce( + (sum: number, l: InputServiceLine) => sum + (Number(l.totalBilled) || 0), + 0 + ); + const { prisma: db } = await import("@repo/db/client"); + await db.payment.updateMany({ + where: { claimId, totalPaid: 0 }, + data: { totalBilled: new Decimal(totalBilled), totalDue: new Decimal(totalBilled) }, + }); + } + + // Link any AppointmentFile records saved via procedures form (dedup by filename). + // Not for Claim Again: its attachment list is final, so a removed file stays removed. + if (!isClaimAgain) { + await autoLinkAppointmentFiles(claimId, updatedClaim.appointmentId); + } // Propagate provider change to the linked payment so both stay in sync if (req.body.npiProviderId) { diff --git a/apps/Frontend/src/pages/claims-page.tsx b/apps/Frontend/src/pages/claims-page.tsx index 35bd0778..7f51c40d 100755 --- a/apps/Frontend/src/pages/claims-page.tsx +++ b/apps/Frontend/src/pages/claims-page.tsx @@ -494,6 +494,25 @@ export default function ClaimsPage() { const handleClaimSubmit = async (claimData: any): Promise => { const { isDraft, claimId, uploadedFiles: _uf, ...cleanData } = claimData; + // Claim Again resubmits the same visit: update the original claim (and its payment) in + // place, so the new claim number replaces the old one instead of creating a duplicate. + // A preauth source, or a preauth request, still creates a separate record. + const againSrc = claimAgainSource; + if ( + !claimId && + !isDraft && + againSrc?.id && + cleanData.status !== "PREAUTH" && + !["PREAUTH", "VOID", "CANCELLED"].includes(String(againSrc.status)) + ) { + const res = await apiRequest("PUT", `/api/claims/${againSrc.id}?claimAgain=true`, cleanData); + const data = await res.json(); + if (!res.ok) throw new Error(data?.message || "Failed to update claim"); + queryClient.invalidateQueries({ queryKey: QK_CLAIMS_BASE }); + advanceAiClaimQueue(); + return data; + } + if (claimId) { // Update existing saved claim (PUT never creates a Payment) const res = await apiRequest("PUT", `/api/claims/${claimId}`, cleanData);