feat: save insurance PDFs (eligibility/claims/preauth) to Cloud Storage instead of Postgres blobs

New saves go into the patient's Cloud Storage folder under category subfolders
(Eligibility/Claims/PreAuth/Claim Status/Attachments) via a new
storage.savePdfToCloudStorage helper, instead of the legacy PdfGroup/PdfFile
Postgres blob tables. Old records and their read paths are left untouched for
backward compatibility; PDF list/content/delete endpoints and viewers now
transparently handle both legacy numeric ids and new "cloud:<id>" ids.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-08 00:20:37 -04:00
co-authored by Claude Sonnet 5
parent 1190bc0494
commit 99cdfa702a
36 changed files with 496 additions and 296 deletions
@@ -79,7 +79,7 @@ export interface CCAClaimProcessorInput {
export async function runCCAClaimProcessor(
input: CCAClaimProcessorInput,
jobId: string
): Promise<{ status: string; claimNumber?: string | null; pdfFileId?: number | null }> {
): Promise<{ status: string; claimNumber?: string | null; pdfFileId?: number | string | null }> {
const { enrichedPayload, userId, claimId, socketId } = input;
log("cca-claim-processor", "starting Python agent session", { claimId });
@@ -106,19 +106,22 @@ export async function runCCAClaimProcessor(
seleniumResult?.pdfFilename || `cca_claim_${claimId ?? "unknown"}_${Date.now()}.pdf`;
// Save PDF to patient's Claims document group
let pdfFileId: number | null = null;
let pdfFileId: number | string | null = null;
if (pdfBase64 && enrichedPayload?.claim?.patientId) {
try {
const patientId = Number(enrichedPayload.claim.patientId);
const pdfBuffer = Buffer.from(pdfBase64, "base64");
let group = await storage.findPdfGroupByPatientTitleKey(patientId, "INSURANCE_CLAIM");
if (!group) {
group = await storage.createPdfGroup(patientId, "Claims", "INSURANCE_CLAIM");
}
const created = await storage.createPdfFile(group.id!, pdfFilename, pdfBuffer);
if (created && typeof created === "object" && "id" in created) {
pdfFileId = Number((created as any).id);
}
const patient = await storage.getPatient(patientId);
const patientName = patient ? `${patient.firstName ?? ""} ${patient.lastName ?? ""}`.trim() : "";
const cloudFile = await storage.savePdfToCloudStorage(
userId,
patientId,
patientName || `Patient ${patientId}`,
"Claims",
pdfFilename,
pdfBuffer
);
pdfFileId = `cloud:${cloudFile.id}`;
log("cca-claim-processor", "PDF saved", { pdfFilename, pdfFileId, patientId });
} catch (e: any) {
log("cca-claim-processor", "failed to save PDF", { error: e?.message });