feat: improve CCA preauth cell filling, implants category, preauth no recording
- Selenium: bulletproof Wait→Click→Clear→Type→Verify for tooth, billed amt cells - Selenium: fix billed amt to click td[23] (correct column) to trigger edit mode - Selenium: skip tentative date (auto-filled by page after tooth entry) - Frontend: add Implants category with Implant/Abut/Crown, Fixture, Abutment, Crown buttons (D6010/D6057/D6058) - Frontend: pdf-preview-modal renders PNG screenshots as <img> instead of PDF iframe - Backend: CCA preauth route creates claim record if none exists - Backend: CCA preauth processor saves authNumber into claimNumber column after submission Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
222
apps/SeleniumService/helpers_cca_preauth.py
Normal file
222
apps/SeleniumService/helpers_cca_preauth.py
Normal file
@@ -0,0 +1,222 @@
|
||||
import os
|
||||
import time
|
||||
import asyncio
|
||||
from typing import Dict, Any
|
||||
from selenium.common.exceptions import WebDriverException
|
||||
|
||||
from selenium_CCA_preAuthWorker import AutomationCCAPreAuth
|
||||
from cca_browser_manager import get_browser_manager
|
||||
|
||||
sessions: Dict[str, Dict[str, Any]] = {}
|
||||
|
||||
|
||||
def make_session_entry() -> str:
|
||||
import uuid
|
||||
sid = str(uuid.uuid4())
|
||||
sessions[sid] = {
|
||||
"status": "created",
|
||||
"created_at": time.time(),
|
||||
"last_activity": time.time(),
|
||||
"bot": None,
|
||||
"driver": None,
|
||||
"result": None,
|
||||
"message": None,
|
||||
}
|
||||
return sid
|
||||
|
||||
|
||||
async def cleanup_session(sid: str, message: str | None = None):
|
||||
s = sessions.get(sid)
|
||||
if not s:
|
||||
return
|
||||
try:
|
||||
if s.get("status") not in ("completed", "error"):
|
||||
s["status"] = "error"
|
||||
if message:
|
||||
s["message"] = message
|
||||
finally:
|
||||
sessions.pop(sid, None)
|
||||
|
||||
|
||||
async def _remove_session_later(sid: str, delay: int = 30):
|
||||
await asyncio.sleep(delay)
|
||||
await cleanup_session(sid)
|
||||
|
||||
|
||||
def _close_browser(bot):
|
||||
try:
|
||||
bm = get_browser_manager()
|
||||
try:
|
||||
bm.save_cookies()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
bm.quit_driver()
|
||||
print("[CCA PreAuth] Browser closed")
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
print(f"[CCA PreAuth] Could not close browser: {e}")
|
||||
|
||||
|
||||
async def start_cca_preauth_run(sid: str, data: dict, url: str):
|
||||
"""
|
||||
Run the CCA pre-authorization workflow:
|
||||
1. Login to ScionDental portal
|
||||
2. Navigate to Authorization Entry page
|
||||
3. Fill patient info and verify eligibility
|
||||
4. Fill services grid and submit authorization
|
||||
5. Capture confirmation PDF
|
||||
"""
|
||||
s = sessions.get(sid)
|
||||
if not s:
|
||||
return {"status": "error", "message": "session not found"}
|
||||
|
||||
s["status"] = "running"
|
||||
s["last_activity"] = time.time()
|
||||
bot = None
|
||||
|
||||
try:
|
||||
bot = AutomationCCAPreAuth(data)
|
||||
bot.config_driver()
|
||||
|
||||
s["bot"] = bot
|
||||
s["driver"] = bot.driver
|
||||
s["last_activity"] = time.time()
|
||||
|
||||
try:
|
||||
bot.driver.maximize_window()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# --- Login ---
|
||||
try:
|
||||
login_result = bot.login(url)
|
||||
except WebDriverException as wde:
|
||||
s["status"] = "error"
|
||||
s["message"] = f"Selenium driver error during login: {wde}"
|
||||
s["result"] = {"status": "error", "message": s["message"]}
|
||||
_close_browser(bot)
|
||||
asyncio.create_task(_remove_session_later(sid, 30))
|
||||
return {"status": "error", "message": s["message"]}
|
||||
except Exception as e:
|
||||
s["status"] = "error"
|
||||
s["message"] = f"Unexpected error during login: {e}"
|
||||
s["result"] = {"status": "error", "message": s["message"]}
|
||||
_close_browser(bot)
|
||||
asyncio.create_task(_remove_session_later(sid, 30))
|
||||
return {"status": "error", "message": s["message"]}
|
||||
|
||||
if isinstance(login_result, str) and login_result == "ALREADY_LOGGED_IN":
|
||||
print("[CCA PreAuth] Session persisted - skipping login")
|
||||
get_browser_manager().save_cookies()
|
||||
|
||||
elif isinstance(login_result, str) and login_result.startswith("ERROR"):
|
||||
s["status"] = "error"
|
||||
s["message"] = login_result
|
||||
s["result"] = {"status": "error", "message": login_result}
|
||||
_close_browser(bot)
|
||||
asyncio.create_task(_remove_session_later(sid, 30))
|
||||
return {"status": "error", "message": login_result}
|
||||
|
||||
elif isinstance(login_result, str) and login_result == "SUCCESS":
|
||||
print("[CCA PreAuth] Login succeeded")
|
||||
get_browser_manager().save_cookies()
|
||||
|
||||
# --- Navigate to Authorization Entry ---
|
||||
step1_result = bot.step1_navigate_to_auth_entry()
|
||||
print(f"[CCA PreAuth] step1 result: {step1_result}")
|
||||
|
||||
if isinstance(step1_result, str) and step1_result.startswith("ERROR"):
|
||||
s["status"] = "error"
|
||||
s["message"] = step1_result
|
||||
s["result"] = {"status": "error", "message": step1_result}
|
||||
_close_browser(bot)
|
||||
asyncio.create_task(_remove_session_later(sid, 30))
|
||||
return {"status": "error", "message": step1_result}
|
||||
|
||||
# --- Fill patient eligibility form & verify ---
|
||||
step2_result = bot.step2_fill_patient_eligibility()
|
||||
print(f"[CCA PreAuth] step2 result: {step2_result}")
|
||||
|
||||
if isinstance(step2_result, str) and step2_result.startswith("ERROR"):
|
||||
try:
|
||||
page_url = bot.driver.current_url
|
||||
body = bot.driver.find_element(
|
||||
__import__('selenium').webdriver.common.by.By.TAG_NAME, "body"
|
||||
).text[:600]
|
||||
print(f"[CCA PreAuth] step2 error — URL: {page_url}")
|
||||
print(f"[CCA PreAuth] step2 error — page text: {body}")
|
||||
except Exception:
|
||||
pass
|
||||
s["status"] = "error"
|
||||
s["message"] = step2_result
|
||||
s["result"] = {"status": "error", "message": step2_result}
|
||||
_close_browser(bot)
|
||||
asyncio.create_task(_remove_session_later(sid, 30))
|
||||
return {"status": "error", "message": step2_result}
|
||||
|
||||
# --- Fill Services grid, attach docs, submit ---
|
||||
step3_result = bot.step3_fill_services_and_submit()
|
||||
print(f"[CCA PreAuth] step3 result: {step3_result}")
|
||||
|
||||
if isinstance(step3_result, str) and step3_result.startswith("ERROR"):
|
||||
try:
|
||||
page_url = bot.driver.current_url
|
||||
body = bot.driver.find_element(
|
||||
__import__('selenium').webdriver.common.by.By.TAG_NAME, "body"
|
||||
).text[:600]
|
||||
print(f"[CCA PreAuth] step3 error — URL: {page_url}")
|
||||
print(f"[CCA PreAuth] step3 error — page text: {body}")
|
||||
except Exception:
|
||||
pass
|
||||
s["status"] = "error"
|
||||
s["message"] = step3_result
|
||||
s["result"] = {"status": "error", "message": step3_result}
|
||||
_close_browser(bot)
|
||||
asyncio.create_task(_remove_session_later(sid, 30))
|
||||
return {"status": "error", "message": step3_result}
|
||||
|
||||
# --- Capture confirmation PDF + auth number ---
|
||||
step4_result = bot.step4_capture_confirmation()
|
||||
print(f"[CCA PreAuth] step4 authNumber={step4_result.get('authNumber')}, "
|
||||
f"pdfLen={len(step4_result.get('pdfBase64', ''))}")
|
||||
|
||||
_close_browser(bot)
|
||||
|
||||
result = {
|
||||
"status": "success",
|
||||
"message": "CCA pre-authorization submitted successfully",
|
||||
"authNumber": step4_result.get("authNumber"),
|
||||
"pdfBase64": step4_result.get("pdfBase64", ""),
|
||||
"pdfFilename": step4_result.get("pdfFilename", ""),
|
||||
}
|
||||
s["status"] = "completed"
|
||||
s["result"] = result
|
||||
s["message"] = "completed"
|
||||
asyncio.create_task(_remove_session_later(sid, 60))
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
if s:
|
||||
s["status"] = "error"
|
||||
s["message"] = f"worker exception: {e}"
|
||||
s["result"] = {"status": "error", "message": s["message"]}
|
||||
if bot:
|
||||
_close_browser(bot)
|
||||
asyncio.create_task(_remove_session_later(sid, 30))
|
||||
return {"status": "error", "message": f"worker exception: {e}"}
|
||||
|
||||
|
||||
def get_session_status(sid: str) -> Dict[str, Any]:
|
||||
s = sessions.get(sid)
|
||||
if not s:
|
||||
return {"status": "not_found"}
|
||||
return {
|
||||
"session_id": sid,
|
||||
"status": s.get("status"),
|
||||
"message": s.get("message"),
|
||||
"created_at": s.get("created_at"),
|
||||
"last_activity": s.get("last_activity"),
|
||||
"result": s.get("result") if s.get("status") in ("completed", "error") else None,
|
||||
}
|
||||
Reference in New Issue
Block a user