feat: DDMA claim submission with OTP, PDF, claim number extraction

- Add full DDMA claim Selenium flow (steps 1-8): search patient, open
  member page, create claim, fill form, attach files, next, submit,
  extract claim number and save confirmation PDF
- Add fee schedule price-mismatch dialog for all claim buttons (MH,
  CCA, DDMA, United, Tufts, Save) with optional price update to JSON
- Add OTP modal for DDMA claim when session expires, mirroring
  eligibility OTP flow
- Close Chrome after claim submission via quit_driver() (session
  preserved in profile)
- Move Map Price button between Direct Submission and procedure table,
  right-aligned above Billed Amount column
- Add fee-schedule update-price backend route
- Add DDMA claim processor with claimNumber/pdf_url result handling

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Gitead
2026-05-24 13:35:04 -04:00
parent 5ceecbeb7f
commit cd1381e9c6
13 changed files with 2139 additions and 22 deletions

View File

@@ -51,6 +51,8 @@ import {
mapPricesForForm,
applyComboToForm,
getDescriptionForCode,
findPriceMismatches,
type PriceMismatch,
} from "@/utils/procedureCombosMapping";
import { PROCEDURE_COMBOS } from "@/utils/procedureCombos";
import { DateInput } from "../ui/dateInput";
@@ -61,6 +63,16 @@ import {
RegularComboButtons,
} from "@/components/procedure/procedure-combo-buttons";
import { Switch } from "@/components/ui/switch";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
interface ClaimFormProps {
patientId: number;
@@ -77,6 +89,7 @@ interface ClaimFormProps {
onHandleForMHSeleniumClaimPreAuth: (data: ClaimPreAuthData) => void;
onHandleForCCASeleniumClaim: (data: ClaimFormData) => void;
onHandleForCCASeleniumPreAuth: (data: ClaimPreAuthData) => void;
onHandleForDDMASeleniumClaim: (data: ClaimFormData) => void;
onClose: () => void;
}
@@ -91,6 +104,7 @@ export function ClaimForm({
onHandleForMHSeleniumClaimPreAuth,
onHandleForCCASeleniumClaim,
onHandleForCCASeleniumPreAuth,
onHandleForDDMASeleniumClaim,
onSubmit,
onClose,
}: ClaimFormProps) {
@@ -601,7 +615,7 @@ export function ClaimForm({
if (!p) return "";
if (p.includes("masshealth") || p === "mh" || p === "mass health") return "MH";
if (p.includes("commonwealth care alliance") || p === "cca") return "CCA";
if (p.includes("ddma")) return "DDMA";
if (p.includes("ddma") || p.includes("delta dental ma")) return "DDMA";
if (p.includes("delta ins") || p === "deltains") return "DeltaIns";
if (p.includes("tufts") || p.includes("dentaquest") || p === "tuftssco") return "TuftsSCO";
if (p.includes("united sco") || p === "unitedsco") return "UnitedSCO";
@@ -717,6 +731,8 @@ export function ClaimForm({
// FILE UPLOAD ZONE
const uploadZoneRef = useRef<MultipleFileUploadZoneHandle | null>(null);
const [isUploading, setIsUploading] = useState(false);
const [priceMismatches, setPriceMismatches] = useState<PriceMismatch[]>([]);
const pendingClaimAction = useRef<(() => void) | null>(null);
// NO validation here — the upload zone handles validation, toasts, max files, sizes, etc.
const handleFilesChange = useCallback((files: File[]) => {
@@ -976,6 +992,84 @@ export function ClaimForm({
onClose();
};
// Delta MA Claim: saves to DB then submits via Selenium
const handleDDMAClaim = async () => {
const missingFields: string[] = [];
if (!form.memberId?.trim()) missingFields.push("Member ID");
if (!form.dateOfBirth?.trim()) missingFields.push("Date of Birth");
if (!patient?.firstName?.trim()) missingFields.push("First Name");
if (missingFields.length > 0) {
toast({
title: "Missing Required Fields",
description: `Please fill out the following field(s): ${missingFields.join(", ")}`,
variant: "destructive",
});
return;
}
const filteredServiceLines = (form.serviceLines || []).filter(
(line) => (line.procedureCode ?? "").trim() !== "",
);
if (filteredServiceLines.length === 0) {
toast({
title: "No procedure codes",
description: "Please add at least one procedure code before submitting the claim.",
variant: "destructive",
});
return;
}
let appointmentIdToUse = appointmentId;
if (appointmentIdToUse == null) {
const created = await onHandleAppointmentSubmit({
patientId,
date: serviceDate,
staffId: appointmentStaffId ?? staff?.id,
});
if (typeof created === "number" && created > 0) {
appointmentIdToUse = created;
} else if (created && typeof (created as any).id === "number") {
appointmentIdToUse = (created as any).id;
}
}
const { uploadedFiles, insuranceSiteKey, npiProvider, ...formToCreateClaim } = form;
// Upload files to server so we get local filePaths for Selenium
const claimFilesMeta: ClaimFileMeta[] = uploadedFiles?.length
? await uploadAttachmentsToLocalFolder(uploadedFiles)
: [];
const selectedNpiProviderId = npiProvider?.npiNumber
? npiProviders.find((p) => p.npiNumber === npiProvider.npiNumber)?.id ?? null
: null;
const createdClaim = await onSubmit({
...formToCreateClaim,
serviceLines: filteredServiceLines,
staffId: appointmentStaffId ?? Number(staff?.id),
patientId,
insuranceProvider: "Delta Dental MA",
appointmentId: appointmentIdToUse!,
claimFiles: claimFilesMeta,
...(selectedNpiProviderId ? { npiProviderId: selectedNpiProviderId } : {}),
});
onHandleForDDMASeleniumClaim({
...form,
serviceLines: filteredServiceLines,
staffId: appointmentStaffId ?? Number(staff?.id),
patientId,
insuranceProvider: "Delta Dental MA",
appointmentId: appointmentIdToUse!,
insuranceSiteKey: "DDMA",
claimId: createdClaim.id,
claimFiles: claimFilesMeta,
});
onClose();
};
const handleCCAPreAuth = async () => {
const missingFields: string[] = [];
if (!form.memberId?.trim()) missingFields.push("Member ID");
@@ -1014,6 +1108,37 @@ export function ClaimForm({
onClose();
};
/** Check prices against the fee schedule. If mismatches exist, show dialog and
* store the action to run after the user responds. Otherwise run immediately. */
const runWithPriceCheck = (action: () => void) => {
const siteKey = form.insuranceSiteKey || deriveInsuranceSiteKey(form.insuranceProvider || "");
const mismatches = findPriceMismatches(
(form.serviceLines || []).filter(l => (l.procedureCode || "").trim()),
siteKey,
patient?.dateOfBirth || "",
form.serviceDate || serviceDate,
);
if (mismatches.length === 0) {
action();
} else {
pendingClaimAction.current = action;
setPriceMismatches(mismatches);
}
};
const savePricesToSchedule = async (mismatches: PriceMismatch[]) => {
const siteKey = form.insuranceSiteKey || deriveInsuranceSiteKey(form.insuranceProvider || "");
await Promise.all(
mismatches.map(m =>
apiRequest("POST", "/api/fee-schedule/update-price", {
siteKey,
procedureCode: m.procedureCode,
price: m.enteredPrice,
})
)
);
};
const uploadAttachmentsToLocalFolder = async (files: File[]): Promise<ClaimFileMeta[]> => {
if (!files.length) return [];
@@ -1493,13 +1618,6 @@ export function ClaimForm({
))}
</SelectContent>
</Select>
<Button
className="ml-4"
variant="success"
onClick={onMapPrice}
>
Map Price
</Button>
</div>
</div>
@@ -1534,6 +1652,12 @@ export function ClaimForm({
/>
</div>
<div className="flex justify-end mb-2">
<Button variant="success" onClick={onMapPrice}>
Map Price
</Button>
</div>
{/* Header */}
<div className="grid grid-cols-[1.5fr,0.5fr,1fr,1fr,1fr,1fr,1fr,0.5fr,1fr,1fr] gap-1 mb-2 mt-10 font-medium text-sm text-gray-700 items-center">
<div className="grid grid-cols-[auto,1fr] items-center gap-2">
@@ -1867,17 +1991,20 @@ export function ClaimForm({
<div className="flex flex-wrap gap-2 justify-center">
<Button
className="w-32 bg-blue-600 hover:bg-blue-700 text-white"
onClick={() => handleMHSubmit()}
onClick={() => runWithPriceCheck(() => handleMHSubmit())}
>
MH Claim
</Button>
<Button
className="w-32 bg-blue-600 hover:bg-blue-700 text-white"
onClick={handleCCAClaim}
onClick={() => runWithPriceCheck(handleCCAClaim)}
>
CCA Claim
</Button>
<Button className="w-36" variant="outline">
<Button
className="w-36 bg-violet-600 hover:bg-violet-700 text-white"
onClick={() => runWithPriceCheck(handleDDMAClaim)}
>
Delta MA Claim
</Button>
<Button className="w-44" variant="outline">
@@ -1888,7 +2015,7 @@ export function ClaimForm({
</Button>
<Button
className="w-36 bg-emerald-600 hover:bg-emerald-700 text-white"
onClick={handleClaimSaved}
onClick={() => runWithPriceCheck(handleClaimSaved)}
>
Claim Saved
</Button>
@@ -2053,17 +2180,16 @@ export function ClaimForm({
))}
</SelectContent>
</Select>
<Button
className="ml-4"
variant="success"
onClick={onMapPrice}
>
Map Price
</Button>
</div>
</div>
</div>
<div className="flex justify-end mb-2">
<Button variant="success" onClick={onMapPrice}>
Map Price
</Button>
</div>
{/* Header */}
<div className="grid grid-cols-[1.5fr,0.5fr,1fr,1fr,1fr,1fr,1fr,0.5fr,1fr,1fr] gap-1 mb-2 mt-10 font-medium text-sm text-gray-700 items-center">
<div className="grid grid-cols-[auto,1fr] items-center gap-2">
@@ -2421,6 +2547,47 @@ export function ClaimForm({
</CardContent>
</Tabs>
</Card>
{/* Price mismatch dialog */}
<AlertDialog open={priceMismatches.length > 0} onOpenChange={open => { if (!open) setPriceMismatches([]); }}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Save new price to the app?</AlertDialogTitle>
<AlertDialogDescription asChild>
<div className="space-y-2">
<p>The following procedure prices differ from the fee schedule:</p>
<ul className="text-sm space-y-1">
{priceMismatches.map(m => (
<li key={m.procedureCode} className="flex justify-between gap-4">
<span className="font-medium">{m.procedureCode}</span>
<span className="text-muted-foreground">Schedule: ${m.schedulePrice.toFixed(2)}</span>
<span className="text-foreground font-semibold">Entered: ${m.enteredPrice.toFixed(2)}</span>
</li>
))}
</ul>
<p className="text-sm">Do you want to save the new price(s) to the fee schedule for future use?</p>
</div>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={() => {
setPriceMismatches([]);
pendingClaimAction.current?.();
pendingClaimAction.current = null;
}}>
No
</AlertDialogCancel>
<AlertDialogAction onClick={async () => {
await savePricesToSchedule(priceMismatches);
setPriceMismatches([]);
pendingClaimAction.current?.();
pendingClaimAction.current = null;
}}>
Yes
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
);
}

View File

@@ -33,6 +33,7 @@ import {
import ClaimDocumentsUploadMultiple from "@/components/claims/claim-document-upload-modal";
import { QK_PATIENTS_BASE } from "@/components/patients/patient-table";
import { PdfPreviewModal } from "@/components/insurance-status/pdf-preview-modal";
import { socket } from "@/lib/socket";
export default function ClaimsPage() {
const [isClaimFormOpen, setIsClaimFormOpen] = useState(false);
@@ -54,6 +55,10 @@ export default function ClaimsPage() {
// Track pending selenium jobs so we can react to completion via socket
const [pendingClaimJobId, setPendingClaimJobId] = useState<string | null>(null);
// DDMA claim OTP modal
const [ddmaClaimOtpOpen, setDdmaClaimOtpOpen] = useState(false);
const [ddmaClaimOtpSubmitting, setDdmaClaimOtpSubmitting] = useState(false);
const ddmaClaimSessionIdRef = useRef<string | null>(null);
const pendingClaimMeta = useRef<{
patientId: number | null;
groupKey: "INSURANCE_CLAIM" | "INSURANCE_CLAIM_PREAUTH";
@@ -454,6 +459,73 @@ export default function ClaimsPage() {
}
};
// DDMA claim OTP submit handler
const handleDdmaClaimOtpSubmit = async (otp: string) => {
const sessionId = ddmaClaimSessionIdRef.current;
if (!sessionId) return;
try {
setDdmaClaimOtpSubmitting(true);
const resp = await apiRequest("POST", "/api/claims/ddma-claim/selenium/submit-otp", {
session_id: sessionId,
otp,
socketId,
});
const data = await resp.json();
if (!resp.ok || data.error) throw new Error(data.error || "Failed to submit OTP");
setDdmaClaimOtpOpen(false);
dispatch(setTaskStatus({ key: "claimSubmit", status: "pending", message: "OTP submitted. Continuing DDMA claim..." }));
} catch (err: any) {
toast({ title: "Failed to submit OTP", description: err?.message || "Error submitting OTP", variant: "destructive" });
} finally {
setDdmaClaimOtpSubmitting(false);
}
};
// DDMA claim selenium handler
const handleDDMAClaimSubmitSelenium = async (data: any) => {
try {
dispatch(setTaskStatus({ key: "claimSubmit", status: "pending", message: "Submitting Delta MA claim..." }));
const response = await apiRequest("POST", "/api/claims/ddma-claim", {
data,
socketId,
});
const result = await response.json();
if (result.error) throw new Error(result.error);
pendingClaimMeta.current = { patientId: selectedPatientId, groupKey: "INSURANCE_CLAIM" };
setPendingClaimJobId(result.jobId);
// Listen for session_id so we can forward OTP if needed
const jobId = result.jobId;
const onSessionStarted = (ev: any) => {
if (String(ev?.jobId) !== String(jobId)) return;
ddmaClaimSessionIdRef.current = ev.session_id ?? null;
};
const onOtpRequired = (ev: any) => {
if (String(ev?.jobId) !== String(jobId)) return;
if (ev.session_id) ddmaClaimSessionIdRef.current = ev.session_id;
setDdmaClaimOtpOpen(true);
dispatch(setTaskStatus({ key: "claimSubmit", status: "pending", message: "OTP required for Delta MA. Please enter the code." }));
};
const onDone = (ev: any) => {
if (String(ev?.jobId) !== String(jobId)) return;
socket.off("selenium:ddma_claim_started", onSessionStarted);
socket.off("selenium:otp_required", onOtpRequired);
socket.off("job:update", onDone);
setDdmaClaimOtpOpen(false);
ddmaClaimSessionIdRef.current = null;
};
socket.on("selenium:ddma_claim_started", onSessionStarted);
socket.on("selenium:otp_required", onOtpRequired);
socket.on("job:update", onDone);
dispatch(setTaskStatus({ key: "claimSubmit", status: "pending", message: "Delta MA claim queued. Awaiting Selenium..." }));
toast({ title: "Delta MA Claim queued", description: "Selenium is opening the claim form.", variant: "default" });
} catch (error: any) {
dispatch(setTaskStatus({ key: "claimSubmit", status: "error", message: error.message || "Delta MA claim failed" }));
toast({ title: "Delta MA Claim error", description: error.message || "An error occurred.", variant: "destructive" });
}
};
// CCA pre-auth selenium handler
const handleCCAPreAuthSubmitSelenium = async (data: any) => {
const formData = new FormData();
@@ -518,6 +590,7 @@ export default function ClaimsPage() {
const isPreAuth = groupTitleKey === "INSURANCE_CLAIM_PREAUTH";
const preAuthNumber = data.preAuthNumber ?? data.result?.preAuthNumber ?? null;
const claimNumberForToast = data.claimNumber ?? null;
dispatch(
setTaskStatus({
@@ -525,7 +598,9 @@ export default function ClaimsPage() {
status: "success",
message: isPreAuth
? `PreAuth submitted & PDF saved.${preAuthNumber ? ` PreAuth #: ${preAuthNumber}` : ""}`
: "Claim submitted & PDF downloaded successfully.",
: claimNumberForToast
? `Claim submitted — Claim #: ${claimNumberForToast}`
: "Claim submitted & PDF downloaded successfully.",
})
);
@@ -535,7 +610,9 @@ export default function ClaimsPage() {
? preAuthNumber
? `PreAuth Number: ${preAuthNumber} — PDF saved to Documents.`
: "PreAuth submitted! PDF saved to Documents page."
: "Claim submitted successfully! PDF saved to Documents page.",
: claimNumberForToast
? `Claim #: ${claimNumberForToast} — PDF saved to Documents.`
: "Claim submitted successfully! PDF saved to Documents page.",
duration: isPreAuth ? 10000 : 5000,
});
@@ -695,6 +772,7 @@ export default function ClaimsPage() {
onHandleForMHSeleniumClaimPreAuth={handleMHClaimPreAuthSubmitSelenium}
onHandleForCCASeleniumClaim={handleCCAClaimSubmitSelenium}
onHandleForCCASeleniumPreAuth={handleCCAPreAuthSubmitSelenium}
onHandleForDDMASeleniumClaim={handleDDMAClaimSubmitSelenium}
/>
)}
@@ -709,6 +787,45 @@ export default function ClaimsPage() {
pdfId={previewPdfId}
fallbackFilename={previewFallbackFilename}
/>
{/* DDMA Claim OTP Modal */}
{ddmaClaimOtpOpen && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40">
<div className="bg-white dark:bg-slate-900 rounded-xl shadow-lg w-full max-w-md p-6">
<div className="flex items-center justify-between mb-4">
<h2 className="text-lg font-semibold">Enter OTP Delta MA Claim</h2>
<button type="button" onClick={() => setDdmaClaimOtpOpen(false)} className="text-slate-500 hover:text-slate-800"></button>
</div>
<p className="text-sm text-slate-500 mb-4">
The Delta Dental MA portal requires a one-time password (OTP) to continue claim submission.
</p>
<form onSubmit={(e) => {
e.preventDefault();
const input = (e.currentTarget.elements.namedItem("otp") as HTMLInputElement);
if (input?.value.trim()) handleDdmaClaimOtpSubmit(input.value.trim());
}} className="space-y-4">
<div className="space-y-2">
<label htmlFor="ddma-claim-otp" className="text-sm font-medium">OTP</label>
<input
id="ddma-claim-otp"
name="otp"
placeholder="Enter OTP code"
autoFocus
className="w-full border rounded-md px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div>
<div className="flex justify-end gap-3">
<button type="button" onClick={() => setDdmaClaimOtpOpen(false)} disabled={ddmaClaimOtpSubmitting}
className="px-4 py-2 text-sm border rounded-md hover:bg-slate-50 disabled:opacity-50">Cancel</button>
<button type="submit" disabled={ddmaClaimOtpSubmitting}
className="px-4 py-2 text-sm bg-primary text-white rounded-md hover:bg-primary/90 disabled:opacity-50">
{ddmaClaimOtpSubmitting ? "Submitting..." : "Submit OTP"}
</button>
</div>
</form>
</div>
</div>
)}
</div>
);
}

View File

@@ -2,6 +2,7 @@ import { InputServiceLine } from "@repo/db/types";
import Decimal from "decimal.js";
import rawCodeTable from "@/assets/data/procedureCodesMH.json";
import rawCCACodeTable from "@/assets/data/procedureCodesCCA.json";
import rawDDMACodeTable from "@/assets/data/procedureCodesDDMA.json";
import { PROCEDURE_COMBOS } from "./procedureCombos";
/* ----------------------------- Types ----------------------------- */
@@ -15,6 +16,7 @@ export type CodeRow = {
};
const CODE_TABLE = rawCodeTable as CodeRow[];
const CCA_CODE_TABLE = rawCCACodeTable as CodeRow[];
const DDMA_CODE_TABLE = rawDDMACodeTable as CodeRow[];
export type ClaimFormLike = {
serviceDate: string; // form-level service date
@@ -56,9 +58,19 @@ const CCA_CODE_MAP: Map<string, CodeRow> = (() => {
return m;
})();
const DDMA_CODE_MAP: Map<string, CodeRow> = (() => {
const m = new Map<string, CodeRow>();
for (const r of DDMA_CODE_TABLE) {
const k = normalizeCode(String(r["Procedure Code"] || ""));
if (k && !m.has(k)) m.set(k, r);
}
return m;
})();
/** Return the correct fee-schedule map for the given insurance type. */
function getCodeMap(insuranceSiteKey?: string): Map<string, CodeRow> {
if (insuranceSiteKey === "CCA") return CCA_CODE_MAP;
if (insuranceSiteKey === "DDMA") return DDMA_CODE_MAP;
return CODE_MAP; // default: MassHealth
}
@@ -333,4 +345,43 @@ export function applyComboToForm<T extends ClaimFormLike>(
}
export { CODE_MAP, CCA_CODE_MAP, getCodeMap, getPriceForCodeWithAgeFromMap };
export { CODE_MAP, CCA_CODE_MAP, DDMA_CODE_MAP, getCodeMap, getPriceForCodeWithAgeFromMap };
export type PriceMismatch = {
procedureCode: string;
enteredPrice: number;
schedulePrice: number;
};
/** Compare each service line's totalBilled against the fee schedule.
* Returns lines where the entered price differs from the schedule price.
* Returns empty array if the siteKey has no schedule (United, Tufts, etc.). */
export function findPriceMismatches(
serviceLines: InputServiceLine[],
insuranceSiteKey: string | undefined,
patientDOB: string,
serviceDate: string,
): PriceMismatch[] {
const supported = ["MH", "MASSHEALTH", "CCA", "DDMA"];
if (!insuranceSiteKey || !supported.includes(insuranceSiteKey.toUpperCase())) return [];
const map = getCodeMap(insuranceSiteKey);
const mismatches: PriceMismatch[] = [];
for (const line of serviceLines) {
const code = normalizeCode(line.procedureCode || "");
if (!code) continue;
const enteredPrice = new Decimal(Number(line.totalBilled) || 0);
if (enteredPrice.isZero()) continue;
const age = ageOnDate(patientDOB, serviceDate);
const schedulePrice = getPriceForCodeWithAgeFromMap(map, code, age);
if (!schedulePrice.isZero() && !enteredPrice.equals(schedulePrice)) {
mismatches.push({
procedureCode: code,
enteredPrice: enteredPrice.toNumber(),
schedulePrice: schedulePrice.toNumber(),
});
}
}
return mismatches;
}