feat(eligibility-check) - add CCA eligibility workflow with new routes and frontend components; enhance patient data processing and eligibility status updates; update insurance provider handling across various workflows
This commit is contained in:
@@ -8,22 +8,19 @@ import { z } from "zod";
|
||||
type SelectUser = z.infer<typeof UserUncheckedCreateInputObjectSchema>;
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET || "your-jwt-secret";
|
||||
const JWT_EXPIRATION = "24h"; // JWT expiration time (1 day)
|
||||
const JWT_EXPIRATION = "24h";
|
||||
|
||||
// Function to hash password using bcrypt
|
||||
async function hashPassword(password: string) {
|
||||
const saltRounds = 10; // Salt rounds for bcrypt
|
||||
const saltRounds = 10;
|
||||
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
||||
return hashedPassword;
|
||||
}
|
||||
|
||||
// Function to compare passwords using bcrypt
|
||||
async function comparePasswords(supplied: string, stored: string) {
|
||||
const isMatch = await bcrypt.compare(supplied, stored);
|
||||
return isMatch;
|
||||
}
|
||||
|
||||
// Function to generate JWT
|
||||
function generateToken(user: SelectUser) {
|
||||
return jwt.sign({ id: user.id, username: user.username }, JWT_SECRET, {
|
||||
expiresIn: JWT_EXPIRATION,
|
||||
@@ -32,35 +29,13 @@ function generateToken(user: SelectUser) {
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// User registration route
|
||||
router.post(
|
||||
"/register",
|
||||
async (req: Request, res: Response, next: NextFunction): Promise<any> => {
|
||||
try {
|
||||
const existingUser = await storage.getUserByUsername(req.body.username);
|
||||
if (existingUser) {
|
||||
return res.status(400).send("Username already exists");
|
||||
}
|
||||
|
||||
const hashedPassword = await hashPassword(req.body.password);
|
||||
const user = await storage.createUser({
|
||||
...req.body,
|
||||
password: hashedPassword,
|
||||
});
|
||||
|
||||
// Generate a JWT token for the user after successful registration
|
||||
const token = generateToken(user);
|
||||
|
||||
const { password, ...safeUser } = user;
|
||||
return res.status(201).json({ user: safeUser, token });
|
||||
} catch (error) {
|
||||
console.error("Registration error:", error);
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
return res.status(403).json({ error: "Public registration is disabled. Please contact your administrator." });
|
||||
}
|
||||
);
|
||||
|
||||
// User login route
|
||||
router.post(
|
||||
"/login",
|
||||
async (req: Request, res: Response, next: NextFunction): Promise<any> => {
|
||||
@@ -77,12 +52,12 @@ router.post(
|
||||
);
|
||||
|
||||
if (!isPasswordMatch) {
|
||||
return res.status(401).json({ error: "Invalid password or password" });
|
||||
return res.status(401).json({ error: "Invalid username or password" });
|
||||
}
|
||||
|
||||
// Generate a JWT token for the user after successful login
|
||||
const token = generateToken(user);
|
||||
const { password, ...safeUser } = user;
|
||||
const { password, ...rest } = user;
|
||||
const safeUser = { ...rest, role: rest.role ?? "USER" };
|
||||
return res.status(200).json({ user: safeUser, token });
|
||||
} catch (error) {
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
@@ -90,9 +65,7 @@ router.post(
|
||||
}
|
||||
);
|
||||
|
||||
// Logout route (client-side action to remove the token)
|
||||
router.post("/logout", (req: Request, res: Response) => {
|
||||
// For JWT-based auth, logout is handled on the client (by removing token)
|
||||
res.status(200).send("Logged out successfully");
|
||||
});
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import insuranceStatusDdmaRoutes from "./insuranceStatusDDMA";
|
||||
import insuranceStatusDentaQuestRoutes from "./insuranceStatusDentaQuest";
|
||||
import insuranceStatusUnitedSCORoutes from "./insuranceStatusUnitedSCO";
|
||||
import insuranceStatusDeltaInsRoutes from "./insuranceStatusDeltaIns";
|
||||
import insuranceStatusCCARoutes from "./insuranceStatusCCA";
|
||||
import paymentsRoutes from "./payments";
|
||||
import databaseManagementRoutes from "./database-management";
|
||||
import notificationsRoutes from "./notifications";
|
||||
@@ -35,6 +36,7 @@ router.use("/insurance-status-ddma", insuranceStatusDdmaRoutes);
|
||||
router.use("/insurance-status-dentaquest", insuranceStatusDentaQuestRoutes);
|
||||
router.use("/insurance-status-unitedsco", insuranceStatusUnitedSCORoutes);
|
||||
router.use("/insurance-status-deltains", insuranceStatusDeltaInsRoutes);
|
||||
router.use("/insurance-status-cca", insuranceStatusCCARoutes);
|
||||
router.use("/payments", paymentsRoutes);
|
||||
router.use("/database-management", databaseManagementRoutes);
|
||||
router.use("/notifications", notificationsRoutes);
|
||||
|
||||
@@ -69,15 +69,15 @@ async function createOrUpdatePatientByInsuranceId(options: {
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
// inside createOrUpdatePatientByInsuranceId, when creating:
|
||||
const createPayload: any = {
|
||||
firstName: incomingFirst,
|
||||
lastName: incomingLast,
|
||||
dateOfBirth: dob, // raw from caller (string | Date | null)
|
||||
dateOfBirth: dob,
|
||||
gender: "",
|
||||
phone: "",
|
||||
userId,
|
||||
insuranceId,
|
||||
insuranceProvider: "MassHealth",
|
||||
};
|
||||
|
||||
let patientData: InsertPatient;
|
||||
@@ -219,8 +219,8 @@ router.post(
|
||||
if (patient && patient.id !== undefined) {
|
||||
const newStatus =
|
||||
seleniumResult.eligibility === "Y" ? "ACTIVE" : "INACTIVE";
|
||||
await storage.updatePatient(patient.id, { status: newStatus });
|
||||
outputResult.patientUpdateStatus = `Patient status updated to ${newStatus}`;
|
||||
await storage.updatePatient(patient.id, { status: newStatus, insuranceProvider: "MassHealth" });
|
||||
outputResult.patientUpdateStatus = `Patient status updated to ${newStatus}, insuranceProvider=MassHealth`;
|
||||
|
||||
// ✅ Step 5: Handle PDF Upload
|
||||
if (
|
||||
@@ -649,8 +649,8 @@ router.post(
|
||||
seleniumResult?.eligibility === "Y" ? "ACTIVE" : "INACTIVE";
|
||||
|
||||
// 1. updating patient
|
||||
await storage.updatePatient(updatedPatient.id, { status: newStatus });
|
||||
resultItem.patientUpdateStatus = `Patient status updated to ${newStatus}`;
|
||||
await storage.updatePatient(updatedPatient.id, { status: newStatus, insuranceProvider: "MassHealth" });
|
||||
resultItem.patientUpdateStatus = `Patient status updated to ${newStatus}, insuranceProvider=MassHealth`;
|
||||
|
||||
// 2. updating appointment status - for aptmnt page
|
||||
try {
|
||||
|
||||
752
apps/Backend/src/routes/insuranceStatusCCA.ts
Normal file
752
apps/Backend/src/routes/insuranceStatusCCA.ts
Normal file
@@ -0,0 +1,752 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { storage } from "../storage";
|
||||
import {
|
||||
forwardToSeleniumCCAEligibilityAgent,
|
||||
getSeleniumCCASessionStatus,
|
||||
} from "../services/seleniumCCAInsuranceEligibilityClient";
|
||||
import fs from "fs/promises";
|
||||
import fsSync from "fs";
|
||||
import path from "path";
|
||||
import PDFDocument from "pdfkit";
|
||||
import { emptyFolderContainingFile } from "../utils/emptyTempFolder";
|
||||
import {
|
||||
InsertPatient,
|
||||
insertPatientSchema,
|
||||
} from "../../../../packages/db/types/patient-types";
|
||||
import { io } from "../socket";
|
||||
|
||||
const router = Router();
|
||||
|
||||
interface CCAJobContext {
|
||||
userId: number;
|
||||
insuranceEligibilityData: any;
|
||||
socketId?: string;
|
||||
}
|
||||
|
||||
const ccaJobs: Record<string, CCAJobContext> = {};
|
||||
|
||||
function splitName(fullName?: string | null) {
|
||||
if (!fullName) return { firstName: "", lastName: "" };
|
||||
const parts = fullName.trim().split(/\s+/).filter(Boolean);
|
||||
const firstName = parts.shift() ?? "";
|
||||
const lastName = parts.join(" ") ?? "";
|
||||
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;
|
||||
const A4_HEIGHT = 841.89;
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function createOrUpdatePatientByInsuranceId(options: {
|
||||
insuranceId: string;
|
||||
firstName?: string | null;
|
||||
lastName?: string | null;
|
||||
dob?: string | Date | null;
|
||||
userId: number;
|
||||
eligibilityStatus?: string;
|
||||
}) {
|
||||
const { insuranceId, firstName, lastName, dob, userId, eligibilityStatus } =
|
||||
options;
|
||||
if (!insuranceId) throw new Error("Missing insuranceId");
|
||||
|
||||
const incomingFirst = (firstName || "").trim();
|
||||
const incomingLast = (lastName || "").trim();
|
||||
|
||||
let patient = await storage.getPatientByInsuranceId(insuranceId);
|
||||
|
||||
if (patient && patient.id) {
|
||||
const updates: any = {};
|
||||
if (
|
||||
incomingFirst &&
|
||||
String(patient.firstName ?? "").trim() !== incomingFirst
|
||||
) {
|
||||
updates.firstName = incomingFirst;
|
||||
}
|
||||
if (
|
||||
incomingLast &&
|
||||
String(patient.lastName ?? "").trim() !== incomingLast
|
||||
) {
|
||||
updates.lastName = incomingLast;
|
||||
}
|
||||
if (Object.keys(updates).length > 0) {
|
||||
await storage.updatePatient(patient.id, updates);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
console.log(
|
||||
`[cca-eligibility] Creating new patient: ${incomingFirst} ${incomingLast} with status: ${eligibilityStatus || "UNKNOWN"}`
|
||||
);
|
||||
const createPayload: any = {
|
||||
firstName: incomingFirst,
|
||||
lastName: incomingLast,
|
||||
dateOfBirth: dob,
|
||||
gender: "Unknown",
|
||||
phone: "",
|
||||
userId,
|
||||
insuranceId,
|
||||
insuranceProvider: "CCA",
|
||||
status: eligibilityStatus || "UNKNOWN",
|
||||
};
|
||||
let patientData: InsertPatient;
|
||||
try {
|
||||
patientData = insertPatientSchema.parse(createPayload);
|
||||
} catch (err) {
|
||||
const safePayload = { ...createPayload };
|
||||
delete (safePayload as any).dateOfBirth;
|
||||
patientData = insertPatientSchema.parse(safePayload);
|
||||
}
|
||||
const newPatient = await storage.createPatient(patientData);
|
||||
console.log(
|
||||
`[cca-eligibility] Created new patient: ${newPatient.id} with status: ${eligibilityStatus || "UNKNOWN"}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCCACompletedJob(
|
||||
sessionId: string,
|
||||
job: CCAJobContext,
|
||||
seleniumResult: any
|
||||
) {
|
||||
let createdPdfFileId: number | null = null;
|
||||
let generatedPdfPath: string | null = null;
|
||||
const outputResult: any = {};
|
||||
|
||||
try {
|
||||
const insuranceEligibilityData = job.insuranceEligibilityData;
|
||||
|
||||
let insuranceId = String(seleniumResult?.memberId ?? "").trim();
|
||||
if (!insuranceId) {
|
||||
insuranceId = String(insuranceEligibilityData.memberId ?? "").trim();
|
||||
}
|
||||
|
||||
if (!insuranceId) {
|
||||
console.log(
|
||||
"[cca-eligibility] No Member ID found - will use name for patient lookup"
|
||||
);
|
||||
} else {
|
||||
console.log(`[cca-eligibility] Using Member ID: ${insuranceId}`);
|
||||
}
|
||||
|
||||
const patientNameFromResult =
|
||||
typeof seleniumResult?.patientName === "string"
|
||||
? seleniumResult.patientName.trim()
|
||||
: null;
|
||||
|
||||
let firstName = insuranceEligibilityData.firstName || "";
|
||||
let lastName = insuranceEligibilityData.lastName || "";
|
||||
|
||||
if (patientNameFromResult) {
|
||||
const parsedName = splitName(patientNameFromResult);
|
||||
firstName = parsedName.firstName || firstName;
|
||||
lastName = parsedName.lastName || lastName;
|
||||
}
|
||||
|
||||
const rawEligibility = String(
|
||||
seleniumResult?.eligibility ?? ""
|
||||
).toLowerCase();
|
||||
const eligibilityStatus =
|
||||
rawEligibility.includes("active") || rawEligibility.includes("eligible")
|
||||
? "ACTIVE"
|
||||
: "INACTIVE";
|
||||
console.log(`[cca-eligibility] Eligibility status: ${eligibilityStatus}`);
|
||||
|
||||
// Extract extra patient data from selenium result
|
||||
const extractedAddress = String(seleniumResult?.address ?? "").trim();
|
||||
const extractedCity = String(seleniumResult?.city ?? "").trim();
|
||||
const extractedZip = String(seleniumResult?.zipCode ?? "").trim();
|
||||
const extractedInsurer = String(seleniumResult?.insurerName ?? "").trim() || "CCA";
|
||||
|
||||
if (extractedAddress || extractedCity || extractedZip) {
|
||||
console.log(`[cca-eligibility] Extra data: address=${extractedAddress}, city=${extractedCity}, zip=${extractedZip}, insurer=${extractedInsurer}`);
|
||||
}
|
||||
|
||||
if (insuranceId) {
|
||||
await createOrUpdatePatientByInsuranceId({
|
||||
insuranceId,
|
||||
firstName,
|
||||
lastName,
|
||||
dob: insuranceEligibilityData.dateOfBirth,
|
||||
userId: job.userId,
|
||||
eligibilityStatus,
|
||||
});
|
||||
}
|
||||
|
||||
let patient = insuranceId
|
||||
? await storage.getPatientByInsuranceId(insuranceId)
|
||||
: null;
|
||||
|
||||
if (!patient?.id && firstName && lastName) {
|
||||
const patients = await storage.getAllPatients(job.userId);
|
||||
patient =
|
||||
patients.find(
|
||||
(p) =>
|
||||
p.firstName?.toLowerCase() === firstName.toLowerCase() &&
|
||||
p.lastName?.toLowerCase() === lastName.toLowerCase()
|
||||
) ?? null;
|
||||
if (patient) {
|
||||
console.log(
|
||||
`[cca-eligibility] Found patient by name: ${patient.id}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!patient && firstName && lastName) {
|
||||
console.log(
|
||||
`[cca-eligibility] Creating new patient: ${firstName} ${lastName}`
|
||||
);
|
||||
try {
|
||||
let parsedDob: Date | undefined = undefined;
|
||||
if (insuranceEligibilityData.dateOfBirth) {
|
||||
try {
|
||||
parsedDob = new Date(insuranceEligibilityData.dateOfBirth);
|
||||
if (isNaN(parsedDob.getTime())) parsedDob = undefined;
|
||||
} catch {
|
||||
parsedDob = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
const newPatientData: InsertPatient = {
|
||||
firstName,
|
||||
lastName,
|
||||
dateOfBirth: parsedDob || new Date(),
|
||||
insuranceId: insuranceId || undefined,
|
||||
insuranceProvider: extractedInsurer,
|
||||
gender: "Unknown",
|
||||
phone: "",
|
||||
userId: job.userId,
|
||||
status: eligibilityStatus,
|
||||
address: extractedAddress || undefined,
|
||||
city: extractedCity || undefined,
|
||||
zipCode: extractedZip || undefined,
|
||||
};
|
||||
|
||||
const validation = insertPatientSchema.safeParse(newPatientData);
|
||||
if (validation.success) {
|
||||
patient = await storage.createPatient(validation.data);
|
||||
console.log(
|
||||
`[cca-eligibility] Created new patient: ${patient.id}`
|
||||
);
|
||||
} else {
|
||||
console.log(
|
||||
`[cca-eligibility] Patient validation failed: ${validation.error.message}`
|
||||
);
|
||||
}
|
||||
} catch (createErr: any) {
|
||||
console.log(
|
||||
`[cca-eligibility] Failed to create patient: ${createErr.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!patient?.id) {
|
||||
outputResult.patientUpdateStatus =
|
||||
"Patient not found and could not be created; no update performed";
|
||||
return {
|
||||
patientUpdateStatus: outputResult.patientUpdateStatus,
|
||||
pdfUploadStatus: "none",
|
||||
pdfFileId: null,
|
||||
};
|
||||
}
|
||||
|
||||
const updatePayload: Record<string, any> = {
|
||||
status: eligibilityStatus,
|
||||
insuranceProvider: extractedInsurer,
|
||||
};
|
||||
if (firstName && (!patient.firstName || patient.firstName.trim() === "")) {
|
||||
updatePayload.firstName = firstName;
|
||||
}
|
||||
if (lastName && (!patient.lastName || patient.lastName.trim() === "")) {
|
||||
updatePayload.lastName = lastName;
|
||||
}
|
||||
if (extractedAddress && (!patient.address || patient.address.trim() === "")) {
|
||||
updatePayload.address = extractedAddress;
|
||||
}
|
||||
if (extractedCity && (!patient.city || patient.city.trim() === "")) {
|
||||
updatePayload.city = extractedCity;
|
||||
}
|
||||
if (extractedZip && (!patient.zipCode || patient.zipCode.trim() === "")) {
|
||||
updatePayload.zipCode = extractedZip;
|
||||
}
|
||||
|
||||
await storage.updatePatient(patient.id, updatePayload);
|
||||
outputResult.patientUpdateStatus = `Patient ${patient.id} updated: status=${eligibilityStatus}, insuranceProvider=${extractedInsurer}, name=${firstName} ${lastName}, address=${extractedAddress}, city=${extractedCity}, zip=${extractedZip}`;
|
||||
console.log(`[cca-eligibility] ${outputResult.patientUpdateStatus}`);
|
||||
|
||||
// Handle PDF
|
||||
let pdfBuffer: Buffer | null = null;
|
||||
|
||||
if (
|
||||
seleniumResult?.pdfBase64 &&
|
||||
typeof seleniumResult.pdfBase64 === "string" &&
|
||||
seleniumResult.pdfBase64.length > 100
|
||||
) {
|
||||
try {
|
||||
pdfBuffer = Buffer.from(seleniumResult.pdfBase64, "base64");
|
||||
const pdfFileName = `cca_eligibility_${insuranceId || "unknown"}_${Date.now()}.pdf`;
|
||||
const downloadDir = path.join(process.cwd(), "seleniumDownloads");
|
||||
if (!fsSync.existsSync(downloadDir)) {
|
||||
fsSync.mkdirSync(downloadDir, { recursive: true });
|
||||
}
|
||||
generatedPdfPath = path.join(downloadDir, pdfFileName);
|
||||
await fs.writeFile(generatedPdfPath, pdfBuffer);
|
||||
console.log(
|
||||
`[cca-eligibility] PDF saved from base64: ${generatedPdfPath}`
|
||||
);
|
||||
} catch (pdfErr: any) {
|
||||
console.error(
|
||||
`[cca-eligibility] Failed to save base64 PDF: ${pdfErr.message}`
|
||||
);
|
||||
pdfBuffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
!pdfBuffer &&
|
||||
seleniumResult?.ss_path &&
|
||||
typeof seleniumResult.ss_path === "string"
|
||||
) {
|
||||
try {
|
||||
if (!fsSync.existsSync(seleniumResult.ss_path)) {
|
||||
throw new Error(`File not found: ${seleniumResult.ss_path}`);
|
||||
}
|
||||
|
||||
if (seleniumResult.ss_path.endsWith(".pdf")) {
|
||||
pdfBuffer = await fs.readFile(seleniumResult.ss_path);
|
||||
generatedPdfPath = seleniumResult.ss_path;
|
||||
seleniumResult.pdf_path = generatedPdfPath;
|
||||
} else if (
|
||||
seleniumResult.ss_path.endsWith(".png") ||
|
||||
seleniumResult.ss_path.endsWith(".jpg") ||
|
||||
seleniumResult.ss_path.endsWith(".jpeg")
|
||||
) {
|
||||
pdfBuffer = await imageToPdfBuffer(seleniumResult.ss_path);
|
||||
const pdfFileName = `cca_eligibility_${insuranceId || "unknown"}_${Date.now()}.pdf`;
|
||||
generatedPdfPath = path.join(
|
||||
path.dirname(seleniumResult.ss_path),
|
||||
pdfFileName
|
||||
);
|
||||
await fs.writeFile(generatedPdfPath, pdfBuffer);
|
||||
seleniumResult.pdf_path = generatedPdfPath;
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error(
|
||||
"[cca-eligibility] Failed to process PDF/screenshot:",
|
||||
err
|
||||
);
|
||||
outputResult.pdfUploadStatus = `Failed to process file: ${String(err)}`;
|
||||
}
|
||||
}
|
||||
|
||||
if (pdfBuffer && generatedPdfPath) {
|
||||
const groupTitle = "Eligibility Status";
|
||||
const groupTitleKey = "ELIGIBILITY_STATUS";
|
||||
|
||||
let group = await storage.findPdfGroupByPatientTitleKey(
|
||||
patient.id,
|
||||
groupTitleKey
|
||||
);
|
||||
if (!group) {
|
||||
group = await storage.createPdfGroup(
|
||||
patient.id,
|
||||
groupTitle,
|
||||
groupTitleKey
|
||||
);
|
||||
}
|
||||
if (!group?.id) {
|
||||
throw new Error("PDF group creation failed: missing group ID");
|
||||
}
|
||||
|
||||
const created = await storage.createPdfFile(
|
||||
group.id,
|
||||
path.basename(generatedPdfPath),
|
||||
pdfBuffer
|
||||
);
|
||||
if (created && typeof created === "object" && "id" in created) {
|
||||
createdPdfFileId = Number(created.id);
|
||||
}
|
||||
outputResult.pdfUploadStatus = `PDF saved to group: ${group.title}`;
|
||||
} else if (!outputResult.pdfUploadStatus) {
|
||||
outputResult.pdfUploadStatus = "No PDF available from Selenium";
|
||||
}
|
||||
|
||||
const pdfFilename = generatedPdfPath
|
||||
? path.basename(generatedPdfPath)
|
||||
: null;
|
||||
|
||||
return {
|
||||
patientUpdateStatus: outputResult.patientUpdateStatus,
|
||||
pdfUploadStatus: outputResult.pdfUploadStatus,
|
||||
pdfFileId: createdPdfFileId,
|
||||
pdfFilename,
|
||||
};
|
||||
} catch (err: any) {
|
||||
const pdfFilename = generatedPdfPath
|
||||
? path.basename(generatedPdfPath)
|
||||
: null;
|
||||
return {
|
||||
patientUpdateStatus: outputResult.patientUpdateStatus,
|
||||
pdfUploadStatus:
|
||||
outputResult.pdfUploadStatus ??
|
||||
`Failed to process CCA job: ${err?.message ?? String(err)}`,
|
||||
pdfFileId: createdPdfFileId,
|
||||
pdfFilename,
|
||||
error: err?.message ?? String(err),
|
||||
};
|
||||
} finally {
|
||||
try {
|
||||
if (seleniumResult && seleniumResult.pdf_path) {
|
||||
await emptyFolderContainingFile(seleniumResult.pdf_path);
|
||||
} else if (seleniumResult && seleniumResult.ss_path) {
|
||||
await emptyFolderContainingFile(seleniumResult.ss_path);
|
||||
}
|
||||
} catch (cleanupErr) {
|
||||
console.error(`[cca-eligibility cleanup failed]`, cleanupErr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let currentFinalSessionId: string | null = null;
|
||||
let currentFinalResult: any = null;
|
||||
|
||||
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 {
|
||||
const socket = io?.sockets.sockets.get(socketId);
|
||||
if (!socket) {
|
||||
log("socket", "socket not found (maybe disconnected)", {
|
||||
socketId,
|
||||
event,
|
||||
});
|
||||
return;
|
||||
}
|
||||
socket.emit(event, payload);
|
||||
log("socket", "emitted", { socketId, event });
|
||||
} catch (err: any) {
|
||||
log("socket", "emit failed", { socketId, event, err: err?.message });
|
||||
}
|
||||
}
|
||||
|
||||
async function pollAgentSessionAndProcess(
|
||||
sessionId: string,
|
||||
socketId?: string,
|
||||
pollTimeoutMs = 4 * 60 * 1000
|
||||
) {
|
||||
const maxAttempts = 300;
|
||||
const baseDelayMs = 1000;
|
||||
const maxTransientErrors = 12;
|
||||
const noProgressLimit = 200;
|
||||
|
||||
const job = ccaJobs[sessionId];
|
||||
let transientErrorCount = 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) {
|
||||
emitSafe(socketId, "selenium:session_update", {
|
||||
session_id: sessionId,
|
||||
status: "error",
|
||||
message: `Polling timeout reached (${Math.round(pollTimeoutMs / 1000)}s).`,
|
||||
});
|
||||
delete ccaJobs[sessionId];
|
||||
return;
|
||||
}
|
||||
|
||||
log(
|
||||
"poller-cca",
|
||||
`attempt=${attempt} session=${sessionId} transientErrCount=${transientErrorCount}`
|
||||
);
|
||||
|
||||
try {
|
||||
const st = await getSeleniumCCASessionStatus(sessionId);
|
||||
const status = st?.status ?? null;
|
||||
log("poller-cca", "got status", {
|
||||
sessionId,
|
||||
status,
|
||||
message: st?.message,
|
||||
resultKeys: st?.result ? Object.keys(st.result) : null,
|
||||
});
|
||||
|
||||
transientErrorCount = 0;
|
||||
|
||||
const isTerminalLike =
|
||||
status === "completed" || status === "error" || status === "not_found";
|
||||
if (status === lastStatus && !isTerminalLike) {
|
||||
consecutiveNoProgress++;
|
||||
} else {
|
||||
consecutiveNoProgress = 0;
|
||||
}
|
||||
lastStatus = status;
|
||||
|
||||
if (consecutiveNoProgress >= noProgressLimit) {
|
||||
emitSafe(socketId, "selenium:session_update", {
|
||||
session_id: sessionId,
|
||||
status: "error",
|
||||
message: `No progress from selenium agent (status="${status}") after ${consecutiveNoProgress} polls; aborting.`,
|
||||
});
|
||||
emitSafe(socketId, "selenium:session_error", {
|
||||
session_id: sessionId,
|
||||
status: "error",
|
||||
message: "No progress from selenium agent",
|
||||
});
|
||||
delete ccaJobs[sessionId];
|
||||
return;
|
||||
}
|
||||
|
||||
emitSafe(socketId, "selenium:debug", {
|
||||
session_id: sessionId,
|
||||
attempt,
|
||||
status,
|
||||
serverTime: new Date().toISOString(),
|
||||
});
|
||||
|
||||
if (status === "completed") {
|
||||
log("poller-cca", "agent completed; processing result", {
|
||||
sessionId,
|
||||
resultKeys: st.result ? Object.keys(st.result) : null,
|
||||
});
|
||||
|
||||
currentFinalSessionId = sessionId;
|
||||
currentFinalResult = {
|
||||
rawSelenium: st.result,
|
||||
processedAt: null,
|
||||
final: null,
|
||||
};
|
||||
|
||||
let finalResult: any = null;
|
||||
if (job && st.result) {
|
||||
try {
|
||||
finalResult = await handleCCACompletedJob(
|
||||
sessionId,
|
||||
job,
|
||||
st.result
|
||||
);
|
||||
currentFinalResult.final = finalResult;
|
||||
currentFinalResult.processedAt = Date.now();
|
||||
} catch (err: any) {
|
||||
currentFinalResult.final = {
|
||||
error: "processing_failed",
|
||||
detail: err?.message ?? String(err),
|
||||
};
|
||||
currentFinalResult.processedAt = Date.now();
|
||||
log("poller-cca", "handleCCACompletedJob failed", {
|
||||
sessionId,
|
||||
err: err?.message ?? err,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
currentFinalResult.final = {
|
||||
error: "no_job_or_no_result",
|
||||
};
|
||||
currentFinalResult.processedAt = Date.now();
|
||||
}
|
||||
|
||||
emitSafe(socketId, "selenium:session_update", {
|
||||
session_id: sessionId,
|
||||
status: "completed",
|
||||
rawSelenium: st.result,
|
||||
final: currentFinalResult.final,
|
||||
});
|
||||
|
||||
delete ccaJobs[sessionId];
|
||||
return;
|
||||
}
|
||||
|
||||
if (status === "error" || status === "not_found") {
|
||||
const emitPayload = {
|
||||
session_id: sessionId,
|
||||
status,
|
||||
message: st?.message || "Selenium session error",
|
||||
};
|
||||
emitSafe(socketId, "selenium:session_update", emitPayload);
|
||||
emitSafe(socketId, "selenium:session_error", emitPayload);
|
||||
delete ccaJobs[sessionId];
|
||||
return;
|
||||
}
|
||||
} catch (err: any) {
|
||||
const axiosStatus =
|
||||
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 (
|
||||
axiosStatus === 404 ||
|
||||
(typeof errMsg === "string" && errMsg.includes("not_found"))
|
||||
) {
|
||||
console.warn(
|
||||
`${new Date().toISOString()} [poller-cca] terminal 404/not_found for ${sessionId}`
|
||||
);
|
||||
|
||||
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);
|
||||
|
||||
delete ccaJobs[sessionId];
|
||||
return;
|
||||
}
|
||||
|
||||
transientErrorCount++;
|
||||
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 ccaJobs[sessionId];
|
||||
return;
|
||||
}
|
||||
|
||||
const backoffMs = Math.min(
|
||||
30_000,
|
||||
baseDelayMs * Math.pow(2, transientErrorCount - 1)
|
||||
);
|
||||
console.warn(
|
||||
`${new Date().toISOString()} [poller-cca] transient error (#${transientErrorCount}) for ${sessionId}: code=${errCode} status=${axiosStatus} msg=${errMsg}`
|
||||
);
|
||||
|
||||
await new Promise((r) => setTimeout(r, backoffMs));
|
||||
continue;
|
||||
}
|
||||
|
||||
await new Promise((r) => setTimeout(r, baseDelayMs));
|
||||
}
|
||||
|
||||
emitSafe(socketId, "selenium:session_update", {
|
||||
session_id: sessionId,
|
||||
status: "error",
|
||||
message: "Polling timeout while waiting for selenium session",
|
||||
});
|
||||
delete ccaJobs[sessionId];
|
||||
}
|
||||
|
||||
router.post(
|
||||
"/cca-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 || !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,
|
||||
"CCA"
|
||||
);
|
||||
if (!credentials) {
|
||||
return res.status(404).json({
|
||||
error:
|
||||
"No insurance credentials found for this provider, Kindly Update this at Settings Page.",
|
||||
});
|
||||
}
|
||||
|
||||
const enrichedData = {
|
||||
...rawData,
|
||||
cca_username: credentials.username,
|
||||
cca_password: credentials.password,
|
||||
};
|
||||
|
||||
const socketId: string | undefined = req.body.socketId;
|
||||
|
||||
const agentResp =
|
||||
await forwardToSeleniumCCAEligibilityAgent(enrichedData);
|
||||
|
||||
if (
|
||||
!agentResp ||
|
||||
agentResp.status !== "started" ||
|
||||
!agentResp.session_id
|
||||
) {
|
||||
return res.status(502).json({
|
||||
error: "Selenium agent did not return a started session",
|
||||
detail: agentResp,
|
||||
});
|
||||
}
|
||||
|
||||
const sessionId = agentResp.session_id as string;
|
||||
|
||||
ccaJobs[sessionId] = {
|
||||
userId: req.user.id,
|
||||
insuranceEligibilityData: enrichedData,
|
||||
socketId,
|
||||
};
|
||||
|
||||
pollAgentSessionAndProcess(sessionId, socketId).catch((e) =>
|
||||
console.warn("pollAgentSessionAndProcess (cca) failed", e)
|
||||
);
|
||||
|
||||
return res.json({ status: "started", session_id: sessionId });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return res.status(500).json({
|
||||
error: err.message || "Failed to start CCA selenium agent",
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
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" });
|
||||
|
||||
if (currentFinalSessionId !== sid || !currentFinalResult) {
|
||||
return res.status(404).json({ error: "final result not found" });
|
||||
}
|
||||
|
||||
return res.json(currentFinalResult);
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
@@ -109,6 +109,7 @@ async function createOrUpdatePatientByInsuranceId(options: {
|
||||
phone: "",
|
||||
userId,
|
||||
insuranceId,
|
||||
insuranceProvider: "Delta MA",
|
||||
};
|
||||
let patientData: InsertPatient;
|
||||
try {
|
||||
@@ -273,8 +274,8 @@ async function handleDdmaCompletedJob(
|
||||
}
|
||||
|
||||
// Update patient status from Delta MA eligibility result
|
||||
await storage.updatePatient(patient.id, { status: eligibilityStatus });
|
||||
outputResult.patientUpdateStatus = `Patient ${patient.id} status set to ${eligibilityStatus} (Delta MA eligibility: ${seleniumResult.eligibility})`;
|
||||
await storage.updatePatient(patient.id, { status: eligibilityStatus, insuranceProvider: "Delta MA" });
|
||||
outputResult.patientUpdateStatus = `Patient ${patient.id} status set to ${eligibilityStatus}, insuranceProvider=Delta MA (Delta MA eligibility: ${seleniumResult.eligibility})`;
|
||||
console.log(`[ddma-eligibility] ${outputResult.patientUpdateStatus}`);
|
||||
|
||||
// Handle PDF or convert screenshot -> pdf if available
|
||||
|
||||
@@ -246,7 +246,7 @@ async function handleDeltaInsCompletedJob(
|
||||
};
|
||||
}
|
||||
|
||||
const updatePayload: Record<string, any> = { status: eligibilityStatus };
|
||||
const updatePayload: Record<string, any> = { status: eligibilityStatus, insuranceProvider: "Delta Dental Ins" };
|
||||
if (firstName && (!patient.firstName || patient.firstName.trim() === "")) {
|
||||
updatePayload.firstName = firstName;
|
||||
}
|
||||
@@ -255,7 +255,7 @@ async function handleDeltaInsCompletedJob(
|
||||
}
|
||||
|
||||
await storage.updatePatient(patient.id, updatePayload);
|
||||
outputResult.patientUpdateStatus = `Patient ${patient.id} updated: status=${eligibilityStatus}, name=${firstName} ${lastName}`;
|
||||
outputResult.patientUpdateStatus = `Patient ${patient.id} updated: status=${eligibilityStatus}, insuranceProvider=Delta Dental Ins, name=${firstName} ${lastName}`;
|
||||
console.log(`[deltains-eligibility] ${outputResult.patientUpdateStatus}`);
|
||||
|
||||
// Handle PDF
|
||||
|
||||
@@ -109,6 +109,7 @@ async function createOrUpdatePatientByInsuranceId(options: {
|
||||
phone: "",
|
||||
userId,
|
||||
insuranceId,
|
||||
insuranceProvider: "Tufts / DentaQuest",
|
||||
};
|
||||
let patientData: InsertPatient;
|
||||
try {
|
||||
@@ -216,7 +217,7 @@ async function handleDentaQuestCompletedJob(
|
||||
phone: "",
|
||||
userId: job.userId,
|
||||
insuranceId: insuranceId || null,
|
||||
insuranceProvider: "DentaQuest", // Set insurance provider
|
||||
insuranceProvider: "Tufts / DentaQuest",
|
||||
status: eligibilityStatus, // Set status from eligibility check
|
||||
};
|
||||
|
||||
@@ -255,8 +256,8 @@ async function handleDentaQuestCompletedJob(
|
||||
}
|
||||
|
||||
// Update patient status from DentaQuest eligibility result
|
||||
await storage.updatePatient(patient.id, { status: eligibilityStatus });
|
||||
outputResult.patientUpdateStatus = `Patient ${patient.id} status set to ${eligibilityStatus} (DentaQuest eligibility: ${seleniumResult.eligibility})`;
|
||||
await storage.updatePatient(patient.id, { status: eligibilityStatus, insuranceProvider: "Tufts / DentaQuest" });
|
||||
outputResult.patientUpdateStatus = `Patient ${patient.id} status set to ${eligibilityStatus}, insuranceProvider=Tufts / DentaQuest (DentaQuest eligibility: ${seleniumResult.eligibility})`;
|
||||
console.log(`[dentaquest-eligibility] ${outputResult.patientUpdateStatus}`);
|
||||
|
||||
// Handle PDF or convert screenshot -> pdf if available
|
||||
|
||||
@@ -260,9 +260,8 @@ async function handleUnitedSCOCompletedJob(
|
||||
}
|
||||
|
||||
// Update patient status and name from United SCO eligibility result
|
||||
const updatePayload: Record<string, any> = { status: eligibilityStatus };
|
||||
const updatePayload: Record<string, any> = { status: eligibilityStatus, insuranceProvider: "United SCO" };
|
||||
|
||||
// Also update first/last name if we extracted them and patient has empty names
|
||||
if (firstName && (!patient.firstName || patient.firstName.trim() === "")) {
|
||||
updatePayload.firstName = firstName;
|
||||
}
|
||||
@@ -271,7 +270,7 @@ async function handleUnitedSCOCompletedJob(
|
||||
}
|
||||
|
||||
await storage.updatePatient(patient.id, updatePayload);
|
||||
outputResult.patientUpdateStatus = `Patient ${patient.id} updated: status=${eligibilityStatus}, name=${firstName} ${lastName} (United SCO eligibility: ${seleniumResult.eligibility})`;
|
||||
outputResult.patientUpdateStatus = `Patient ${patient.id} updated: status=${eligibilityStatus}, insuranceProvider=United SCO, name=${firstName} ${lastName} (United SCO eligibility: ${seleniumResult.eligibility})`;
|
||||
console.log(`[unitedsco-eligibility] ${outputResult.patientUpdateStatus}`);
|
||||
|
||||
// Handle PDF or convert screenshot -> pdf if available
|
||||
|
||||
@@ -3,16 +3,13 @@ import type { Request, Response } from "express";
|
||||
import { storage } from "../storage";
|
||||
import { z } from "zod";
|
||||
import { UserUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas";
|
||||
import jwt from 'jsonwebtoken';
|
||||
import bcrypt from 'bcrypt';
|
||||
import bcrypt from "bcrypt";
|
||||
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Type based on shared schema
|
||||
type SelectUser = z.infer<typeof UserUncheckedCreateInputObjectSchema>;
|
||||
|
||||
// Zod validation
|
||||
const userCreateSchema = UserUncheckedCreateInputObjectSchema;
|
||||
const userUpdateSchema = (UserUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>).partial();
|
||||
|
||||
@@ -25,16 +22,32 @@ router.get("/", async (req: Request, res: Response): Promise<any> => {
|
||||
const user = await storage.getUser(userId);
|
||||
if (!user) return res.status(404).send("User not found");
|
||||
|
||||
|
||||
const { password, ...safeUser } = user;
|
||||
res.json(safeUser);
|
||||
const { password, ...rest } = user;
|
||||
res.json({ ...rest, role: rest.role ?? "USER" });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send("Failed to fetch user");
|
||||
}
|
||||
});
|
||||
|
||||
// GET: User by ID
|
||||
router.get("/list", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
if (!req.user?.id) return res.status(401).send("Unauthorized");
|
||||
|
||||
const limit = Math.min(Number(req.query.limit) || 100, 500);
|
||||
const offset = Number(req.query.offset) || 0;
|
||||
const users = await storage.getUsers(limit, offset);
|
||||
const safe = users.map((u) => {
|
||||
const { password: _p, ...rest } = u;
|
||||
return { ...rest, role: rest.role ?? "USER" };
|
||||
});
|
||||
res.json(safe);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send("Failed to fetch users");
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/:id", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const idParam = req.params.id;
|
||||
@@ -46,35 +59,36 @@ router.get("/:id", async (req: Request, res: Response): Promise<any> => {
|
||||
const user = await storage.getUser(id);
|
||||
if (!user) return res.status(404).send("User not found");
|
||||
|
||||
const { password, ...safeUser } = user;
|
||||
res.json(safeUser);
|
||||
const { password, ...rest } = user;
|
||||
res.json({ ...rest, role: rest.role ?? "USER" });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send("Failed to fetch user");
|
||||
}
|
||||
});
|
||||
|
||||
// POST: Create new user
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const input = userCreateSchema.parse(req.body);
|
||||
const newUser = await storage.createUser(input);
|
||||
const { password, ...safeUser } = newUser;
|
||||
res.status(201).json(safeUser);
|
||||
const existing = await storage.getUserByUsername(input.username);
|
||||
if (existing) {
|
||||
return res.status(400).json({ error: "Username already exists" });
|
||||
}
|
||||
const hashed = await hashPassword(input.password);
|
||||
const newUser = await storage.createUser({ ...input, password: hashed });
|
||||
const { password: _p, ...rest } = newUser;
|
||||
res.status(201).json({ ...rest, role: rest.role ?? "USER" });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(400).json({ error: "Invalid user data", details: err });
|
||||
}
|
||||
});
|
||||
|
||||
// Function to hash password using bcrypt
|
||||
async function hashPassword(password: string) {
|
||||
const saltRounds = 10; // Salt rounds for bcrypt
|
||||
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
||||
return hashedPassword;
|
||||
const saltRounds = 10;
|
||||
return bcrypt.hash(password, saltRounds);
|
||||
}
|
||||
|
||||
// PUT: Update user
|
||||
router.put("/:id", async (req: Request, res: Response):Promise<any> => {
|
||||
try {
|
||||
const idParam = req.params.id;
|
||||
@@ -86,27 +100,24 @@ router.put("/:id", async (req: Request, res: Response):Promise<any> => {
|
||||
|
||||
const updates = userUpdateSchema.parse(req.body);
|
||||
|
||||
// If password is provided and non-empty, hash it
|
||||
if (updates.password && updates.password.trim() !== "") {
|
||||
updates.password = await hashPassword(updates.password);
|
||||
} else {
|
||||
// Remove password field if empty, so it won't overwrite existing password with blank
|
||||
delete updates.password;
|
||||
}
|
||||
|
||||
const updatedUser = await storage.updateUser(id, updates);
|
||||
if (!updatedUser) return res.status(404).send("User not found");
|
||||
|
||||
const { password, ...safeUser } = updatedUser;
|
||||
res.json(safeUser);
|
||||
const { password, ...rest } = updatedUser;
|
||||
res.json({ ...rest, role: rest.role ?? "USER" });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(400).json({ error: "Invalid update data", details: err });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE: Delete user
|
||||
router.delete("/:id", async (req: Request, res: Response):Promise<any> => {
|
||||
router.delete("/:id", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const idParam = req.params.id;
|
||||
if (!idParam) return res.status(400).send("User ID is required");
|
||||
@@ -114,6 +125,10 @@ router.delete("/:id", async (req: Request, res: Response):Promise<any> => {
|
||||
const id = parseInt(idParam);
|
||||
if (isNaN(id)) return res.status(400).send("Invalid user ID");
|
||||
|
||||
if (req.user?.id === id) {
|
||||
return res.status(403).json({ error: "Cannot delete your own account" });
|
||||
}
|
||||
|
||||
const success = await storage.deleteUser(id);
|
||||
if (!success) return res.status(404).send("User not found");
|
||||
|
||||
@@ -124,4 +139,4 @@ router.delete("/:id", async (req: Request, res: Response):Promise<any> => {
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
export default router;
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
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-cca-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-cca-client] transient network error ${code} (attempt ${attempt})`
|
||||
);
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, baseBackoffMs * attempt));
|
||||
}
|
||||
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 forwardToSeleniumCCAEligibilityAgent(
|
||||
insuranceEligibilityData: any
|
||||
): Promise<any> {
|
||||
const payload = { data: insuranceEligibilityData };
|
||||
const url = `/cca-eligibility`;
|
||||
log("selenium-cca-client", "POST cca-eligibility", {
|
||||
url: SELENIUM_AGENT_BASE + url,
|
||||
keys: Object.keys(payload),
|
||||
});
|
||||
const r = await requestWithRetries({ url, method: "POST", data: payload }, 4);
|
||||
log("selenium-cca-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 getSeleniumCCASessionStatus(
|
||||
sessionId: string
|
||||
): Promise<any> {
|
||||
const url = `/cca-session/${sessionId}/status`;
|
||||
log("selenium-cca-client", "GET session status", {
|
||||
url: SELENIUM_AGENT_BASE + url,
|
||||
sessionId,
|
||||
});
|
||||
const r = await requestWithRetries({ url, method: "GET" }, 4);
|
||||
log("selenium-cca-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;
|
||||
}
|
||||
Reference in New Issue
Block a user