From 0f0f3a6db988095e60badba5086c2a752d1cb112 Mon Sep 17 00:00:00 2001 From: Gitead Date: Wed, 8 Jul 2026 14:36:02 -0400 Subject: [PATCH] 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 --- apps/Backend/src/routes/documents.ts | 2 ++ .../src/storage/cloudStorage-storage.ts | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/apps/Backend/src/routes/documents.ts b/apps/Backend/src/routes/documents.ts index 5b816ce1..5d964517 100755 --- a/apps/Backend/src/routes/documents.ts +++ b/apps/Backend/src/routes/documents.ts @@ -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; } diff --git a/apps/Backend/src/storage/cloudStorage-storage.ts b/apps/Backend/src/storage/cloudStorage-storage.ts index c82decef..fc480fea 100755 --- a/apps/Backend/src/storage/cloudStorage-storage.ts +++ b/apps/Backend/src/storage/cloudStorage-storage.ts @@ -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 = { + 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; },