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; },