feat(ddma eligbility) - v4 scripts
This commit is contained in:
@@ -6,16 +6,16 @@ import {
|
|||||||
getSeleniumDdmaSessionStatus,
|
getSeleniumDdmaSessionStatus,
|
||||||
} from "../services/seleniumDdmaInsuranceEligibilityClient";
|
} from "../services/seleniumDdmaInsuranceEligibilityClient";
|
||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
|
import fsSync from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import PDFDocument from "pdfkit";
|
||||||
import { emptyFolderContainingFile } from "../utils/emptyTempFolder";
|
import { emptyFolderContainingFile } from "../utils/emptyTempFolder";
|
||||||
import forwardToPatientDataExtractorService from "../services/patientDataExtractorService";
|
|
||||||
import {
|
import {
|
||||||
InsertPatient,
|
InsertPatient,
|
||||||
insertPatientSchema,
|
insertPatientSchema,
|
||||||
} from "../../../../packages/db/types/patient-types";
|
} from "../../../../packages/db/types/patient-types";
|
||||||
import { io } from "../socket";
|
import { io } from "../socket";
|
||||||
|
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
/** Job context stored in memory by sessionId */
|
/** Job context stored in memory by sessionId */
|
||||||
@@ -35,6 +35,34 @@ function splitName(fullName?: string | null) {
|
|||||||
return { firstName, lastName };
|
return { firstName, lastName };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function imageToPdfBuffer(imagePath: string): Promise<Buffer> {
|
||||||
|
return new Promise<Buffer>((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const doc = new PDFDocument({ autoFirstPage: false });
|
||||||
|
const chunks: Uint8Array[] = [];
|
||||||
|
|
||||||
|
doc.on("data", (chunk: any) => chunks.push(chunk));
|
||||||
|
doc.on("end", () => resolve(Buffer.concat(chunks)));
|
||||||
|
doc.on("error", (err: any) => reject(err));
|
||||||
|
|
||||||
|
const A4_WIDTH = 595.28; // points
|
||||||
|
const A4_HEIGHT = 841.89; // points
|
||||||
|
|
||||||
|
doc.addPage({ size: [A4_WIDTH, A4_HEIGHT] });
|
||||||
|
|
||||||
|
doc.image(imagePath, 0, 0, {
|
||||||
|
fit: [A4_WIDTH, A4_HEIGHT],
|
||||||
|
align: "center",
|
||||||
|
valign: "center",
|
||||||
|
});
|
||||||
|
|
||||||
|
doc.end();
|
||||||
|
} catch (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure patient exists for given insuranceId.
|
* Ensure patient exists for given insuranceId.
|
||||||
*/
|
*/
|
||||||
@@ -85,10 +113,6 @@ async function createOrUpdatePatientByInsuranceId(options: {
|
|||||||
try {
|
try {
|
||||||
patientData = insertPatientSchema.parse(createPayload);
|
patientData = insertPatientSchema.parse(createPayload);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(
|
|
||||||
"Failed to validate patient payload in ddma insurance flow:",
|
|
||||||
err
|
|
||||||
);
|
|
||||||
const safePayload = { ...createPayload };
|
const safePayload = { ...createPayload };
|
||||||
delete (safePayload as any).dateOfBirth;
|
delete (safePayload as any).dateOfBirth;
|
||||||
patientData = insertPatientSchema.parse(safePayload);
|
patientData = insertPatientSchema.parse(safePayload);
|
||||||
@@ -108,125 +132,175 @@ async function handleDdmaCompletedJob(
|
|||||||
) {
|
) {
|
||||||
let createdPdfFileId: number | null = null;
|
let createdPdfFileId: number | null = null;
|
||||||
const outputResult: any = {};
|
const outputResult: any = {};
|
||||||
const extracted: any = {};
|
|
||||||
|
|
||||||
const insuranceEligibilityData = job.insuranceEligibilityData;
|
const insuranceEligibilityData = job.insuranceEligibilityData;
|
||||||
|
|
||||||
// 1) Extract name from PDF if available
|
// We'll wrap the processing in try/catch/finally so cleanup always runs
|
||||||
if (
|
try {
|
||||||
seleniumResult?.pdf_path &&
|
// 1) ensuring memberid.
|
||||||
typeof seleniumResult.pdf_path === "string" &&
|
const insuranceId = String(insuranceEligibilityData.memberId ?? "").trim();
|
||||||
seleniumResult.pdf_path.endsWith(".pdf")
|
if (!insuranceId) {
|
||||||
) {
|
throw new Error("Missing memberId for ddma job");
|
||||||
try {
|
|
||||||
const pdfPath = seleniumResult.pdf_path;
|
|
||||||
const pdfBuffer = await fs.readFile(pdfPath);
|
|
||||||
|
|
||||||
const extraction = await forwardToPatientDataExtractorService({
|
|
||||||
buffer: pdfBuffer,
|
|
||||||
originalname: path.basename(pdfPath),
|
|
||||||
mimetype: "application/pdf",
|
|
||||||
} as any);
|
|
||||||
|
|
||||||
if (extraction.name) {
|
|
||||||
const parts = splitName(extraction.name);
|
|
||||||
extracted.firstName = parts.firstName;
|
|
||||||
extracted.lastName = parts.lastName;
|
|
||||||
}
|
|
||||||
} catch (err: any) {
|
|
||||||
outputResult.extractionError =
|
|
||||||
err?.message ?? "Patient data extraction failed";
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 2) Create or update patient
|
// 2) Create or update patient
|
||||||
const insuranceId = String(insuranceEligibilityData.memberId ?? "").trim();
|
await createOrUpdatePatientByInsuranceId({
|
||||||
if (!insuranceId) {
|
insuranceId,
|
||||||
throw new Error("Missing memberId for ddma job");
|
dob: insuranceEligibilityData.dateOfBirth,
|
||||||
}
|
userId: job.userId,
|
||||||
|
});
|
||||||
|
|
||||||
const preferFirst = extracted.firstName;
|
// 3) Update patient status + PDF upload
|
||||||
const preferLast = extracted.lastName;
|
const patient = await storage.getPatientByInsuranceId(
|
||||||
|
insuranceEligibilityData.memberId
|
||||||
|
);
|
||||||
|
|
||||||
await createOrUpdatePatientByInsuranceId({
|
if (patient && patient.id !== undefined) {
|
||||||
insuranceId,
|
const newStatus =
|
||||||
firstName: preferFirst,
|
seleniumResult.eligibility === "active" ? "ACTIVE" : "INACTIVE";
|
||||||
lastName: preferLast,
|
await storage.updatePatient(patient.id, { status: newStatus });
|
||||||
dob: insuranceEligibilityData.dateOfBirth,
|
outputResult.patientUpdateStatus = `Patient status updated to ${newStatus}`;
|
||||||
userId: job.userId,
|
|
||||||
});
|
|
||||||
|
|
||||||
// 3) Update patient status + PDF upload
|
// Expect only ss_path (screenshot)
|
||||||
const patient = await storage.getPatientByInsuranceId(
|
let pdfBuffer: Buffer | null = null;
|
||||||
insuranceEligibilityData.memberId
|
let generatedPdfPath: string | null = null;
|
||||||
);
|
|
||||||
|
|
||||||
if (patient && patient.id !== undefined) {
|
if (
|
||||||
const newStatus =
|
seleniumResult &&
|
||||||
seleniumResult.eligibility === "Y" ? "ACTIVE" : "INACTIVE";
|
seleniumResult.ss_path &&
|
||||||
await storage.updatePatient(patient.id, { status: newStatus });
|
typeof seleniumResult.ss_path === "string" &&
|
||||||
outputResult.patientUpdateStatus = `Patient status updated to ${newStatus}`;
|
(seleniumResult.ss_path.endsWith(".png") ||
|
||||||
|
seleniumResult.ss_path.endsWith(".jpg") ||
|
||||||
|
seleniumResult.ss_path.endsWith(".jpeg"))
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
if (!fsSync.existsSync(seleniumResult.ss_path)) {
|
||||||
|
throw new Error(
|
||||||
|
`Screenshot file not found: ${seleniumResult.ss_path}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
pdfBuffer = await imageToPdfBuffer(seleniumResult.ss_path);
|
||||||
seleniumResult.pdf_path &&
|
|
||||||
typeof seleniumResult.pdf_path === "string" &&
|
|
||||||
seleniumResult.pdf_path.endsWith(".pdf")
|
|
||||||
) {
|
|
||||||
const pdfBuffer = await fs.readFile(seleniumResult.pdf_path);
|
|
||||||
|
|
||||||
const groupTitle = "Eligibility Status";
|
const pdfFileName = `ddma_eligibility_${insuranceEligibilityData.memberId}_${Date.now()}.pdf`;
|
||||||
const groupTitleKey = "ELIGIBILITY_STATUS";
|
generatedPdfPath = path.join(
|
||||||
|
path.dirname(seleniumResult.ss_path),
|
||||||
|
pdfFileName
|
||||||
|
);
|
||||||
|
await fs.writeFile(generatedPdfPath, pdfBuffer);
|
||||||
|
|
||||||
let group = await storage.findPdfGroupByPatientTitleKey(
|
// ensure cleanup uses this
|
||||||
patient.id,
|
seleniumResult.pdf_path = generatedPdfPath;
|
||||||
groupTitleKey
|
} catch (err: any) {
|
||||||
);
|
console.error("Failed to convert screenshot to PDF:", err);
|
||||||
if (!group) {
|
outputResult.pdfUploadStatus = `Failed to convert screenshot to PDF: ${String(err)}`;
|
||||||
group = await storage.createPdfGroup(
|
}
|
||||||
|
} else {
|
||||||
|
outputResult.pdfUploadStatus =
|
||||||
|
"No valid screenshot (ss_path) provided by Selenium; nothing to upload.";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pdfBuffer && generatedPdfPath) {
|
||||||
|
const groupTitle = "Eligibility Status";
|
||||||
|
const groupTitleKey = "ELIGIBILITY_STATUS";
|
||||||
|
|
||||||
|
let group = await storage.findPdfGroupByPatientTitleKey(
|
||||||
patient.id,
|
patient.id,
|
||||||
groupTitle,
|
|
||||||
groupTitleKey
|
groupTitleKey
|
||||||
);
|
);
|
||||||
}
|
if (!group) {
|
||||||
if (!group?.id) {
|
group = await storage.createPdfGroup(
|
||||||
throw new Error("PDF group creation failed: missing group ID");
|
patient.id,
|
||||||
}
|
groupTitle,
|
||||||
|
groupTitleKey
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!group?.id) {
|
||||||
|
throw new Error("PDF group creation failed: missing group ID");
|
||||||
|
}
|
||||||
|
|
||||||
const created = await storage.createPdfFile(
|
const created = await storage.createPdfFile(
|
||||||
group.id,
|
group.id,
|
||||||
path.basename(seleniumResult.pdf_path),
|
path.basename(generatedPdfPath),
|
||||||
pdfBuffer
|
pdfBuffer
|
||||||
);
|
);
|
||||||
if (created && typeof created === "object" && "id" in created) {
|
if (created && typeof created === "object" && "id" in created) {
|
||||||
createdPdfFileId = Number(created.id);
|
createdPdfFileId = Number(created.id);
|
||||||
|
}
|
||||||
|
outputResult.pdfUploadStatus = `PDF saved to group: ${group.title}`;
|
||||||
|
} else {
|
||||||
|
outputResult.pdfUploadStatus =
|
||||||
|
"No valid PDF path provided by Selenium, Couldn't upload pdf to server.";
|
||||||
}
|
}
|
||||||
outputResult.pdfUploadStatus = `PDF saved to group: ${group.title}`;
|
|
||||||
} else {
|
} else {
|
||||||
outputResult.pdfUploadStatus =
|
outputResult.patientUpdateStatus =
|
||||||
"No valid PDF path provided by Selenium, Couldn't upload pdf to server.";
|
"Patient not found or missing ID; no update performed";
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
outputResult.patientUpdateStatus =
|
|
||||||
"Patient not found or missing ID; no update performed";
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4) Cleanup PDF temp folder
|
return {
|
||||||
|
patientUpdateStatus: outputResult.patientUpdateStatus,
|
||||||
|
pdfUploadStatus: outputResult.pdfUploadStatus,
|
||||||
|
pdfFileId: createdPdfFileId,
|
||||||
|
};
|
||||||
|
} catch (err: any) {
|
||||||
|
return {
|
||||||
|
patientUpdateStatus: outputResult.patientUpdateStatus,
|
||||||
|
pdfUploadStatus:
|
||||||
|
outputResult.pdfUploadStatus ??
|
||||||
|
`Failed to process DDMA job: ${err?.message ?? String(err)}`,
|
||||||
|
pdfFileId: createdPdfFileId,
|
||||||
|
error: err?.message ?? String(err),
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
// ALWAYS attempt cleanup of temp files
|
||||||
|
try {
|
||||||
|
if (seleniumResult && seleniumResult.pdf_path) {
|
||||||
|
await emptyFolderContainingFile(seleniumResult.pdf_path);
|
||||||
|
} else if (seleniumResult && seleniumResult.ss_path) {
|
||||||
|
await emptyFolderContainingFile(seleniumResult.ss_path);
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
`[ddma-eligibility] no pdf_path or ss_path available to cleanup`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (cleanupErr) {
|
||||||
|
console.error(
|
||||||
|
`[ddma-eligibility cleanup failed for ${seleniumResult?.pdf_path ?? seleniumResult?.ss_path}]`,
|
||||||
|
cleanupErr
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- top of file, alongside ddmaJobs ---
|
||||||
|
const finalResults: Record<string, any> = {};
|
||||||
|
|
||||||
|
function now() {
|
||||||
|
return new Date().toISOString();
|
||||||
|
}
|
||||||
|
function log(tag: string, msg: string, ctx?: any) {
|
||||||
|
console.log(`${now()} [${tag}] ${msg}`, ctx ?? "");
|
||||||
|
}
|
||||||
|
|
||||||
|
function emitSafe(socketId: string | undefined, event: string, payload: any) {
|
||||||
|
if (!socketId) {
|
||||||
|
log("socket", "no socketId for emit", { event });
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
if (seleniumResult && seleniumResult.pdf_path) {
|
const socket = io?.sockets.sockets.get(socketId);
|
||||||
await emptyFolderContainingFile(seleniumResult.pdf_path);
|
if (!socket) {
|
||||||
|
log("socket", "socket not found (maybe disconnected)", {
|
||||||
|
socketId,
|
||||||
|
event,
|
||||||
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} catch (cleanupErr) {
|
socket.emit(event, payload);
|
||||||
console.error(
|
log("socket", "emitted", { socketId, event });
|
||||||
`[ddma-eligibility cleanup failed for ${seleniumResult?.pdf_path}]`,
|
} catch (err: any) {
|
||||||
cleanupErr
|
log("socket", "emit failed", { socketId, event, err: err?.message });
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
|
||||||
patientUpdateStatus: outputResult.patientUpdateStatus,
|
|
||||||
pdfUploadStatus: outputResult.pdfUploadStatus,
|
|
||||||
pdfFileId: createdPdfFileId,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -238,29 +312,66 @@ async function pollAgentSessionAndProcess(
|
|||||||
sessionId: string,
|
sessionId: string,
|
||||||
socketId?: string
|
socketId?: string
|
||||||
) {
|
) {
|
||||||
const maxAttempts = 300; // ~5 minutes @ 1s
|
const maxAttempts = 300; // ~5 minutes @ 1s base (adjust if needed)
|
||||||
const delayMs = 1000;
|
const baseDelayMs = 1000;
|
||||||
|
const maxTransientErrors = 12; // tolerate more transient errors
|
||||||
|
|
||||||
const job = ddmaJobs[sessionId];
|
const job = ddmaJobs[sessionId];
|
||||||
|
let transientErrorCount = 0;
|
||||||
|
|
||||||
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
||||||
|
const attemptTs = new Date().toISOString();
|
||||||
|
log(
|
||||||
|
"poller",
|
||||||
|
`attempt=${attempt} session=${sessionId} transientErrCount=${transientErrorCount}`
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const st = await getSeleniumDdmaSessionStatus(sessionId);
|
const st = await getSeleniumDdmaSessionStatus(sessionId);
|
||||||
const status = st?.status;
|
const status = st?.status;
|
||||||
|
log("poller", "got status", {
|
||||||
|
sessionId,
|
||||||
|
status,
|
||||||
|
message: st?.message,
|
||||||
|
resultKeys: st?.result ? Object.keys(st.result) : null,
|
||||||
|
});
|
||||||
|
|
||||||
|
// reset transient errors on success
|
||||||
|
transientErrorCount = 0;
|
||||||
|
|
||||||
|
// always emit debug to client if socket exists
|
||||||
|
emitSafe(socketId, "selenium:debug", {
|
||||||
|
session_id: sessionId,
|
||||||
|
attempt,
|
||||||
|
status,
|
||||||
|
serverTime: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// If agent is waiting for OTP, inform client but keep polling (do not return)
|
||||||
if (status === "waiting_for_otp") {
|
if (status === "waiting_for_otp") {
|
||||||
if (socketId && io && io.sockets.sockets.get(socketId)) {
|
emitSafe(socketId, "selenium:otp_required", {
|
||||||
io.to(socketId).emit("selenium:otp_required", {
|
session_id: sessionId,
|
||||||
session_id: sessionId,
|
message: "OTP required. Please enter the OTP.",
|
||||||
message: "OTP required. Please enter the OTP.",
|
});
|
||||||
});
|
// do not return — keep polling (allows same poller to pick up completion)
|
||||||
}
|
await new Promise((r) => setTimeout(r, baseDelayMs));
|
||||||
// once waiting_for_otp, we stop polling here; OTP flow continues separately
|
continue;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Completed path
|
||||||
if (status === "completed") {
|
if (status === "completed") {
|
||||||
// run DB + PDF pipeline
|
log("poller", "agent completed; processing result", {
|
||||||
|
sessionId,
|
||||||
|
resultKeys: st.result ? Object.keys(st.result) : null,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Persist raw result so frontend can fetch if socket disconnects
|
||||||
|
finalResults[sessionId] = {
|
||||||
|
rawSelenium: st.result,
|
||||||
|
processedAt: null,
|
||||||
|
final: null,
|
||||||
|
};
|
||||||
|
|
||||||
let finalResult: any = null;
|
let finalResult: any = null;
|
||||||
if (job && st.result) {
|
if (job && st.result) {
|
||||||
try {
|
try {
|
||||||
@@ -269,53 +380,120 @@ async function pollAgentSessionAndProcess(
|
|||||||
job,
|
job,
|
||||||
st.result
|
st.result
|
||||||
);
|
);
|
||||||
|
finalResults[sessionId].final = finalResult;
|
||||||
|
finalResults[sessionId].processedAt = Date.now();
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
finalResult = {
|
finalResults[sessionId].final = {
|
||||||
error: "Failed to process ddma completed job",
|
error: "processing_failed",
|
||||||
detail: err?.message ?? String(err),
|
detail: err?.message ?? String(err),
|
||||||
};
|
};
|
||||||
|
finalResults[sessionId].processedAt = Date.now();
|
||||||
|
log("poller", "handleDdmaCompletedJob failed", {
|
||||||
|
sessionId,
|
||||||
|
err: err?.message ?? err,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
finalResults[sessionId].final = { error: "no_job_or_no_result" };
|
||||||
|
finalResults[sessionId].processedAt = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (socketId && io && io.sockets.sockets.get(socketId)) {
|
// Emit final update (if socket present)
|
||||||
io.to(socketId).emit("selenium:session_update", {
|
emitSafe(socketId, "selenium:session_update", {
|
||||||
session_id: sessionId,
|
session_id: sessionId,
|
||||||
status: "completed",
|
status: "completed",
|
||||||
rawSelenium: st.result,
|
rawSelenium: st.result,
|
||||||
final: finalResult,
|
final: finalResults[sessionId].final,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
// cleanup job context
|
||||||
delete ddmaJobs[sessionId];
|
delete ddmaJobs[sessionId];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Terminal error / not_found
|
||||||
if (status === "error" || status === "not_found") {
|
if (status === "error" || status === "not_found") {
|
||||||
if (socketId && io && io.sockets.sockets.get(socketId)) {
|
const emitPayload = {
|
||||||
io.to(socketId).emit("selenium:session_update", {
|
session_id: sessionId,
|
||||||
session_id: sessionId,
|
status,
|
||||||
status,
|
message: st?.message || "Selenium session error",
|
||||||
message: st?.message || "Selenium session error",
|
};
|
||||||
});
|
emitSafe(socketId, "selenium:session_update", emitPayload);
|
||||||
}
|
emitSafe(socketId, "selenium:session_error", emitPayload);
|
||||||
delete ddmaJobs[sessionId];
|
delete ddmaJobs[sessionId];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
// swallow transient errors and keep polling
|
const axiosStatus =
|
||||||
console.warn("pollAgentSessionAndProcess error", err);
|
err?.response?.status ?? (err?.status ? Number(err.status) : undefined);
|
||||||
|
const errCode = err?.code ?? err?.errno;
|
||||||
|
const errMsg = err?.message ?? String(err);
|
||||||
|
const errData = err?.response?.data ?? null;
|
||||||
|
|
||||||
|
// If agent explicitly returned 404 -> terminal (session gone)
|
||||||
|
if (
|
||||||
|
axiosStatus === 404 ||
|
||||||
|
(typeof errMsg === "string" && errMsg.includes("not_found"))
|
||||||
|
) {
|
||||||
|
console.warn(
|
||||||
|
`${new Date().toISOString()} [poller] terminal 404/not_found for ${sessionId}: data=${JSON.stringify(errData)}`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Emit not_found to client
|
||||||
|
const emitPayload = {
|
||||||
|
session_id: sessionId,
|
||||||
|
status: "not_found",
|
||||||
|
message:
|
||||||
|
errData?.detail || "Selenium session not found (agent cleaned up).",
|
||||||
|
};
|
||||||
|
emitSafe(socketId, "selenium:session_update", emitPayload);
|
||||||
|
emitSafe(socketId, "selenium:session_error", emitPayload);
|
||||||
|
|
||||||
|
// Remove job context and stop polling
|
||||||
|
delete ddmaJobs[sessionId];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detailed transient error logging
|
||||||
|
transientErrorCount++;
|
||||||
|
const backoffMs = Math.min(
|
||||||
|
30_000,
|
||||||
|
baseDelayMs * Math.pow(2, transientErrorCount - 1)
|
||||||
|
);
|
||||||
|
console.warn(
|
||||||
|
`${new Date().toISOString()} [poller] transient error (#${transientErrorCount}) for ${sessionId}: code=${errCode} status=${axiosStatus} msg=${errMsg} data=${JSON.stringify(errData)}`
|
||||||
|
);
|
||||||
|
console.warn(
|
||||||
|
`${new Date().toISOString()} [poller] backing off ${backoffMs}ms before next attempt`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (transientErrorCount > maxTransientErrors) {
|
||||||
|
const emitPayload = {
|
||||||
|
session_id: sessionId,
|
||||||
|
status: "error",
|
||||||
|
message:
|
||||||
|
"Repeated network errors while polling selenium agent; giving up.",
|
||||||
|
};
|
||||||
|
emitSafe(socketId, "selenium:session_update", emitPayload);
|
||||||
|
emitSafe(socketId, "selenium:session_error", emitPayload);
|
||||||
|
delete ddmaJobs[sessionId];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await new Promise((r) => setTimeout(r, backoffMs));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
await new Promise((r) => setTimeout(r, delayMs));
|
// normal poll interval
|
||||||
|
await new Promise((r) => setTimeout(r, baseDelayMs));
|
||||||
}
|
}
|
||||||
|
|
||||||
// fallback: timeout
|
// overall timeout fallback
|
||||||
if (socketId && io && io.sockets.sockets.get(socketId)) {
|
emitSafe(socketId, "selenium:session_update", {
|
||||||
io.to(socketId).emit("selenium:session_update", {
|
session_id: sessionId,
|
||||||
session_id: sessionId,
|
status: "error",
|
||||||
status: "error",
|
message: "Polling timeout while waiting for selenium session",
|
||||||
message: "Polling timeout while waiting for selenium session",
|
});
|
||||||
});
|
delete ddmaJobs[sessionId];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -363,11 +541,14 @@ router.post(
|
|||||||
|
|
||||||
const socketId: string | undefined = req.body.socketId;
|
const socketId: string | undefined = req.body.socketId;
|
||||||
|
|
||||||
const agentResp = await forwardToSeleniumDdmaEligibilityAgent(
|
const agentResp =
|
||||||
enrichedData,
|
await forwardToSeleniumDdmaEligibilityAgent(enrichedData);
|
||||||
);
|
|
||||||
|
|
||||||
if (!agentResp || agentResp.status !== "started" || !agentResp.session_id) {
|
if (
|
||||||
|
!agentResp ||
|
||||||
|
agentResp.status !== "started" ||
|
||||||
|
!agentResp.session_id
|
||||||
|
) {
|
||||||
return res.status(502).json({
|
return res.status(502).json({
|
||||||
error: "Selenium agent did not return a started session",
|
error: "Selenium agent did not return a started session",
|
||||||
detail: agentResp,
|
detail: agentResp,
|
||||||
@@ -408,30 +589,24 @@ router.post(
|
|||||||
async (req: Request, res: Response): Promise<any> => {
|
async (req: Request, res: Response): Promise<any> => {
|
||||||
const { session_id: sessionId, otp, socketId } = req.body;
|
const { session_id: sessionId, otp, socketId } = req.body;
|
||||||
if (!sessionId || !otp) {
|
if (!sessionId || !otp) {
|
||||||
return res
|
return res.status(400).json({ error: "session_id and otp are required" });
|
||||||
.status(400)
|
|
||||||
.json({ error: "session_id and otp are required" });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const r = await forwardOtpToSeleniumDdmaAgent(sessionId, otp);
|
const r = await forwardOtpToSeleniumDdmaAgent(sessionId, otp);
|
||||||
|
|
||||||
// notify socket that OTP was accepted (if socketId present)
|
// emit OTP accepted (if socket present)
|
||||||
try {
|
emitSafe(socketId, "selenium:otp_submitted", {
|
||||||
const { io } = require("../socket");
|
session_id: sessionId,
|
||||||
if (socketId && io && io.sockets.sockets.get(socketId)) {
|
result: r,
|
||||||
io.to(socketId).emit("selenium:otp_submitted", {
|
});
|
||||||
session_id: sessionId,
|
|
||||||
result: r,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (emitErr) {
|
|
||||||
console.warn("Failed to emit selenium:otp_submitted", emitErr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.json(r);
|
return res.json(r);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error("Failed to forward OTP:", err?.response?.data || err?.message || err);
|
console.error(
|
||||||
|
"Failed to forward OTP:",
|
||||||
|
err?.response?.data || err?.message || err
|
||||||
|
);
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
error: "Failed to forward otp to selenium agent",
|
error: "Failed to forward otp to selenium agent",
|
||||||
detail: err?.message || err,
|
detail: err?.message || err,
|
||||||
@@ -440,4 +615,16 @@ router.post(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// GET /selenium/session/:sid/final
|
||||||
|
router.get(
|
||||||
|
"/selenium/session/:sid/final",
|
||||||
|
async (req: Request, res: Response) => {
|
||||||
|
const sid = req.params.sid;
|
||||||
|
if (!sid) return res.status(400).json({ error: "session id required" });
|
||||||
|
const f = finalResults[sid];
|
||||||
|
if (!f) return res.status(404).json({ error: "final result not found" });
|
||||||
|
return res.json(f);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import http from "http";
|
||||||
|
import https from "https";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
@@ -7,66 +9,114 @@ export interface SeleniumPayload {
|
|||||||
url?: string;
|
url?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SELENIUM_AGENT_BASE =
|
const SELENIUM_AGENT_BASE = process.env.SELENIUM_AGENT_BASE_URL;
|
||||||
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 forwardToSeleniumDdmaEligibilityAgent(
|
export async function forwardToSeleniumDdmaEligibilityAgent(
|
||||||
insuranceEligibilityData: any,
|
insuranceEligibilityData: any
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const payload: SeleniumPayload = {
|
const payload = { data: insuranceEligibilityData };
|
||||||
data: insuranceEligibilityData,
|
const url = `/ddma-eligibility`;
|
||||||
};
|
log("selenium-client", "POST ddma-eligibility", {
|
||||||
|
url: SELENIUM_AGENT_BASE + url,
|
||||||
const url = `${SELENIUM_AGENT_BASE}/ddma-eligibility`;
|
keys: Object.keys(payload),
|
||||||
console.log(url)
|
});
|
||||||
const result = await axios.post(
|
const r = await requestWithRetries({ url, method: "POST", data: payload }, 4);
|
||||||
`${SELENIUM_AGENT_BASE}/ddma-eligibility`,
|
log("selenium-client", "agent response", {
|
||||||
payload,
|
status: r.status,
|
||||||
{ timeout: 5 * 60 * 1000 }
|
dataKeys: r.data ? Object.keys(r.data) : null,
|
||||||
);
|
});
|
||||||
|
if (r.status >= 500)
|
||||||
if (!result || !result.data) {
|
throw new Error(`Selenium agent server error: ${r.status}`);
|
||||||
throw new Error("Empty response from selenium agent");
|
return r.data;
|
||||||
}
|
|
||||||
|
|
||||||
if (result.data.status === "error") {
|
|
||||||
const errorMsg =
|
|
||||||
typeof result.data.message === "string"
|
|
||||||
? result.data.message
|
|
||||||
: result.data.message?.msg || "Selenium agent error";
|
|
||||||
throw new Error(errorMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.data; // { status: "started", session_id }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function forwardOtpToSeleniumDdmaAgent(
|
export async function forwardOtpToSeleniumDdmaAgent(
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
otp: string
|
otp: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const result = await axios.post(`${SELENIUM_AGENT_BASE}/submit-otp`, {
|
const url = `/submit-otp`;
|
||||||
session_id: sessionId,
|
log("selenium-client", "POST submit-otp", {
|
||||||
otp,
|
url: SELENIUM_AGENT_BASE + url,
|
||||||
|
sessionId,
|
||||||
});
|
});
|
||||||
|
const r = await requestWithRetries(
|
||||||
if (!result || !result.data) throw new Error("Empty OTP response");
|
{ url, method: "POST", data: { session_id: sessionId, otp } },
|
||||||
if (result.data.status === "error") {
|
4
|
||||||
const message =
|
);
|
||||||
typeof result.data.message === "string"
|
log("selenium-client", "submit-otp response", {
|
||||||
? result.data.message
|
status: r.status,
|
||||||
: JSON.stringify(result.data);
|
data: r.data,
|
||||||
throw new Error(message);
|
});
|
||||||
}
|
if (r.status >= 500)
|
||||||
|
throw new Error(`Selenium agent server error on submit-otp: ${r.status}`);
|
||||||
return result.data;
|
return r.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getSeleniumDdmaSessionStatus(
|
export async function getSeleniumDdmaSessionStatus(
|
||||||
sessionId: string
|
sessionId: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const result = await axios.get(
|
const url = `/session/${sessionId}/status`;
|
||||||
`${SELENIUM_AGENT_BASE}/session/${sessionId}/status`
|
log("selenium-client", "GET session status", {
|
||||||
);
|
url: SELENIUM_AGENT_BASE + url,
|
||||||
if (!result || !result.data) throw new Error("Empty session status");
|
sessionId,
|
||||||
return result.data;
|
});
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import { setTaskStatus } from "@/redux/slices/seleniumEligibilityCheckTaskSlice"
|
|||||||
import { formatLocalDate } from "@/utils/dateUtils";
|
import { formatLocalDate } from "@/utils/dateUtils";
|
||||||
import { QK_PATIENTS_BASE } from "@/components/patients/patient-table";
|
import { QK_PATIENTS_BASE } from "@/components/patients/patient-table";
|
||||||
|
|
||||||
// Use Vite env (set VITE_BACKEND_URL in your frontend .env)
|
|
||||||
const SOCKET_URL =
|
const SOCKET_URL =
|
||||||
import.meta.env.VITE_API_BASE_URL_BACKEND ||
|
import.meta.env.VITE_API_BASE_URL_BACKEND ||
|
||||||
(typeof window !== "undefined" ? window.location.origin : "");
|
(typeof window !== "undefined" ? window.location.origin : "");
|
||||||
@@ -24,7 +23,12 @@ interface DdmaOtpModalProps {
|
|||||||
isSubmitting: boolean;
|
isSubmitting: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function DdmaOtpModal({ open, onClose, onSubmit, isSubmitting }: DdmaOtpModalProps) {
|
function DdmaOtpModal({
|
||||||
|
open,
|
||||||
|
onClose,
|
||||||
|
onSubmit,
|
||||||
|
isSubmitting,
|
||||||
|
}: DdmaOtpModalProps) {
|
||||||
const [otp, setOtp] = useState("");
|
const [otp, setOtp] = useState("");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -135,6 +139,17 @@ export function DdmaEligibilityButton({
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const closeSocket = () => {
|
||||||
|
try {
|
||||||
|
socketRef.current?.removeAllListeners();
|
||||||
|
socketRef.current?.disconnect();
|
||||||
|
} catch (e) {
|
||||||
|
// ignore
|
||||||
|
} finally {
|
||||||
|
socketRef.current = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Lazy socket setup: called only when we actually need it (first click)
|
// Lazy socket setup: called only when we actually need it (first click)
|
||||||
const ensureSocketConnected = async () => {
|
const ensureSocketConnected = async () => {
|
||||||
// If already connected, nothing to do
|
// If already connected, nothing to do
|
||||||
@@ -159,13 +174,68 @@ export function DdmaEligibilityButton({
|
|||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("connect_error", (err) => {
|
// connection error when first connecting (or later)
|
||||||
console.error("DDMA socket connect_error:", err);
|
socket.on("connect_error", (err: any) => {
|
||||||
reject(err);
|
dispatch(
|
||||||
|
setTaskStatus({
|
||||||
|
status: "error",
|
||||||
|
message: "Connection failed",
|
||||||
|
})
|
||||||
|
);
|
||||||
|
toast({
|
||||||
|
title: "Realtime connection failed",
|
||||||
|
description:
|
||||||
|
"Could not connect to realtime server. Retrying automatically...",
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
// do not reject here because socket.io will attempt reconnection
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("disconnect", () => {
|
// socket.io will emit 'reconnect_attempt' for retries
|
||||||
console.log("DDMA socket disconnected");
|
socket.on("reconnect_attempt", (attempt: number) => {
|
||||||
|
dispatch(
|
||||||
|
setTaskStatus({
|
||||||
|
status: "pending",
|
||||||
|
message: `Realtime reconnect attempt #${attempt}`,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// when reconnection failed after configured attempts
|
||||||
|
socket.on("reconnect_failed", () => {
|
||||||
|
dispatch(
|
||||||
|
setTaskStatus({
|
||||||
|
status: "error",
|
||||||
|
message: "Reconnect failed",
|
||||||
|
})
|
||||||
|
);
|
||||||
|
toast({
|
||||||
|
title: "Realtime reconnect failed",
|
||||||
|
description:
|
||||||
|
"Connection to realtime server could not be re-established. Please try again later.",
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
// terminal failure — cleanup and reject so caller can stop start flow
|
||||||
|
closeSocket();
|
||||||
|
reject(new Error("Realtime reconnect failed"));
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("disconnect", (reason: any) => {
|
||||||
|
dispatch(
|
||||||
|
setTaskStatus({
|
||||||
|
status: "error",
|
||||||
|
message: "Connection disconnected",
|
||||||
|
})
|
||||||
|
);
|
||||||
|
toast({
|
||||||
|
title: "Connection Disconnected",
|
||||||
|
description:
|
||||||
|
"Connection to the server was lost. If a DDMA job was running it may have failed.",
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
// clear sessionId/OTP modal
|
||||||
|
setSessionId(null);
|
||||||
|
setOtpModalOpen(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
// OTP required
|
// OTP required
|
||||||
@@ -176,15 +246,14 @@ export function DdmaEligibilityButton({
|
|||||||
dispatch(
|
dispatch(
|
||||||
setTaskStatus({
|
setTaskStatus({
|
||||||
status: "pending",
|
status: "pending",
|
||||||
message:
|
message: "OTP required for DDMA eligibility. Please enter the OTP.",
|
||||||
"OTP required for DDMA eligibility. Please enter the OTP.",
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// OTP submitted (optional UX)
|
// OTP submitted (optional UX)
|
||||||
socket.on("selenium:otp_submitted", (payload: any) => {
|
socket.on("selenium:otp_submitted", (payload: any) => {
|
||||||
if (!payload?.session_id || payload.session_id !== sessionId) return;
|
if (!payload?.session_id) return;
|
||||||
dispatch(
|
dispatch(
|
||||||
setTaskStatus({
|
setTaskStatus({
|
||||||
status: "pending",
|
status: "pending",
|
||||||
@@ -196,7 +265,7 @@ export function DdmaEligibilityButton({
|
|||||||
// Session update
|
// Session update
|
||||||
socket.on("selenium:session_update", (payload: any) => {
|
socket.on("selenium:session_update", (payload: any) => {
|
||||||
const { session_id, status, final } = payload || {};
|
const { session_id, status, final } = payload || {};
|
||||||
if (!session_id || session_id !== sessionId) return;
|
if (!session_id) return;
|
||||||
|
|
||||||
if (status === "completed") {
|
if (status === "completed") {
|
||||||
dispatch(
|
dispatch(
|
||||||
@@ -238,20 +307,65 @@ export function DdmaEligibilityButton({
|
|||||||
description: msg,
|
description: msg,
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Ensure socket is torn down for this session (stop receiving stale events)
|
||||||
|
try {
|
||||||
|
closeSocket();
|
||||||
|
} catch (e) {}
|
||||||
setSessionId(null);
|
setSessionId(null);
|
||||||
setOtpModalOpen(false);
|
setOtpModalOpen(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
queryClient.invalidateQueries({ queryKey: QK_PATIENTS_BASE });
|
queryClient.invalidateQueries({ queryKey: QK_PATIENTS_BASE });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// explicit session error event (helpful)
|
||||||
|
socket.on("selenium:session_error", (payload: any) => {
|
||||||
|
const msg = payload?.message || "Selenium session error";
|
||||||
|
|
||||||
|
dispatch(
|
||||||
|
setTaskStatus({
|
||||||
|
status: "error",
|
||||||
|
message: msg,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: "Selenium session error",
|
||||||
|
description: msg,
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
|
||||||
|
// tear down socket to avoid stale updates
|
||||||
|
try {
|
||||||
|
closeSocket();
|
||||||
|
} catch (e) {}
|
||||||
|
setSessionId(null);
|
||||||
|
setOtpModalOpen(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
// If socket.io initial connection fails permanently (very rare: client-level)
|
||||||
|
// set a longer timeout to reject the first attempt to connect.
|
||||||
|
const initialConnectTimeout = setTimeout(() => {
|
||||||
|
if (!socket.connected) {
|
||||||
|
// if still not connected after 8s, treat as failure and reject so caller can handle it
|
||||||
|
closeSocket();
|
||||||
|
reject(new Error("Realtime initial connection timeout"));
|
||||||
|
}
|
||||||
|
}, 8000);
|
||||||
|
|
||||||
|
// When the connect resolves we should clear this timer
|
||||||
|
socket.once("connect", () => {
|
||||||
|
clearTimeout(initialConnectTimeout);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// store promise to prevent multiple concurrent connections
|
||||||
connectingRef.current = promise;
|
connectingRef.current = promise;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await promise;
|
await promise;
|
||||||
} finally {
|
} finally {
|
||||||
// Once resolved or rejected, allow future attempts if needed
|
|
||||||
connectingRef.current = null;
|
connectingRef.current = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ from typing import Dict, Any
|
|||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
from selenium.common.exceptions import WebDriverException
|
||||||
|
import pickle
|
||||||
|
|
||||||
from selenium_DDMA_eligibilityCheckWorker import AutomationDeltaDentalMAEligibilityCheck
|
from selenium_DDMA_eligibilityCheckWorker import AutomationDeltaDentalMAEligibilityCheck
|
||||||
|
|
||||||
@@ -33,23 +35,46 @@ def make_session_entry() -> str:
|
|||||||
return sid
|
return sid
|
||||||
|
|
||||||
|
|
||||||
async def cleanup_session(sid: str):
|
async def cleanup_session(sid: str, message: str | None = None):
|
||||||
"""Close driver (if any) and remove session entry."""
|
"""
|
||||||
|
Close driver (if any), wake OTP waiter, set final state, and remove session entry.
|
||||||
|
Idempotent: safe to call multiple times.
|
||||||
|
"""
|
||||||
s = sessions.get(sid)
|
s = sessions.get(sid)
|
||||||
if not s:
|
if not s:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
|
# Ensure final state
|
||||||
|
try:
|
||||||
|
if s.get("status") not in ("completed", "error", "not_found"):
|
||||||
|
s["status"] = "error"
|
||||||
|
if message:
|
||||||
|
s["message"] = message
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Wake any OTP waiter (so awaiting coroutines don't hang)
|
||||||
|
try:
|
||||||
|
ev = s.get("otp_event")
|
||||||
|
if ev and not ev.is_set():
|
||||||
|
ev.set()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Attempt to quit driver (may already be dead)
|
||||||
driver = s.get("driver")
|
driver = s.get("driver")
|
||||||
if driver:
|
if driver:
|
||||||
try:
|
try:
|
||||||
driver.quit()
|
driver.quit()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
# ignore errors from quit (session already gone)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
# Remove session entry from map
|
||||||
sessions.pop(sid, None)
|
sessions.pop(sid, None)
|
||||||
print(f"[helpers] cleaned session {sid}")
|
print(f"[helpers] cleaned session {sid}")
|
||||||
|
|
||||||
|
|
||||||
async def _remove_session_later(sid: str, delay: int = 20):
|
async def _remove_session_later(sid: str, delay: int = 20):
|
||||||
await asyncio.sleep(delay)
|
await asyncio.sleep(delay)
|
||||||
await cleanup_session(sid)
|
await cleanup_session(sid)
|
||||||
@@ -89,7 +114,18 @@ async def start_ddma_run(sid: str, data: dict, url: str):
|
|||||||
return {"status": "error", "message": s["message"]}
|
return {"status": "error", "message": s["message"]}
|
||||||
|
|
||||||
# Login
|
# Login
|
||||||
login_result = bot.login()
|
try:
|
||||||
|
login_result = bot.login(url)
|
||||||
|
except WebDriverException as wde:
|
||||||
|
s["status"] = "error"
|
||||||
|
s["message"] = f"Selenium driver error during login: {wde}"
|
||||||
|
await cleanup_session(sid, s["message"])
|
||||||
|
return {"status": "error", "message": s["message"]}
|
||||||
|
except Exception as e:
|
||||||
|
s["status"] = "error"
|
||||||
|
s["message"] = f"Unexpected error during login: {e}"
|
||||||
|
await cleanup_session(sid, s["message"])
|
||||||
|
return {"status": "error", "message": s["message"]}
|
||||||
|
|
||||||
# OTP required path
|
# OTP required path
|
||||||
if isinstance(login_result, str) and login_result == "OTP_REQUIRED":
|
if isinstance(login_result, str) and login_result == "OTP_REQUIRED":
|
||||||
@@ -138,6 +174,38 @@ async def start_ddma_run(sid: str, data: dict, url: str):
|
|||||||
s["status"] = "otp_submitted"
|
s["status"] = "otp_submitted"
|
||||||
s["last_activity"] = time.time()
|
s["last_activity"] = time.time()
|
||||||
await asyncio.sleep(0.5)
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
|
# Wait for post-OTP login to complete and then save cookies
|
||||||
|
try:
|
||||||
|
driver = s["driver"]
|
||||||
|
wait = WebDriverWait(driver, 30)
|
||||||
|
# Wait for dashboard element or URL change indicating success
|
||||||
|
logged_in_el = wait.until(
|
||||||
|
EC.presence_of_element_located(
|
||||||
|
(By.XPATH, "//a[text()='Member Eligibility' or contains(., 'Member Eligibility')]")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# If found, save cookies
|
||||||
|
if logged_in_el:
|
||||||
|
try:
|
||||||
|
# Prefer direct save to avoid subtle create_if_missing behavior
|
||||||
|
cookies = driver.get_cookies()
|
||||||
|
pickle.dump(cookies, open(bot.cookies_path, "wb"))
|
||||||
|
print(f"[start_ddma_run] Saved {len(cookies)} cookies after OTP to {bot.cookies_path}")
|
||||||
|
except Exception as e:
|
||||||
|
print("[start_ddma_run] Warning saving cookies after OTP:", e)
|
||||||
|
except Exception as e:
|
||||||
|
# If waiting times out, still attempt a heuristic check by URL
|
||||||
|
cur = s["driver"].current_url if s.get("driver") else ""
|
||||||
|
print("[start_ddma_run] Post-OTP dashboard detection timed out. Current URL:", cur)
|
||||||
|
if "dashboard" in cur or "providers" in cur:
|
||||||
|
try:
|
||||||
|
cookies = s["driver"].get_cookies()
|
||||||
|
pickle.dump(cookies, open(bot.cookies_path, "wb"))
|
||||||
|
print(f"[start_ddma_run] Saved {len(cookies)} cookies after OTP (URL heuristic).")
|
||||||
|
except Exception as e2:
|
||||||
|
print("[start_ddma_run] Warning saving cookies after OTP (heuristic):", e2)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
s["status"] = "error"
|
s["status"] = "error"
|
||||||
s["message"] = f"Failed to submit OTP into page: {e}"
|
s["message"] = f"Failed to submit OTP into page: {e}"
|
||||||
|
|||||||
Reference in New Issue
Block a user