- Add MH Eligibility & History button: runs full MH eligibility flow then clicks member ID → service history, prints both PDFs via CDP, opens dual side-by-side PDF modal (eligibility auto-downloads, history does not) - Add CMSP Eligibility & History & Remaining button: same flow plus navigates back to member details, clicks View Accumulator, prints accumulator PDF via CDP; opens 3-panel side-by-side PDF modal - Generalize DualPdfPreviewModal to accept panels[] array (works for 2 or 3 PDFs) - Auto-download eligibility PDF via direct API URL to avoid Chrome Safe Browsing pause on blob: URL downloads - New backend processors, job types, and routes for both flows - New Python Selenium workers with stable CSS selectors (ng-bind, href*) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
160 lines
5.8 KiB
TypeScript
160 lines
5.8 KiB
TypeScript
/**
|
|
* Processor for "mh-eligibility-history-check" jobs.
|
|
* Runs MH eligibility + service history selenium flow and saves both PDFs
|
|
* to the patient's ELIGIBILITY_STATUS document group.
|
|
*/
|
|
import fs from "fs/promises";
|
|
import path from "path";
|
|
import { storage } from "../../storage";
|
|
import { emptyFolderContainingFile } from "../../utils/emptyTempFolder";
|
|
import forwardToPatientDataExtractorService from "../../services/patientDataExtractorService";
|
|
import {
|
|
callPythonSync,
|
|
splitName,
|
|
createOrUpdatePatientByInsuranceId,
|
|
} from "./_shared";
|
|
|
|
export interface EligibilityHistoryProcessorInput {
|
|
enrichedPayload: any;
|
|
userId: number;
|
|
insuranceId: string;
|
|
formFirstName?: string;
|
|
formLastName?: string;
|
|
formDob?: string;
|
|
}
|
|
|
|
export interface EligibilityHistoryProcessorResult {
|
|
patientUpdateStatus?: string;
|
|
pdfUploadStatus?: string;
|
|
pdfFileId?: number | null;
|
|
historyPdfFileId?: number | null;
|
|
pdfFilename?: string | null;
|
|
historyPdfFilename?: string | null;
|
|
}
|
|
|
|
export async function runEligibilityHistoryProcessor(
|
|
input: EligibilityHistoryProcessorInput
|
|
): Promise<EligibilityHistoryProcessorResult> {
|
|
const { enrichedPayload, userId, insuranceId, formFirstName, formLastName, formDob } = input;
|
|
|
|
const seleniumResult = await callPythonSync("/mh-eligibility-history-check", {
|
|
data: enrichedPayload,
|
|
});
|
|
|
|
const outputResult: EligibilityHistoryProcessorResult = {};
|
|
|
|
// Extract patient name
|
|
const seleniumFirst = seleniumResult.firstName?.trim() || null;
|
|
const seleniumLast = seleniumResult.lastName?.trim() || null;
|
|
const seleniumName = seleniumResult.name?.trim() || null;
|
|
const seleniumInsurance = seleniumResult.insurance?.trim() || null;
|
|
|
|
const extracted: { firstName?: string | null; lastName?: string | null } = {};
|
|
|
|
if (seleniumFirst || seleniumLast) {
|
|
extracted.firstName = seleniumFirst;
|
|
extracted.lastName = seleniumLast;
|
|
} else if (seleniumName) {
|
|
const parts = splitName(seleniumName);
|
|
extracted.firstName = parts.firstName || null;
|
|
extracted.lastName = parts.lastName || null;
|
|
}
|
|
|
|
if (!extracted.firstName && !extracted.lastName && seleniumResult?.pdf_path?.endsWith(".pdf")) {
|
|
try {
|
|
const pdfBuffer = await fs.readFile(seleniumResult.pdf_path);
|
|
const extraction = await forwardToPatientDataExtractorService({
|
|
buffer: pdfBuffer,
|
|
originalname: path.basename(seleniumResult.pdf_path),
|
|
mimetype: "application/pdf",
|
|
} as any);
|
|
if (extraction.name) {
|
|
const parts = splitName(extraction.name);
|
|
extracted.firstName = parts.firstName || null;
|
|
extracted.lastName = parts.lastName || null;
|
|
}
|
|
} catch (e) {
|
|
console.error("[eligibilityHistoryProcessor] PDF name extraction failed:", e);
|
|
}
|
|
}
|
|
|
|
const preferFirst = extracted.firstName || formFirstName || null;
|
|
const preferLast = extracted.lastName || formLastName || null;
|
|
|
|
let patient;
|
|
try {
|
|
patient = await createOrUpdatePatientByInsuranceId({
|
|
insuranceId,
|
|
firstName: preferFirst,
|
|
lastName: preferLast,
|
|
dob: formDob,
|
|
userId,
|
|
});
|
|
} catch (e: any) {
|
|
throw new Error(`Failed to create/update patient: ${e.message}`);
|
|
}
|
|
|
|
if (patient && patient.id !== undefined) {
|
|
let newStatus = "UNKNOWN";
|
|
if (seleniumResult.eligibility === "Y") newStatus = "ACTIVE";
|
|
else if (seleniumResult.eligibility === "N") newStatus = "INACTIVE";
|
|
|
|
const updates: any = { status: newStatus };
|
|
if (seleniumInsurance) updates.insuranceProvider = seleniumInsurance;
|
|
await storage.updatePatient(patient.id, updates);
|
|
outputResult.patientUpdateStatus = `Patient status updated to ${newStatus}`;
|
|
|
|
// Helper: save a PDF buffer to the patient's ELIGIBILITY_STATUS group
|
|
const saveToGroup = async (pdfPath: string): Promise<number | null> => {
|
|
try {
|
|
const buffer = await fs.readFile(pdfPath);
|
|
const groupTitleKey = "ELIGIBILITY_STATUS";
|
|
const groupTitle = "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");
|
|
const created = await storage.createPdfFile(group.id, path.basename(pdfPath), buffer);
|
|
if (created && typeof created === "object" && "id" in created) {
|
|
return Number(created.id);
|
|
}
|
|
return null;
|
|
} catch (e: any) {
|
|
console.error("[eligibilityHistoryProcessor] saveToGroup failed:", e.message);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
// Save eligibility PDF
|
|
let eligibilityPdfFileId: number | null = null;
|
|
if (seleniumResult.pdf_path?.endsWith(".pdf")) {
|
|
eligibilityPdfFileId = await saveToGroup(seleniumResult.pdf_path);
|
|
outputResult.pdfUploadStatus = eligibilityPdfFileId
|
|
? "Eligibility PDF saved"
|
|
: "Eligibility PDF upload failed";
|
|
outputResult.pdfFilename = path.basename(seleniumResult.pdf_path);
|
|
} else {
|
|
outputResult.pdfUploadStatus = "No eligibility PDF returned by Selenium";
|
|
}
|
|
outputResult.pdfFileId = eligibilityPdfFileId;
|
|
|
|
// Save history PDF
|
|
let historyPdfFileId: number | null = null;
|
|
if (seleniumResult.history_pdf_path?.endsWith(".pdf")) {
|
|
historyPdfFileId = await saveToGroup(seleniumResult.history_pdf_path);
|
|
outputResult.historyPdfFilename = path.basename(seleniumResult.history_pdf_path);
|
|
}
|
|
outputResult.historyPdfFileId = historyPdfFileId;
|
|
} else {
|
|
outputResult.patientUpdateStatus = "Patient not found or missing ID; no update performed";
|
|
}
|
|
|
|
// Cleanup temp files
|
|
try {
|
|
if (seleniumResult.pdf_path) await emptyFolderContainingFile(seleniumResult.pdf_path);
|
|
} catch (e) {
|
|
console.error("[eligibilityHistoryProcessor] cleanup failed:", e);
|
|
}
|
|
|
|
return outputResult;
|
|
}
|