pdf upload checkpoint1
This commit is contained in:
67
apps/Backend/src/routes/claim-pdf.ts
Normal file
67
apps/Backend/src/routes/claim-pdf.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { Router } from "express";
|
||||
import { Request, Response } from "express";
|
||||
import { storage } from "../storage";
|
||||
import { z } from "zod";
|
||||
import { ClaimUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas";
|
||||
|
||||
const router = Router();
|
||||
|
||||
|
||||
router.post("/claim-pdf/upload", upload.single("file"), async (req: Request, res: Response) => {
|
||||
const { patientId, claimId } = req.body;
|
||||
const file = req.file;
|
||||
|
||||
if (!file || !patientId) return res.status(400).json({ error: "Missing file or patientId" });
|
||||
|
||||
const created = await storage.createClaimPdf({
|
||||
filename: file.originalname,
|
||||
patientId: parseInt(patientId),
|
||||
claimId: claimId ? parseInt(claimId) : undefined,
|
||||
pdfData: file.buffer,
|
||||
});
|
||||
|
||||
res.json(created);
|
||||
});
|
||||
|
||||
router.get("/claim-pdf/recent", async (req: Request, res: Response) => {
|
||||
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);
|
||||
res.json(recent);
|
||||
});
|
||||
|
||||
router.get("/claim-pdf/:id", async (req: Request, res: Response) => {
|
||||
const id = parseInt(req.params.id);
|
||||
const pdf = await storage.getClaimPdfById(id);
|
||||
|
||||
if (!pdf) return res.status(404).json({ error: "PDF not found" });
|
||||
|
||||
res.setHeader("Content-Type", "application/pdf");
|
||||
res.send(pdf.pdfData);
|
||||
});
|
||||
|
||||
router.delete("/claim-pdf/:id", async (req: Request, res: Response) => {
|
||||
const id = parseInt(req.params.id);
|
||||
const success = await storage.deleteClaimPdf(id);
|
||||
|
||||
res.json({ success });
|
||||
});
|
||||
|
||||
router.put("/claim-pdf/:id", upload.single("file"), async (req: Request, res: Response) => {
|
||||
const id = parseInt(req.params.id);
|
||||
const file = req.file;
|
||||
const claimId = req.body.claimId ? parseInt(req.body.claimId) : undefined;
|
||||
|
||||
const updated = await storage.updateClaimPdf(id, {
|
||||
claimId,
|
||||
filename: file?.originalname,
|
||||
pdfData: file?.buffer,
|
||||
});
|
||||
|
||||
if (!updated) return res.status(404).json({ error: "PDF not found or update failed" });
|
||||
|
||||
res.json(updated);
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -6,7 +6,6 @@ import { ClaimUncheckedCreateInputObjectSchema } from "@repo/db/usedSchemas";
|
||||
import multer from "multer";
|
||||
import { forwardToSeleniumAgent, forwardToSeleniumAgent2 } from "../services/seleniumClient";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import axios from "axios";
|
||||
|
||||
const router = Router();
|
||||
@@ -96,6 +95,14 @@ router.post(
|
||||
}
|
||||
|
||||
try{
|
||||
const { patientId, claimId } = req.body; // ✅ Extract patientId from the body
|
||||
|
||||
if (!patientId || !claimId) {
|
||||
return res.status(400).json({ error: "Missing patientId or claimId" });
|
||||
}
|
||||
const parsedPatientId = parseInt(patientId);
|
||||
const parsedClaimId = parseInt(claimId);
|
||||
|
||||
const result = await forwardToSeleniumAgent2();
|
||||
|
||||
if (result.status !== "success") {
|
||||
@@ -104,17 +111,14 @@ router.post(
|
||||
|
||||
const pdfUrl = result.pdf_url;
|
||||
const filename = path.basename(new URL(pdfUrl).pathname);
|
||||
|
||||
const tempDir = path.join(__dirname, "..", "..", "temp");
|
||||
if (!fs.existsSync(tempDir)) {
|
||||
fs.mkdirSync(tempDir, { recursive: true });
|
||||
}
|
||||
|
||||
const filePath = path.join(tempDir, filename);
|
||||
|
||||
// Download the PDF directly using axios
|
||||
const pdfResponse = await axios.get(pdfUrl, { responseType: "arraybuffer" });
|
||||
fs.writeFileSync(filePath, pdfResponse.data);
|
||||
|
||||
await storage.createClaimPdf(
|
||||
parsedPatientId,
|
||||
parsedClaimId,
|
||||
filename,
|
||||
pdfResponse.data
|
||||
);
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
|
||||
Reference in New Issue
Block a user