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 <noreply@anthropic.com>
This commit is contained in:
@@ -1614,10 +1614,36 @@ router.put("/:id", async (req: Request, res: Response): Promise<any> => {
|
||||
...(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) {
|
||||
|
||||
@@ -494,6 +494,25 @@ export default function ClaimsPage() {
|
||||
const handleClaimSubmit = async (claimData: any): Promise<Claim> => {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user