feat: chatbot CDT lookup — SRP quad, 4-digit auto-prefix, quad field to Selenium

- 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 <noreply@anthropic.com>
This commit is contained in:
Gitead
2026-06-11 22:14:50 -04:00
parent 6cfca0d015
commit 831f67b093
9 changed files with 81 additions and 26 deletions

View File

@@ -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) {

View File

@@ -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"]

View File

@@ -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,
},
};

View File

@@ -825,6 +825,7 @@ router.post(
insuranceSiteKey: "DDMA",
massddmaUsername: credentials.username,
massddmaPassword: credentials.password,
providerName: resolvedNpiProvider?.providerName ?? "",
},
};
jobId = enqueueSeleniumJob({

View File

@@ -40,16 +40,20 @@ router.post("/ddma-claim", async (req: Request, res: Response): Promise<any> =>
});
}
// 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,
},
};

View File

@@ -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 } : {}),
};
}
});

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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", "")
)