fix: two bugs in Cloud Storage PDF viewing surfaced by live testing

- PDF viewer hung on "Loading" forever for cloud-backed PDFs: streamFileTo
  pipes with {end: false} (by design, so cloud-storage.ts's own routes can
  call res.end() themselves), but the new /pdf-files/:id cloud branch never
  did, so the HTTP response never completed.
- Documents page showed nothing for patients whose PDFs are only in Cloud
  Storage: the page lists categories from the legacy PdfGroup table, which
  new saves stopped populating. savePdfToCloudStorage now also ensures a
  blob-less PdfGroup row exists per patient/category so the category still
  appears, with actual files served from Cloud Storage via the existing
  merge in the pdf-files/group listing routes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-08 14:36:02 -04:00
parent 3582da8510
commit 0f0f3a6db9
2 changed files with 25 additions and 0 deletions

View File

@@ -290,9 +290,11 @@ router.get(
);
try {
await storage.streamFileTo(res, cloudId);
if (!res.writableEnded) res.end();
} catch (streamErr) {
console.error("Error streaming cloud PDF file:", streamErr);
if (!res.headersSent) res.status(500).json({ error: "Failed to stream PDF" });
else if (!res.writableEnded) res.end();
}
return;
}

View File

@@ -613,6 +613,29 @@ export const cloudStorageStorage: IStorage = {
await cloudStorageStorage.finalizeFileUpload(cloudFile.id!);
const finalFile = await cloudStorageStorage.getFile(cloudFile.id!);
if (!finalFile) throw new Error("Failed to finalize PDF upload to cloud storage");
// Ensure a (blob-less) PdfGroup row exists for this patient/category so the
// Documents page — which lists categories from PdfGroup — still shows this
// category even when every PDF in it lives in Cloud Storage.
const CATEGORY_TO_TITLE_KEY: Record<string, { titleKey: string; title: string }> = {
Eligibility: { titleKey: "ELIGIBILITY_STATUS", title: "Eligibility Status" },
Claims: { titleKey: "INSURANCE_CLAIM", title: "Claims" },
PreAuth: { titleKey: "INSURANCE_CLAIM_PREAUTH", title: "Preauth" },
"Claim Status": { titleKey: "CLAIM_STATUS", title: "Claim Status" },
Attachments: { titleKey: "OTHER", title: "Attachments" },
};
const mapping = CATEGORY_TO_TITLE_KEY[category];
if (mapping) {
const existingGroup = await db.pdfGroup.findFirst({
where: { patientId, titleKey: mapping.titleKey as any },
});
if (!existingGroup) {
await db.pdfGroup.create({
data: { patientId, title: mapping.title, titleKey: mapping.titleKey as any },
});
}
}
return finalFile;
},