document page updated

This commit is contained in:
2025-07-17 19:21:16 +05:30
parent aa913673de
commit 1071b80930
11 changed files with 838 additions and 1089 deletions

View File

@@ -132,7 +132,7 @@ router.post(
return sendError(res, "Unauthorized: user info missing", 401);
}
const { patientId, claimId, pdf_url } = req.body;
const { patientId, pdf_url } = req.body;
if (!pdf_url) {
return sendError(res, "Missing pdf_url");
@@ -141,26 +141,35 @@ router.post(
if (!patientId) {
return sendError(res, "Missing Patient Id");
}
if (!claimId) {
return sendError(res, "Missing Claim Id");
}
const parsedPatientId = parseInt(patientId);
const parsedClaimId = parseInt(claimId);
const filename = path.basename(new URL(pdf_url).pathname);
const pdfResponse = await axios.get(pdf_url, {
responseType: "arraybuffer",
});
// saving at postgres db
await storage.createClaimPdf(
const groupTitle = `Insurance Claim`;
const groupCategory = "CLAIM";
// ✅ Find or create PDF group for this claim
let group = await storage.findPdfGroupByPatientTitleAndCategory(
parsedPatientId,
parsedClaimId,
filename,
pdfResponse.data
groupTitle,
groupCategory
);
if (!group) {
group = await storage.createPdfGroup(
parsedPatientId,
groupTitle,
groupCategory
);
}
// ✅ Save PDF file into that group
await storage.createPdfFile(group.id!, filename, pdfResponse.data);
return res.json({
success: true,
pdfPath: `/temp/${filename}`,

View File

@@ -33,6 +33,24 @@ router.post(
}
);
router.get(
"/pdf-groups/patient/:patientId",
async (req: Request, res: Response): Promise<any> => {
try {
const { patientId } = req.params;
if (!patientId) {
return res.status(400).json({ error: "Missing patient ID" });
}
const groups = await storage.getPdfGroupsByPatientId(parseInt(patientId));
res.json(groups);
} catch (err) {
console.error("Error fetching groups by patient ID:", err);
res.status(500).json({ error: "Internal server error" });
}
}
);
router.get(
"/pdf-groups/:id",
async (req: Request, res: Response): Promise<any> => {
@@ -126,6 +144,21 @@ router.post(
}
);
router.get("/pdf-files/group/:groupId", async (req: Request, res: Response):Promise<any> => {
try {
const idParam = req.params.groupId;
if (!idParam) {
return res.status(400).json({ error: "Missing Groupt ID" });
}
const groupId = parseInt(idParam);
const files = await storage.getPdfFilesByGroupId(groupId); // implement this
res.json(files);
} catch (err) {
res.status(500).json({ error: "Internal server error" });
}
});
router.get(
"/pdf-files/:id",
async (req: Request, res: Response): Promise<any> => {

View File

@@ -2,6 +2,8 @@ import { Router } from "express";
import { Request, Response } from "express";
import { storage } from "../storage";
import { forwardToSeleniumInsuranceEligibilityAgent } from "../services/seleniumInsuranceEligibilityClient";
import fs from "fs/promises";
import path from "path";
const router = Router();
@@ -48,15 +50,55 @@ router.post("/check", async (req: Request, res: Response): Promise<any> => {
const newStatus = result.eligibility === "Y" ? "active" : "inactive";
await storage.updatePatient(patient.id, { status: newStatus });
result.patientUpdateStatus = `Patient status updated to ${newStatus}`;
// ✅ Step 2: Handle PDF Upload
if (result.pdf_path && result.pdf_path.endsWith(".pdf")) {
const pdfBuffer = await fs.readFile(result.pdf_path);
const groupTitle = "Eligibility PDFs";
const groupCategory = "ELIGIBILITY";
let group = await storage.findPdfGroupByPatientTitleAndCategory(
patient.id,
groupTitle,
groupCategory
);
// Step 2b: Create group if it doesnt exist
if (!group) {
group = await storage.createPdfGroup(
patient.id,
groupTitle,
groupCategory
);
}
if (!group?.id) {
throw new Error("PDF group creation failed: missing group ID");
}
await storage.createPdfFile(
group.id,
path.basename(result.pdf_path),
pdfBuffer
);
await fs.unlink(result.pdf_path);
result.pdfUploadStatus = `PDF saved to group: ${group.title}`;
} else {
result.pdfUploadStatus =
"No valid PDF path provided by Selenium, Couldn't upload pdf to server.";
}
} else {
console.warn(
`No patient found with insuranceId: ${insuranceEligibilityData.memberId}`
);
result.patientUpdateStatus =
"Patient not found or missing ID; no update performed";
}
res.json(result);
res.json({
patientUpdateStatus: result.patientUpdateStatus,
pdfUploadStatus: result.pdfUploadStatus,
});
} catch (err: any) {
console.error(err);
return res.status(500).json({

View File

@@ -266,11 +266,15 @@ export interface IStorage {
// Group management
createPdfGroup(
patientId: number,
title: string,
category: PdfCategory
): Promise<PdfGroup>;
patientId: number,
title: string,
category: PdfCategory
): Promise<PdfGroup>;
findPdfGroupByPatientTitleAndCategory(
patientId: number,
title: string,
category: PdfCategory
): Promise<PdfGroup | undefined>;
getAllPdfGroups(): Promise<PdfGroup[]>;
getPdfGroupById(id: number): Promise<PdfGroup | undefined>;
getPdfGroupsByPatientId(patientId: number): Promise<PdfGroup[]>;
@@ -694,6 +698,18 @@ export const storage: IStorage = {
});
},
async findPdfGroupByPatientTitleAndCategory(patientId, title, category) {
return (
(await db.pdfGroup.findFirst({
where: {
patientId,
title,
category,
},
})) ?? undefined
);
},
async getPdfGroupById(id) {
return (await db.pdfGroup.findUnique({ where: { id } })) ?? undefined;
},