feat: MetLife Dental eligibility check with persistent session

Mirrors the DDMA flow: persistent Chrome profile + credential-change logout,
PingOne login with email MFA selection and OTP from app or browser,
Eligibility & Plan Details search (masked Subscriber ID/SSN), family member
picked by DOB and re-verified on the plan details page, PDF saved to the
patient's Eligibility folder and status set ACTIVE/INACTIVE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-13 00:29:28 -04:00
co-authored by Claude Opus 5.5
parent 143319909f
commit db7ea8ef2f
17 changed files with 2475 additions and 12 deletions
@@ -88,6 +88,7 @@ export function siteKeyToAutoCheck(siteKey: string, dob?: string | Date | null):
case "UNITED_SCO": return "united-sco";
case "UNITED_UCARD": return "united-ucard";
case "BCBS_MA": return "bcbs-ma";
case "METLIFE": return "metlife";
default: {
// MassHealth: patients under 21 use the CMSP button
if (dob) {
+5 -1
View File
@@ -39,5 +39,9 @@
{ "keyword": "blue cross blue shield", "siteKey": "BCBS_MA" },
{ "keyword": "bcbs ma", "siteKey": "BCBS_MA" },
{ "keyword": "blue cross", "siteKey": "BCBS_MA" },
{ "keyword": "bcbs", "siteKey": "BCBS_MA" }
{ "keyword": "bcbs", "siteKey": "BCBS_MA" },
{ "keyword": "metlife", "siteKey": "METLIFE" },
{ "keyword": "met life", "siteKey": "METLIFE" },
{ "keyword": "metdental", "siteKey": "METLIFE" }
]
+15
View File
@@ -30,6 +30,7 @@ import { runUnitedUcardClaimProcessor } from "./processors/unitedUcardClaimProce
import { runEligibilityHistoryProcessor } from "./processors/eligibilityHistoryProcessor";
import { runCmspEligibilityHistoryRemainingProcessor } from "./processors/cmspEligibilityHistoryRemainingProcessor";
import { runBcbsMaEligibilityProcessor } from "./processors/bcbsMaEligibilityProcessor";
import { runMetLifeEligibilityProcessor } from "./processors/metlifeEligibilityProcessor";
import type { SeleniumJobData, OcrJobData } from "./queues";
// ── Queue instances ──────────────────────────────────────────────────────────
@@ -104,6 +105,20 @@ export function enqueueSeleniumJob(data: SeleniumJobData): string {
job.id
);
}
if (jobType === "metlife-eligibility-check") {
return runMetLifeEligibilityProcessor(
{
enrichedPayload: data.enrichedPayload,
userId: data.userId,
insuranceId: data.insuranceId!,
formFirstName: data.formFirstName,
formLastName: data.formLastName,
formDob: data.formDob,
socketId: data.socketId,
},
job.id
);
}
if (jobType === "deltains-eligibility-check") {
return runDeltaInsEligibilityProcessor(
{
@@ -0,0 +1,394 @@
/**
* Processor for "metlife-eligibility-check" jobs.
*
* Integrates the MetLife persistent-session flow into the InProcessQueue
* (same shape as the DDMA processor):
* 1. Start a session on the Python agent (POST /metlife-eligibility)
* 2. Emit selenium:metlife_session_started → frontend stores session_id for OTP
* 3. Poll agent status, emitting selenium:otp_required when OTP is needed
* 4. On completion: save PDF, create/update patient, update eligibility status
* 5. Return { pdfFileId, pdfFilename, patientUpdateStatus, pdfUploadStatus }
*
* The OTP submission endpoint (/api/insurance-status-metlife/selenium/submit-otp)
* continues to forward OTPs directly to the Python agent — it does NOT go
* through the queue.
*/
import fs from "fs/promises";
import fsSync from "fs";
import path from "path";
import { storage } from "../../storage";
import { emptyFolderContainingFile } from "../../utils/emptyTempFolder";
import {
forwardToSeleniumMetLifeEligibilityAgent,
getSeleniumMetLifeSessionStatus,
} from "../../services/seleniumMetLifeEligibilityClient";
import {
splitName,
createOrUpdatePatientByInsuranceId,
imageToPdfBuffer,
} from "./_shared";
import { io } from "../../socket";
// ─── Helpers ────────────────────────────────────────────────────────────────
function now() {
return new Date().toISOString();
}
function log(tag: string, msg: string, ctx?: any) {
console.log(`${now()} [${tag}] ${msg}`, ctx ?? "");
}
function emitToSocket(socketId: string | undefined, event: string, payload: any) {
if (!socketId || !io) return;
try {
const socket = io.sockets.sockets.get(socketId);
if (socket) {
socket.emit(event, payload);
log("metlife-processor", `emitted ${event}`, { socketId });
}
} catch (err: any) {
log("metlife-processor", `emit failed for ${event}`, { err: err?.message });
}
}
// ─── Types ───────────────────────────────────────────────────────────────────
export interface MetLifeEligibilityProcessorInput {
enrichedPayload: any;
userId: number;
insuranceId: string;
formFirstName?: string;
formLastName?: string;
formDob?: string;
socketId?: string;
}
export interface MetLifeEligibilityProcessorResult {
patientUpdateStatus?: string;
pdfUploadStatus?: string;
pdfFileId?: number | string | null;
pdfFilename?: string | null;
}
// ─── Core DB processing ──────────────────────────────────────────────────────
async function processMetLifeResult(
userId: number,
insuranceId: string,
formFirstName: string | undefined,
formLastName: string | undefined,
formDob: string | undefined,
seleniumResult: any
): Promise<MetLifeEligibilityProcessorResult> {
const output: MetLifeEligibilityProcessorResult = {};
let createdPdfFileId: number | string | null = null;
try {
// 1) Resolve patient name (prefer selenium extraction → form data)
const rawName =
typeof seleniumResult?.patientName === "string"
? seleniumResult.patientName.trim()
: null;
let firstName: string;
let lastName: string;
if (rawName) {
// Strip any trailing bare date e.g. "Christian Rodriguez 04/27/2026"
const cleanName = rawName.replace(/\s+\d{1,2}\/\d{1,2}\/\d{2,4}$/, "").trim();
if (cleanName.includes(",")) {
// "LAST, FIRST" format common on insurance portals
const [last, ...firstParts] = cleanName.split(",").map((s: string) => s.trim());
lastName = last || formLastName || "";
firstName = firstParts.join(" ").trim() || formFirstName || "";
} else {
const parsed = splitName(cleanName);
if (!parsed.lastName) {
// Single word — treat as last name, pull first name from form
lastName = parsed.firstName || formLastName || "";
firstName = formFirstName || "";
} else {
firstName = parsed.firstName || formFirstName || "";
lastName = parsed.lastName || formLastName || "";
}
}
} else {
firstName = formFirstName ?? "";
lastName = formLastName ?? "";
}
// 2) Create / update patient
const upsertedPatient = await createOrUpdatePatientByInsuranceId({
insuranceId,
firstName,
lastName,
dob: formDob,
userId,
});
// 3) Fetch patient (needed for ID)
const normalizedInsuranceId = insuranceId.replace(/\s+/g, "");
// Use the patient just created/updated (family members share an insuranceId, so an
// id-only lookup could return a different member).
const dobForLookup = formDob ? new Date(formDob) : null;
const patient =
upsertedPatient ??
(dobForLookup && !isNaN(dobForLookup.getTime())
? await storage.getPatientByInsuranceIdAndDob(normalizedInsuranceId, dobForLookup)
: await storage.getPatientByInsuranceId(normalizedInsuranceId));
log("metlife-processor", `patient lookup after create: ${patient ? `id=${patient.id}` : "NOT FOUND"} for insuranceId="${normalizedInsuranceId}"`);
if (!patient?.id) {
output.patientUpdateStatus = "Patient not found; no update performed";
return output;
}
// 4) Determine and update eligibility status + insurance provider name
const eligStatus = (seleniumResult?.eligibility ?? "").toLowerCase();
let newStatus = "UNKNOWN";
if (eligStatus === "active" || eligStatus === "y") newStatus = "ACTIVE";
else if (eligStatus === "inactive" || eligStatus === "n" || eligStatus === "termed") newStatus = "INACTIVE";
await storage.updatePatient(patient.id, {
status: newStatus,
insuranceProvider: "MetLife Dental",
});
output.patientUpdateStatus = `Patient status updated to ${newStatus}`;
// 5) Resolve PDF buffer
// Worker returns a real PDF via pdf_path / ss_path (.pdf); screenshots are converted.
let pdfBuffer: Buffer | null = null;
let pdfFilename: string | null = null;
const pdfPath: string | null =
seleniumResult?.pdf_path ?? seleniumResult?.ss_path ?? null;
if (pdfPath && fsSync.existsSync(pdfPath)) {
if (pdfPath.endsWith(".pdf")) {
// Already a PDF — read directly
try {
pdfBuffer = await fs.readFile(pdfPath);
pdfFilename = path.basename(pdfPath);
log("metlife-processor", "read PDF directly", { pdfPath });
} catch (e: any) {
output.pdfUploadStatus = `Failed to read PDF: ${e.message}`;
}
} else if (
pdfPath.endsWith(".png") ||
pdfPath.endsWith(".jpg") ||
pdfPath.endsWith(".jpeg")
) {
// Legacy screenshot → convert to PDF
try {
pdfBuffer = await imageToPdfBuffer(pdfPath);
pdfFilename = `metlife_eligibility_${insuranceId}_${Date.now()}.pdf`;
log("metlife-processor", "converted screenshot to PDF", { pdfPath });
} catch (e: any) {
output.pdfUploadStatus = `Failed to convert screenshot to PDF: ${e.message}`;
}
}
} else {
output.pdfUploadStatus = "No valid file path from Selenium; nothing uploaded.";
}
// 6) Save PDF to patient document group
if (pdfBuffer && pdfFilename) {
const patientName = `${patient.firstName ?? ""} ${patient.lastName ?? ""}`.trim() || `Patient ${patient.id}`;
const cloudFile = await storage.savePdfToCloudStorage(
userId,
patient.id,
patientName,
"Eligibility",
pdfFilename,
pdfBuffer
);
createdPdfFileId = `cloud:${cloudFile.id}`;
output.pdfUploadStatus = "PDF saved to Eligibility folder";
output.pdfFilename = pdfFilename;
}
output.pdfFileId = createdPdfFileId;
return output;
} catch (err: any) {
log("metlife-processor", `processMetLifeResult ERROR: ${err?.message ?? String(err)}`, err);
return {
...output,
pdfUploadStatus:
output.pdfUploadStatus ?? `Processing failed: ${err?.message ?? String(err)}`,
pdfFileId: createdPdfFileId,
};
} finally {
// Always clean up temp files
const cleanupPath =
seleniumResult?.pdf_path ?? seleniumResult?.ss_path ?? null;
if (cleanupPath) {
try {
await emptyFolderContainingFile(cleanupPath);
} catch (e) {
log("metlife-processor", "cleanup failed", { cleanupPath });
}
}
}
}
// ─── Polling loop ────────────────────────────────────────────────────────────
async function pollUntilDone(
sessionId: string,
socketId: string | undefined,
jobId: string,
pollTimeoutMs = 5 * 60 * 1000 // 5 min total (includes OTP wait)
): Promise<any> {
const maxAttempts = 600; // 600 × 500ms = 5 min
const pollIntervalMs = 500;
const maxTransientErrors = 12;
const noProgressLimit = 120; // 60 s of same status → abort
let transientErrors = 0;
let consecutiveNoProgress = 0;
let lastStatus: string | null = null;
const deadline = Date.now() + pollTimeoutMs;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
if (Date.now() > deadline) {
throw new Error(
`MetLife polling timeout (${Math.round(pollTimeoutMs / 1000)}s) for session ${sessionId}`
);
}
try {
const st = await getSeleniumMetLifeSessionStatus(sessionId);
const status: string = st?.status ?? "unknown";
log("metlife-processor", `poll attempt=${attempt}`, { sessionId, status });
transientErrors = 0; // reset on success
// Track no-progress
const isTerminal =
status === "completed" || status === "error" || status === "not_found";
// waiting_for_otp legitimately sits unchanged while the user finds the code
if (status === lastStatus && !isTerminal && status !== "waiting_for_otp") {
consecutiveNoProgress++;
} else {
consecutiveNoProgress = 0;
}
lastStatus = status;
if (consecutiveNoProgress >= noProgressLimit) {
throw new Error(
`No progress from Python agent (status="${status}") after ${consecutiveNoProgress} polls`
);
}
// OTP required — notify frontend and keep polling
if (status === "waiting_for_otp") {
emitToSocket(socketId, "selenium:otp_required", {
session_id: sessionId,
jobId,
message: "OTP required. Please enter the code sent by the MetLife portal.",
});
await new Promise((r) => setTimeout(r, pollIntervalMs));
continue;
}
if (status === "completed") {
log("metlife-processor", "session completed", { sessionId });
return st.result;
}
if (status === "error" || status === "not_found") {
throw new Error(
st?.message || `MetLife session ended with status: ${status}`
);
}
// Still running / otp_submitted / created — keep polling
await new Promise((r) => setTimeout(r, pollIntervalMs));
} catch (err: any) {
// Propagate terminal errors immediately
const isTerminal =
err?.response?.status === 404 ||
(typeof err?.message === "string" &&
(err.message.includes("not_found") ||
err.message.includes("polling timeout") ||
err.message.includes("No progress")));
if (isTerminal) throw err;
// Transient network errors — back off
transientErrors++;
if (transientErrors > maxTransientErrors) {
throw new Error(
`Too many transient network errors polling MetLife session ${sessionId}`
);
}
const backoff = Math.min(30_000, 500 * Math.pow(2, transientErrors - 1));
log("metlife-processor", `transient error #${transientErrors}, backoff ${backoff}ms`, {
err: err?.message,
});
await new Promise((r) => setTimeout(r, backoff));
}
}
throw new Error(`MetLife polling exhausted all attempts for session ${sessionId}`);
}
// ─── Main processor entry point ───────────────────────────────────────────────
export async function runMetLifeEligibilityProcessor(
input: MetLifeEligibilityProcessorInput,
jobId: string
): Promise<MetLifeEligibilityProcessorResult> {
const {
enrichedPayload,
userId,
insuranceId,
formFirstName,
formLastName,
formDob,
socketId,
} = input;
// 1) Tell Python agent to start a MetLife session
log("metlife-processor", "starting Python agent session", { insuranceId });
const agentResp = await forwardToSeleniumMetLifeEligibilityAgent(enrichedPayload);
if (!agentResp?.session_id) {
throw new Error(
"Python agent did not return a session_id for MetLife eligibility"
);
}
const sessionId = agentResp.session_id as string;
log("metlife-processor", "got session_id", { sessionId });
// 2) Emit session started so frontend can store session_id for OTP submission
emitToSocket(socketId, "selenium:metlife_session_started", {
session_id: sessionId,
jobId,
});
// 3) Poll until done (handles OTP events internally)
const seleniumResult = await pollUntilDone(sessionId, socketId, jobId);
if (!seleniumResult || seleniumResult.status === "error") {
throw new Error(
seleniumResult?.message ?? "MetLife session returned an error result"
);
}
// 4) Process DB writes and PDF upload
log("metlife-processor", "processing DB result", { insuranceId });
const result = await processMetLifeResult(
userId,
insuranceId,
formFirstName,
formLastName,
formDob,
seleniumResult
);
log("metlife-processor", "done", { result });
return result;
}
+2 -1
View File
@@ -25,7 +25,8 @@ export type SeleniumJobType =
| "tuftssco-eligibility-check"
| "mh-eligibility-history-check"
| "cmsp-eligibility-history-remaining-check"
| "bcbs-ma-eligibility-check";
| "bcbs-ma-eligibility-check"
| "metlife-eligibility-check";
export interface SeleniumJobData {
jobType: SeleniumJobType;
+2
View File
@@ -48,6 +48,7 @@ import shoppingVendorsRoutes from "./shopping-vendors";
import feeScheduleRoutes from "./feeSchedule";
import licenseRoutes from "./license";
import insuranceStatusBcbsMaRoutes from "./insuranceStatusBcbsMa";
import insuranceStatusMetLifeRoutes from "./insuranceStatusMetLife";
import labRxRoutes from "./lab-rx";
import seleniumSettingsRoutes from "./selenium-settings";
import typeAgentRoutes from "./type-agent";
@@ -75,6 +76,7 @@ router.use("/insurance-status-uniteducard", insuranceStatusUnitedUcardRoutes);
router.use("/insurance-status-tuftssco", insuranceStatusTuftsSCORoutes);
router.use("/insurance-status-cca", insuranceStatusCCARoutes);
router.use("/insurance-status-bcbs-ma", insuranceStatusBcbsMaRoutes);
router.use("/insurance-status-metlife", insuranceStatusMetLifeRoutes);
router.use("/claims", insuranceStatusCCAClaimRoutes);
router.use("/claims", insuranceStatusCCAPreAuthRoutes);
router.use("/claims", insuranceStatusDDMAClaimRoutes);
@@ -0,0 +1,138 @@
import { Router, Request, Response } from "express";
import { storage } from "../storage";
import { forwardOtpToSeleniumMetLifeAgent } from "../services/seleniumMetLifeEligibilityClient";
import { io } from "../socket";
import { enqueueSeleniumJob } from "../queue/jobRunner";
const router = Router();
function log(tag: string, msg: string, ctx?: any) {
console.log(`${new Date().toISOString()} [${tag}] ${msg}`, ctx ?? "");
}
function emitSafe(socketId: string | undefined, event: string, payload: any) {
if (!socketId || !io) return;
try {
const socket = io.sockets.sockets.get(socketId);
if (socket) socket.emit(event, payload);
} catch (err: any) {
log("socket", "emit failed", { socketId, event, err: err?.message });
}
}
/**
* POST /metlife-eligibility
*
* Enqueues a MetLife Dental eligibility check in the shared InProcessQueue
* (concurrency=1, mirrors the Python semaphore).
*
* Body:
* data — patient + search fields (memberId, dateOfBirth, …)
* socketId — socket.io client id for real-time updates
*
* Response: { status: "queued", jobId: "…" }
*
* Real-time events emitted to socketId during job execution:
* job:update { jobId, jobType, status, … }
* selenium:metlife_session_started { session_id, jobId }
* selenium:otp_required { session_id, jobId, message }
*/
router.post(
"/metlife-eligibility",
async (req: Request, res: Response): Promise<any> => {
if (!req.body.data) {
return res
.status(400)
.json({ error: "Missing Insurance Eligibility data for selenium" });
}
if (!req.user?.id) {
return res.status(401).json({ error: "Unauthorized: user info missing" });
}
try {
const rawData =
typeof req.body.data === "string"
? JSON.parse(req.body.data)
: req.body.data;
const credentials = await storage.getInsuranceCredentialByUserAndSiteKey(
req.user.id,
rawData.insuranceSiteKey
);
if (!credentials) {
return res.status(404).json({
error:
"No MetLife credentials found for this provider. Please add them at the Settings page.",
});
}
const enrichedData = {
...rawData,
metlifeUsername: credentials.username,
metlifePassword: credentials.password,
};
const socketId: string | undefined = req.body.socketId;
const jobId = enqueueSeleniumJob({
jobType: "metlife-eligibility-check",
userId: req.user.id,
socketId,
enrichedPayload: enrichedData,
insuranceId: String(rawData.memberId ?? "").trim(),
formFirstName: rawData.firstName,
formLastName: rawData.lastName,
formDob: rawData.dateOfBirth,
});
log("metlife-route", "job enqueued", { jobId, insuranceId: rawData.memberId });
return res.json({ status: "queued", jobId });
} catch (err: any) {
console.error("[metlife-route] enqueue failed:", err);
return res.status(500).json({
error: err.message || "Failed to enqueue MetLife selenium job",
});
}
}
);
/**
* POST /selenium/submit-otp
*
* Forwards the OTP entered by the user directly to the Python agent
* (side-channel — does NOT go through the queue).
*
* Body: { session_id, otp, socketId? }
*/
router.post(
"/selenium/submit-otp",
async (req: Request, res: Response): Promise<any> => {
const { session_id: sessionId, otp, socketId } = req.body;
if (!sessionId || !otp) {
return res.status(400).json({ error: "session_id and otp are required" });
}
try {
const r = await forwardOtpToSeleniumMetLifeAgent(sessionId, otp);
emitSafe(socketId, "selenium:otp_submitted", {
session_id: sessionId,
result: r,
});
return res.json(r);
} catch (err: any) {
console.error(
"[metlife-route] submit-otp failed:",
err?.response?.data || err?.message || err
);
return res.status(500).json({
error: "Failed to forward OTP to selenium agent",
detail: err?.message || err,
});
}
}
);
export default router;
@@ -0,0 +1,122 @@
import axios from "axios";
import http from "http";
import https from "https";
import dotenv from "dotenv";
dotenv.config();
export interface SeleniumPayload {
data: any;
url?: string;
}
const SELENIUM_AGENT_BASE = process.env.SELENIUM_AGENT_BASE_URL;
const httpAgent = new http.Agent({ keepAlive: true, keepAliveMsecs: 60_000 });
const httpsAgent = new https.Agent({ keepAlive: true, keepAliveMsecs: 60_000 });
const client = axios.create({
baseURL: SELENIUM_AGENT_BASE,
timeout: 5 * 60 * 1000,
httpAgent,
httpsAgent,
validateStatus: (s) => s >= 200 && s < 600,
});
async function requestWithRetries(
config: any,
retries = 4,
baseBackoffMs = 300
) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const r = await client.request(config);
if (![502, 503, 504].includes(r.status)) return r;
console.warn(
`[selenium-client] retryable HTTP status ${r.status} (attempt ${attempt})`
);
} catch (err: any) {
const code = err?.code;
const isTransient =
code === "ECONNRESET" ||
code === "ECONNREFUSED" ||
code === "EPIPE" ||
code === "ETIMEDOUT";
if (!isTransient) throw err;
console.warn(
`[selenium-client] transient network error ${code} (attempt ${attempt})`
);
}
await new Promise((r) => setTimeout(r, baseBackoffMs * attempt));
}
// final attempt (let exception bubble if it fails)
return client.request(config);
}
function now() {
return new Date().toISOString();
}
function log(tag: string, msg: string, ctx?: any) {
console.log(`${now()} [${tag}] ${msg}`, ctx ?? "");
}
export async function forwardToSeleniumMetLifeEligibilityAgent(
insuranceEligibilityData: any
): Promise<any> {
const payload = { data: insuranceEligibilityData };
const url = `/metlife-eligibility`;
log("selenium-client", "POST metlife-eligibility", {
url: SELENIUM_AGENT_BASE + url,
keys: Object.keys(payload),
});
const r = await requestWithRetries({ url, method: "POST", data: payload }, 4);
log("selenium-client", "agent response", {
status: r.status,
dataKeys: r.data ? Object.keys(r.data) : null,
});
if (r.status >= 500)
throw new Error(`Selenium agent server error: ${r.status}`);
return r.data;
}
export async function forwardOtpToSeleniumMetLifeAgent(
sessionId: string,
otp: string
): Promise<any> {
const url = `/submit-otp`;
log("selenium-client", "POST submit-otp", {
url: SELENIUM_AGENT_BASE + url,
sessionId,
});
const r = await requestWithRetries(
{ url, method: "POST", data: { session_id: sessionId, otp } },
4
);
log("selenium-client", "submit-otp response", {
status: r.status,
data: r.data,
});
if (r.status >= 500)
throw new Error(`Selenium agent server error on submit-otp: ${r.status}`);
return r.data;
}
export async function getSeleniumMetLifeSessionStatus(
sessionId: string
): Promise<any> {
const url = `/session/${sessionId}/status`;
log("selenium-client", "GET session status", {
url: SELENIUM_AGENT_BASE + url,
sessionId,
});
const r = await requestWithRetries({ url, method: "GET" }, 4);
log("selenium-client", "session status response", {
status: r.status,
dataKeys: r.data ? Object.keys(r.data) : null,
});
if (r.status === 404) {
const e: any = new Error("not_found");
e.response = { status: 404, data: r.data };
throw e;
}
return r.data;
}