backend routes seems done
This commit is contained in:
@@ -3,65 +3,105 @@ import { Request, Response } from "express";
|
|||||||
import { storage } from "../storage";
|
import { storage } from "../storage";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { ClaimUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas";
|
import { ClaimUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas";
|
||||||
|
import multer from "multer";
|
||||||
|
|
||||||
|
const upload = multer({ storage: multer.memoryStorage() });
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
|
router.post("/claim-pdf/upload", upload.single("file"), async (req: Request, res: Response): Promise<any> => {
|
||||||
|
try {
|
||||||
|
const { patientId, claimId } = req.body;
|
||||||
|
const file = req.file;
|
||||||
|
|
||||||
router.post("/claim-pdf/upload", upload.single("file"), async (req: Request, res: Response) => {
|
if (!patientId || !claimId) {
|
||||||
const { patientId, claimId } = req.body;
|
return res.status(400).json({ error: "Missing patientId, or claimId" });
|
||||||
const file = req.file;
|
}
|
||||||
|
|
||||||
if (!file || !patientId) return res.status(400).json({ error: "Missing file or patientId" });
|
if (!file){
|
||||||
|
return res.status(400).json({ error: "Missing file" });
|
||||||
|
}
|
||||||
|
|
||||||
const created = await storage.createClaimPdf({
|
const created = await storage.createClaimPdf(
|
||||||
filename: file.originalname,
|
parseInt(patientId),
|
||||||
patientId: parseInt(patientId),
|
parseInt(claimId),
|
||||||
claimId: claimId ? parseInt(claimId) : undefined,
|
file.originalname,
|
||||||
pdfData: file.buffer,
|
file.buffer
|
||||||
});
|
);
|
||||||
|
|
||||||
res.json(created);
|
res.json(created);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error uploading claim PDF:", err);
|
||||||
|
res.status(500).json({ error: "Internal server error" });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/claim-pdf/recent", async (req: Request, res: Response) => {
|
router.get("/claim-pdf/recent", async (req: Request, res: Response): Promise<any> => {
|
||||||
const limit = parseInt(req.query.limit as string) || 5;
|
try {
|
||||||
const offset = parseInt(req.query.offset as string) || 0;
|
const limit = parseInt(req.query.limit as string) || 5;
|
||||||
|
const offset = parseInt(req.query.offset as string) || 0;
|
||||||
|
|
||||||
const recent = await storage.getRecentClaimPdfs(limit, offset);
|
const recent = await storage.getRecentClaimPdfs(limit, offset);
|
||||||
res.json(recent);
|
res.json(recent);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error fetching recent PDFs:", err);
|
||||||
|
res.status(500).json({ error: "Internal server error" });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/claim-pdf/:id", async (req: Request, res: Response) => {
|
router.get("/claim-pdf/:id", async (req: Request, res: Response): Promise<any> => {
|
||||||
const id = parseInt(req.params.id);
|
try {
|
||||||
const pdf = await storage.getClaimPdfById(id);
|
const idParam = req.params.id;
|
||||||
|
if (!idParam) return res.status(400).json({ error: "Missing ID" });
|
||||||
|
|
||||||
if (!pdf) return res.status(404).json({ error: "PDF not found" });
|
const id = parseInt(idParam);
|
||||||
|
const pdf = await storage.getClaimPdfById(id);
|
||||||
|
|
||||||
res.setHeader("Content-Type", "application/pdf");
|
if (!pdf) return res.status(404).json({ error: "PDF not found" });
|
||||||
res.send(pdf.pdfData);
|
|
||||||
|
res.setHeader("Content-Type", "application/pdf");
|
||||||
|
res.send(pdf.pdfData);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error fetching PDF by ID:", err);
|
||||||
|
res.status(500).json({ error: "Internal server error" });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.delete("/claim-pdf/:id", async (req: Request, res: Response) => {
|
router.delete("/claim-pdf/:id", async (req: Request, res: Response): Promise<any> => {
|
||||||
const id = parseInt(req.params.id);
|
try {
|
||||||
const success = await storage.deleteClaimPdf(id);
|
const idParam = req.params.id;
|
||||||
|
if (!idParam) return res.status(400).json({ error: "Missing ID" });
|
||||||
|
|
||||||
res.json({ success });
|
const id = parseInt(idParam);
|
||||||
|
const success = await storage.deleteClaimPdf(id);
|
||||||
|
|
||||||
|
res.json({ success });
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error deleting PDF:", err);
|
||||||
|
res.status(500).json({ error: "Internal server error" });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.put("/claim-pdf/:id", upload.single("file"), async (req: Request, res: Response) => {
|
router.put("/claim-pdf/:id", upload.single("file"), async (req: Request, res: Response): Promise<any> => {
|
||||||
const id = parseInt(req.params.id);
|
try {
|
||||||
const file = req.file;
|
const idParam = req.params.id;
|
||||||
const claimId = req.body.claimId ? parseInt(req.body.claimId) : undefined;
|
if (!idParam) return res.status(400).json({ error: "Missing ID" });
|
||||||
|
|
||||||
const updated = await storage.updateClaimPdf(id, {
|
const id = parseInt(idParam);
|
||||||
claimId,
|
const file = req.file;
|
||||||
filename: file?.originalname,
|
|
||||||
pdfData: file?.buffer,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!updated) return res.status(404).json({ error: "PDF not found or update failed" });
|
const updated = await storage.updateClaimPdf(id, {
|
||||||
|
filename: file?.originalname,
|
||||||
|
pdfData: file?.buffer,
|
||||||
|
});
|
||||||
|
|
||||||
res.json(updated);
|
if (!updated) return res.status(404).json({ error: "PDF not found or update failed" });
|
||||||
|
|
||||||
|
res.json(updated);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error updating PDF:", err);
|
||||||
|
res.status(500).json({ error: "Internal server error" });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import multer from "multer";
|
|||||||
import { forwardToSeleniumAgent, forwardToSeleniumAgent2 } from "../services/seleniumClient";
|
import { forwardToSeleniumAgent, forwardToSeleniumAgent2 } from "../services/seleniumClient";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
@@ -95,7 +96,7 @@ router.post(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
const { patientId, claimId } = req.body; // ✅ Extract patientId from the body
|
const { patientId, claimId } = req.body;
|
||||||
|
|
||||||
if (!patientId || !claimId) {
|
if (!patientId || !claimId) {
|
||||||
return res.status(400).json({ error: "Missing patientId or claimId" });
|
return res.status(400).json({ error: "Missing patientId or claimId" });
|
||||||
@@ -113,6 +114,15 @@ router.post(
|
|||||||
const filename = path.basename(new URL(pdfUrl).pathname);
|
const filename = path.basename(new URL(pdfUrl).pathname);
|
||||||
const pdfResponse = await axios.get(pdfUrl, { responseType: "arraybuffer" });
|
const pdfResponse = await axios.get(pdfUrl, { responseType: "arraybuffer" });
|
||||||
|
|
||||||
|
// Temp savving the pdf incase, creatClaimPdf failed:
|
||||||
|
const tempDir = path.join(__dirname, "..", "..", "temp");
|
||||||
|
if (!fs.existsSync(tempDir)) {
|
||||||
|
fs.mkdirSync(tempDir, { recursive: true });
|
||||||
|
}
|
||||||
|
const filePath = path.join(tempDir, filename);
|
||||||
|
fs.writeFileSync(filePath, pdfResponse.data);
|
||||||
|
|
||||||
|
// saving at postgres db
|
||||||
await storage.createClaimPdf(
|
await storage.createClaimPdf(
|
||||||
parsedPatientId,
|
parsedPatientId,
|
||||||
parsedClaimId,
|
parsedClaimId,
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import staffRoutes from './staffs'
|
|||||||
import pdfExtractionRoutes from './pdfExtraction';
|
import pdfExtractionRoutes from './pdfExtraction';
|
||||||
import claimsRoutes from './claims';
|
import claimsRoutes from './claims';
|
||||||
import insuranceCredsRoutes from './insuranceCreds';
|
import insuranceCredsRoutes from './insuranceCreds';
|
||||||
|
import claimsPdfRoutes from './claim-pdf';
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.use('/patients', patientRoutes);
|
router.use('/patients', patientRoutes);
|
||||||
@@ -15,5 +17,6 @@ router.use('/staffs', staffRoutes);
|
|||||||
router.use('/pdfExtraction', pdfExtractionRoutes);
|
router.use('/pdfExtraction', pdfExtractionRoutes);
|
||||||
router.use('/claims', claimsRoutes);
|
router.use('/claims', claimsRoutes);
|
||||||
router.use('/insuranceCreds', insuranceCredsRoutes);
|
router.use('/insuranceCreds', insuranceCredsRoutes);
|
||||||
|
router.use('/claimspdf', claimsPdfRoutes);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
Reference in New Issue
Block a user