From 831f67b0931beff70dd58c08caf4f3d054852d20 Mon Sep 17 00:00:00 2001 From: Gitead Date: Thu, 11 Jun 2026 22:14:50 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20chatbot=20CDT=20lookup=20=E2=80=94=20SR?= =?UTF-8?q?P=20quad,=204-digit=20auto-prefix,=20quad=20field=20to=20Seleni?= =?UTF-8?q?um?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - parseSrpCode: recognize "D4341 UL" / "4341 LR" etc., store quadrant in quad field - matchOne: auto-prefix D for 4-digit inputs like "0120" → D0120 - LLM prompt: keep SRP code and quadrant together as one procedureName entry - CdtMatch / CdtResult: add quad field, thread through matchedCodes action data - claim-form.tsx: include quad in chatbot_claim_prefill type and spread to service line - selenium_claimSubmitWorker.py: pass quad to fill_service_line, select quadrant dropdown by index (UR=1, UL=2, LL=3, LR=4) matching MassHealth form structure Co-Authored-By: Claude Sonnet 4.6 --- apps/Backend/src/ai/cdt-lookup.ts | 28 +++++++++++ apps/Backend/src/ai/internal-chat-graph.ts | 2 + apps/Backend/src/ai/internal-chat-workflow.ts | 8 ++-- apps/Backend/src/routes/claims.ts | 1 + .../src/routes/insuranceStatusDDMAClaim.ts | 10 ++-- .../src/components/claims/claim-form.tsx | 3 +- .../src/components/layout/chatbot.tsx | 4 ++ .../src/pages/insurance-status-page.tsx | 4 +- .../selenium_claimSubmitWorker.py | 47 ++++++++++++------- 9 files changed, 81 insertions(+), 26 deletions(-) diff --git a/apps/Backend/src/ai/cdt-lookup.ts b/apps/Backend/src/ai/cdt-lookup.ts index 1dda2a43..97550864 100644 --- a/apps/Backend/src/ai/cdt-lookup.ts +++ b/apps/Backend/src/ai/cdt-lookup.ts @@ -19,6 +19,7 @@ interface CdtMatch { input: string; toothNumber?: string; toothSurface?: string; + quad?: string; } // Load once at module init @@ -284,6 +285,21 @@ function parseCompositeCode(input: string): CdtMatch | null { return { code, description: row?.Description ?? code, input, toothNumber: String(toothNum), toothSurface: surfaces.toUpperCase() }; } +/** + * Parse SRP code with quadrant: "D4341 UL", "4341 LR", "D4342 UR", etc. + * UL/UR/LL/LR only appear with D4341 or D4342. + */ +function parseSrpCode(input: string): CdtMatch | null { + const m = input.trim().match(/^(D?4341|D?4342)\s+(UL|UR|LL|LR)$/i); + if (!m) return null; + const base = m[1]!.toUpperCase(); + const codeStr = base.startsWith("D") ? base : "D" + base; + const quadrant = m[2]!.toUpperCase(); + const row = ALL_CODES.find((r) => r["Procedure Code"].toUpperCase() === codeStr); + if (!row) return null; + return { code: codeStr, description: row.Description, input, quad: quadrant }; +} + /** * Parse RCT/root-canal notation like "rct #29", "#14 rct", "root canal #6". * Returns D3310 (anterior), D3320 (premolar), or D3330 (molar) based on tooth number. @@ -352,6 +368,14 @@ function matchOne(input: string): CdtMatch | null { return { code, description: row?.Description ?? code, input }; } + // 4-digit shorthand: auto-prefix "D" (e.g. "0120" → "D0120") + if (/^\d{4}$/.test(cleaned)) { + const withD = "D" + cleaned; + const row = ALL_CODES.find((r) => r["Procedure Code"].toUpperCase() === withD.toUpperCase()); + const code = row?.["Procedure Code"] ?? withD.toUpperCase(); + return { code, description: row?.Description ?? code, input }; + } + // Apply alias before tokenizing const normalized = ALIAS_MAP[cleaned] ?? cleaned; const queryTokens = normalized @@ -420,6 +444,10 @@ export function lookupCdtCodes( const rctMatch = parseRctCode(name); if (rctMatch) return rctMatch; + // 2c. SRP + quadrant — D4341/D4342 with UL/UR/LL/LR + const srpMatch = parseSrpCode(name); + if (srpMatch) return srpMatch; + // 3. Hardcoded direct code map (short abbreviations / codes not in MH schedule) const directKey = DIRECT_CODE_MAP[cleaned] ? cleaned : DIRECT_CODE_MAP[strippedCleaned] ? strippedCleaned : null; if (directKey) { diff --git a/apps/Backend/src/ai/internal-chat-graph.ts b/apps/Backend/src/ai/internal-chat-graph.ts index 2bfffc33..f05b94e3 100644 --- a/apps/Backend/src/ai/internal-chat-graph.ts +++ b/apps/Backend/src/ai/internal-chat-graph.ts @@ -101,6 +101,8 @@ Rules: e.g. "composite #29 O", "#8 MO", "composite #11 MOD" — keep the #number and surface letters together as one entry - For RCT/root canal with a tooth number, preserve the tooth# in the entry: e.g. "rct #29", "#14 root canal", "rct #6" — keep the #number with the procedure so the correct code can be selected +- For SRP with a quadrant abbreviation (UL, UR, LL, LR), keep the code and quadrant together as one entry: + e.g. "D4341 UL", "4341 LR", "D4342 UR" — the quadrant always travels with the SRP code - For multiple PA X-rays with tooth numbers, expand each PA into its own entry: "1 pa, #T" for the first tooth, "2nd pa, #T" for each additional tooth e.g. "2 PA (#30, 15)" → ["1 pa, #30", "2nd pa, #15"] diff --git a/apps/Backend/src/ai/internal-chat-workflow.ts b/apps/Backend/src/ai/internal-chat-workflow.ts index 6119c690..c483ae13 100644 --- a/apps/Backend/src/ai/internal-chat-workflow.ts +++ b/apps/Backend/src/ai/internal-chat-workflow.ts @@ -35,6 +35,7 @@ export interface CdtResult { input: string; toothNumber?: string; toothSurface?: string; + quad?: string; } export interface ChatResponse { @@ -467,8 +468,9 @@ async function handleCheckAndClaim( siteKey, autoCheck: siteKeyToAutoCheck(siteKey), cdtResults, - matchedCodes: matched.map((r) => ({ code: r.code!, description: r.description, toothNumber: r.toothNumber, toothSurface: r.toothSurface })), + matchedCodes: matched.map((r) => ({ code: r.code!, description: r.description, toothNumber: r.toothNumber, toothSurface: r.toothSurface, quad: r.quad })), serviceDate: c.appointmentDate ?? null, + renderingProvider: c.renderingProvider ?? null, }, }; } @@ -550,7 +552,7 @@ async function handleClaimOnly( actionData: { patient, siteKey: resolveSiteKey(patient.insuranceProvider ?? null, c.insuranceHint ?? null) ?? "MH", - matchedCodes: matched.map((r) => ({ code: r.code!, description: r.description, toothNumber: r.toothNumber, toothSurface: r.toothSurface })), + matchedCodes: matched.map((r) => ({ code: r.code!, description: r.description, toothNumber: r.toothNumber, toothSurface: r.toothSurface, quad: r.quad })), options: [ { label: fmtUTC(sorted[0]), appointmentId: sorted[0].id, serviceDate: isoUTC(sorted[0]) }, { label: fmtUTC(sorted[1]), appointmentId: sorted[1].id, serviceDate: isoUTC(sorted[1]) }, @@ -593,7 +595,7 @@ async function handleClaimOnly( siteKey, serviceDate, appointmentId, - matchedCodes: matched.map((r) => ({ code: r.code!, description: r.description, toothNumber: r.toothNumber, toothSurface: r.toothSurface })), + matchedCodes: matched.map((r) => ({ code: r.code!, description: r.description, toothNumber: r.toothNumber, toothSurface: r.toothSurface, quad: r.quad })), renderingProvider: c.renderingProvider ?? null, }, }; diff --git a/apps/Backend/src/routes/claims.ts b/apps/Backend/src/routes/claims.ts index 25aea799..27fc6a75 100755 --- a/apps/Backend/src/routes/claims.ts +++ b/apps/Backend/src/routes/claims.ts @@ -825,6 +825,7 @@ router.post( insuranceSiteKey: "DDMA", massddmaUsername: credentials.username, massddmaPassword: credentials.password, + providerName: resolvedNpiProvider?.providerName ?? "", }, }; jobId = enqueueSeleniumJob({ diff --git a/apps/Backend/src/routes/insuranceStatusDDMAClaim.ts b/apps/Backend/src/routes/insuranceStatusDDMAClaim.ts index a62b94ff..87707359 100644 --- a/apps/Backend/src/routes/insuranceStatusDDMAClaim.ts +++ b/apps/Backend/src/routes/insuranceStatusDDMAClaim.ts @@ -40,16 +40,20 @@ router.post("/ddma-claim", async (req: Request, res: Response): Promise => }); } - // Fetch NPI providers to pick the target provider on the DDMA portal + // Use provider from the claim form if set; fall back to Provider #1 (default) const npiProviders = await storage.getNpiProvidersByUser(req.user.id); - const primaryProvider = npiProviders[0]; // sorted by sortOrder asc, then id asc + const primaryProvider = npiProviders[0]; + const providerName = + (claimData.npiProvider?.providerName as string | undefined)?.trim() || + primaryProvider?.providerName || + ""; const enrichedPayload = { claim: { ...claimData, massddmaUsername: credentials.username, massddmaPassword: credentials.password, - providerName: primaryProvider?.providerName ?? "", + providerName, }, }; diff --git a/apps/Frontend/src/components/claims/claim-form.tsx b/apps/Frontend/src/components/claims/claim-form.tsx index fe4ee661..eef005e4 100755 --- a/apps/Frontend/src/components/claims/claim-form.tsx +++ b/apps/Frontend/src/components/claims/claim-form.tsx @@ -527,7 +527,7 @@ export function ClaimForm({ if (!raw) return; try { const { codes, serviceDate, renderingProvider } = JSON.parse(raw) as { - codes: { code: string; description: string; toothNumber?: string; toothSurface?: string }[]; + codes: { code: string; description: string; toothNumber?: string; toothSurface?: string; quad?: string }[]; serviceDate?: string; renderingProvider?: string | null; }; @@ -553,6 +553,7 @@ export function ClaimForm({ procedureDate: date, ...(c.toothNumber ? { toothNumber: c.toothNumber } : {}), ...(c.toothSurface ? { toothSurface: c.toothSurface } : {}), + ...(c.quad ? { quad: c.quad } : {}), }; } }); diff --git a/apps/Frontend/src/components/layout/chatbot.tsx b/apps/Frontend/src/components/layout/chatbot.tsx index 98d314e9..17cc8f18 100644 --- a/apps/Frontend/src/components/layout/chatbot.tsx +++ b/apps/Frontend/src/components/layout/chatbot.tsx @@ -62,6 +62,7 @@ interface CheckAndClaimData { autoCheck: string; matchedCodes: { code: string; description: string }[]; serviceDate?: string | null; + renderingProvider?: string | null; } let msgCounter = 0; @@ -346,6 +347,7 @@ export function ChatbotButton() { memberId: checkAndClaimData.memberId, dob: checkAndClaimData.dob, serviceDate: checkAndClaimData.serviceDate ?? null, + renderingProvider: checkAndClaimData.renderingProvider ?? null, }) ); prefillAndNavigate(checkAndClaimData.memberId, checkAndClaimData.dob, checkAndClaimData.autoCheck); @@ -412,6 +414,7 @@ export function ChatbotButton() { autoCheck: data.actionData.autoCheck, matchedCodes: data.actionData.matchedCodes ?? [], serviceDate: data.actionData.serviceDate ?? null, + renderingProvider: data.actionData.renderingProvider ?? null, }); setStep("check-and-claim-ready"); return; @@ -788,6 +791,7 @@ export function ChatbotButton() { autoCheck: data.actionData.autoCheck, matchedCodes: data.actionData.matchedCodes ?? [], serviceDate: data.actionData.serviceDate ?? null, + renderingProvider: data.actionData.renderingProvider ?? null, }); setStep("check-and-claim-ready"); } else if (data.action === "eligibility_id_ready" && data.actionData) { diff --git a/apps/Frontend/src/pages/insurance-status-page.tsx b/apps/Frontend/src/pages/insurance-status-page.tsx index 47543ca1..898f6579 100755 --- a/apps/Frontend/src/pages/insurance-status-page.tsx +++ b/apps/Frontend/src/pages/insurance-status-page.tsx @@ -686,7 +686,7 @@ export default function InsuranceStatusPage() { try { const raw = sessionStorage.getItem("chatbot_claim_codes"); if (!raw) return false; - const { codes, siteKey, patientId, memberId: storedMemberId, serviceDate } = JSON.parse(raw); + const { codes, siteKey, patientId, memberId: storedMemberId, serviceDate, renderingProvider } = JSON.parse(raw); sessionStorage.removeItem("chatbot_claim_codes"); let pid: number | null = resolvedPatientId ?? patientId ?? null; @@ -706,7 +706,7 @@ export default function InsuranceStatusPage() { sessionStorage.setItem( "chatbot_claim_prefill", - JSON.stringify({ codes, siteKey, serviceDate: serviceDate ?? null, autoSubmit: true }) + JSON.stringify({ codes, siteKey, serviceDate: serviceDate ?? null, autoSubmit: true, renderingProvider: renderingProvider ?? null }) ); setLocation(`/claims?newPatient=${pid}`); return true; diff --git a/apps/SeleniumService/selenium_claimSubmitWorker.py b/apps/SeleniumService/selenium_claimSubmitWorker.py index 2e0d9358..833d5d9a 100755 --- a/apps/SeleniumService/selenium_claimSubmitWorker.py +++ b/apps/SeleniumService/selenium_claimSubmitWorker.py @@ -1176,11 +1176,19 @@ class AutomationMassHealthClaimsLogin: "message": f"PDF + Screenshot failed: {ss_error}", } - def fill_service_line(self, row_number=1, procedure_code="", tooth_number="", tooth_surface="", quadrant="Upper Right", arch="Entire Oral Cavity", auth_number="AUTH123456", billed_amount=""): + # Map quadrant abbreviations to MassHealth dropdown visible text + QUAD_LABEL = { + "UL": "Upper Left", + "UR": "Upper Right", + "LL": "Lower Left", + "LR": "Lower Right", + } + + def fill_service_line(self, row_number=1, procedure_code="", tooth_number="", tooth_surface="", quadrant="", arch="Entire Oral Cavity", auth_number="AUTH123456", billed_amount=""): wait = WebDriverWait(self.driver, 30) try: - print(f"DEBUG: Filling service line {row_number} - Procedure: {procedure_code}, Tooth: {tooth_number}, Surface: {tooth_surface}, Amount: {billed_amount}") + print(f"DEBUG: Filling service line {row_number} - Procedure: {procedure_code}, Tooth: {tooth_number}, Surface: {tooth_surface}, Quadrant: {quadrant}, Amount: {billed_amount}") # Fill Procedure Code - find the specific row's procedure input if procedure_code: @@ -1261,20 +1269,23 @@ class AutomationMassHealthClaimsLogin: else: print(f"DEBUG: No tooth surface provided for row {row_number}, skipping") - # COMMENTED OUT: Quadrant - not needed per user requirement - # Will be handled later for specific procedure codes that require it - # quadrant_dropdowns = wait.until( - # EC.presence_of_all_elements_located( - # (By.XPATH, "//select[@ng-model='serviceLine.quadrant']") - # ) - # ) - # if len(quadrant_dropdowns) < row_number: - # raise Exception(f"Only {len(quadrant_dropdowns)} quadrant dropdowns found, but need row {row_number}") - # quadrant_dropdown = quadrant_dropdowns[row_number - 1] - # time.sleep(1) - # select_quadrant = Select(quadrant_dropdown) - # select_quadrant.select_by_visible_text(quadrant) - # time.sleep(2) + # Fill Quadrant - only if provided (D4341/D4342 SRP codes) + # Dropdown index: 1=Upper Right, 2=Upper Left, 3=Lower Left, 4=Lower Right + quad_index = {"UR": 1, "UL": 2, "LL": 3, "LR": 4}.get((quadrant or "").upper()) + if quad_index is not None: + print(f"DEBUG: Selecting quadrant '{quadrant}' (index {quad_index}) for row {row_number}") + quadrant_dropdowns = wait.until( + EC.presence_of_all_elements_located( + (By.XPATH, "//select[@ng-model='serviceLine.quadrant']") + ) + ) + if len(quadrant_dropdowns) < row_number: + raise Exception(f"Only {len(quadrant_dropdowns)} quadrant dropdowns found, but need row {row_number}") + select_quadrant = Select(quadrant_dropdowns[row_number - 1]) + select_quadrant.select_by_index(quad_index) + time.sleep(1) + else: + print(f"DEBUG: No quadrant provided for row {row_number}, skipping") # COMMENTED OUT: Arch - not needed per user requirement # Will be handled later for specific procedure codes that require it @@ -1402,6 +1413,7 @@ class AutomationMassHealthClaimsLogin: procedure_code=service_line.get("procedureCode", ""), tooth_number=service_line.get("toothNumber", ""), tooth_surface=service_line.get("toothSurface", ""), + quadrant=service_line.get("quad", ""), billed_amount=service_line.get("totalBilled", "") ) else: @@ -1409,12 +1421,13 @@ class AutomationMassHealthClaimsLogin: plus_result = self.click_plus_button() if plus_result != "Success": return {"status": "error", "message": plus_result} - + service_result = self.fill_service_line( row_number=row_num, procedure_code=service_line.get("procedureCode", ""), tooth_number=service_line.get("toothNumber", ""), tooth_surface=service_line.get("toothSurface", ""), + quadrant=service_line.get("quad", ""), billed_amount=service_line.get("totalBilled", "") )