Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-19 18:22:20 +00:00
co-authored by renee-png
parent 53704a5687
commit 04f36c6cab
+35
View File
@@ -171,3 +171,38 @@ export function downloadStatusReport(opts: StatusReportOptions, filename: string
const doc = generateStatusReportPdf(opts);
doc.save(filename);
}
import { supabase } from "@/integrations/supabase/client";
/**
* Upload a status-report PDF to the `generated-documents` bucket and create
* a `generated_documents` row with kind='status_report'. Returns the new row id.
*/
export async function saveStatusReportToDb(
opts: StatusReportOptions,
filename: string,
meta: { userId: string; payload?: Record<string, unknown> },
): Promise<{ id: string; storage_path: string }> {
const doc = generateStatusReportPdf(opts);
const blob = doc.output("blob");
const safe = filename.replace(/[^a-zA-Z0-9._-]/g, "_");
const storagePath = `${meta.userId}/${Date.now()}-${safe}`;
const { error: upErr } = await supabase.storage
.from("generated-documents")
.upload(storagePath, blob, { contentType: "application/pdf" });
if (upErr) throw upErr;
const { data, error } = await supabase
.from("generated_documents")
.insert({
kind: "status_report",
name: filename.replace(/\.pdf$/i, ""),
storage_path: storagePath,
created_by: meta.userId,
payload: (meta.payload ?? {}) as never,
})
.select("id, storage_path")
.single();
if (error) throw error;
return data as { id: string; storage_path: string };
}