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
@@ -15,6 +15,7 @@ export interface ClaimSubmitProcessorInput {
variant?: "claimsubmit" | "claim-pre-auth";
/** When set, the frontend socket listener will handle the PDF download — skip auto-save */
socketId?: string;
userId: number;
}
export interface ClaimSubmitProcessorResult {
@@ -28,7 +29,8 @@ export interface ClaimSubmitProcessorResult {
async function savePdfFromSelenium(
pdf_url: string,
patientId: number,
variant: "claimsubmit" | "claim-pre-auth"
variant: "claimsubmit" | "claim-pre-auth",
userId: number
) {
try {
const filename = path.basename(new URL(pdf_url).pathname);
@@ -37,15 +39,19 @@ async function savePdfFromSelenium(
const resp = await axios.get(localUrl, { responseType: "arraybuffer", timeout: 30000 });
const groupTitleKey = variant === "claim-pre-auth" ? "INSURANCE_CLAIM_PREAUTH" : "INSURANCE_CLAIM";
const groupTitle = variant === "claim-pre-auth" ? "Preauth" : "Claims";
const category = variant === "claim-pre-auth" ? "PreAuth" : "Claims";
let group = await storage.findPdfGroupByPatientTitleKey(patientId, groupTitleKey);
if (!group) {
group = await storage.createPdfGroup(patientId, groupTitle, groupTitleKey);
}
const patient = await storage.getPatient(patientId);
const patientName = patient ? `${patient.firstName ?? ""} ${patient.lastName ?? ""}`.trim() : "";
await storage.createPdfFile(group.id!, filename, resp.data);
await storage.savePdfToCloudStorage(
userId,
patientId,
patientName || `Patient ${patientId}`,
category,
filename,
Buffer.from(resp.data)
);
console.log(`[claimSubmitProcessor] PDF saved for patient ${patientId}: ${filename}`);
} catch (err: any) {
// Non-fatal — claim was submitted; just log the PDF failure
@@ -56,7 +62,7 @@ async function savePdfFromSelenium(
export async function runClaimSubmitProcessor(
input: ClaimSubmitProcessorInput
): Promise<ClaimSubmitProcessorResult> {
const { enrichedPayload, files, claimId } = input;
const { enrichedPayload, files, claimId, userId } = input;
const variant = input.variant ?? "claimsubmit";
// Build the same payload shape the Python /claimsubmit endpoint expects
@@ -97,7 +103,8 @@ export async function runClaimSubmitProcessor(
await savePdfFromSelenium(
result.pdf_url,
Number(enrichedPayload.patientId),
variant
variant,
userId
);
}