Files
DentalManagement09/apps/Backend/src/utils/appointmentAttachments.ts
T
GiteadandClaude Opus 5.5 4cd780e9b2 feat: Check Preauth from claim details, Tufts SCO approval PDF, all-folder appointment picker
- Same patient may be booked at the same time in different columns (appointment + preauth)
- New preauths save service lines and attachments so the details view shows them
- Claim/Preauth Details: renamed, Check Preauth button picks the worker from the claim's
  insurance (MassHealth, United SCO, Tufts SCO, CCA); OTP popup survives closing the window,
  window closes after a successful check
- Tufts SCO Check Preauth: searches Submitted by pre-auth number, saves the detail page PDF
  to the patient's new Approval folder (PdfTitleKey PREAUTH_APPROVAL migration)
- Appointment attachment picker lists files from all of the patient's folders

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 23:01:20 -05:00

49 lines
2.1 KiB
TypeScript

import path from "path";
import fs from "fs";
import { storage } from "../storage";
type QueuedFile = { originalname: string; bufferBase64: string; mimetype: string };
// Include attachments already saved on the appointment (e.g. X-rays added via "Select Procedures")
// that weren't re-uploaded in this request — read straight from disk by filePath.
export async function appendAppointmentFiles(
filesForQueue: QueuedFile[],
appointmentId: number | string | null | undefined,
): Promise<void> {
if (!appointmentId) return;
const seenNames = new Set(filesForQueue.map((f) => f.originalname));
const apptFiles = await storage.getAppointmentFiles(Number(appointmentId));
for (const f of apptFiles) {
if (!f.filePath || seenNames.has(f.filename)) continue;
const absPath = path.join(process.cwd(), f.filePath);
if (!fs.existsSync(absPath)) continue;
filesForQueue.push({
originalname: f.filename,
bufferBase64: fs.readFileSync(absPath).toString("base64"),
mimetype: f.mimeType ?? "application/octet-stream",
});
seenNames.add(f.filename);
}
}
// Links AppointmentFile records to a claim as ClaimFile records (dedup by filename).
// Called after claim create or update so files saved in the procedures form always appear.
export async function autoLinkAppointmentFiles(claimId: number, appointmentId: number | null | undefined) {
if (!appointmentId) return;
const { prisma: db } = await import("@repo/db/client");
const aptFiles = await db.appointmentFile.findMany({ where: { appointmentId } });
if (!aptFiles.length) return;
const existing = await db.claimFile.findMany({ where: { claimId }, select: { filename: true } });
const existingNames = new Set(existing.map((f: any) => f.filename));
const toLink = aptFiles.filter((f: any) => f.filename && !existingNames.has(f.filename));
if (!toLink.length) return;
await db.claimFile.createMany({
data: toLink.map((f: any) => ({
claimId,
filename: f.filename,
mimeType: f.mimeType ?? "",
...(f.filePath ? { filePath: String(f.filePath) } : {}),
})),
});
}