diff --git a/src/lib/forms-shared.ts b/src/lib/forms-shared.ts index 79a51a5..d31c96f 100644 --- a/src/lib/forms-shared.ts +++ b/src/lib/forms-shared.ts @@ -162,12 +162,13 @@ export async function savePdfToDocuments(opts: { caseId: string; name: string; folder?: string; + mimeType?: string; }) { - const { blob, caseId, name, folder = "Forms & Letters" } = opts; + const { blob, caseId, name, folder = "Forms & Letters", mimeType = "application/pdf" } = opts; const path = `${caseId}/${Date.now()}-${name}`; const { error: upErr } = await supabase.storage .from("case-documents") - .upload(path, blob, { contentType: "application/pdf", upsert: false }); + .upload(path, blob, { contentType: mimeType, upsert: false }); if (upErr) throw upErr; const { data: u } = await supabase.auth.getUser(); const { error: insErr } = await supabase.from("documents").insert({ @@ -175,9 +176,37 @@ export async function savePdfToDocuments(opts: { folder, name, storage_path: path, - mime_type: "application/pdf", + mime_type: mimeType, size_bytes: blob.size, uploaded_by: u.user?.id ?? null, }); if (insErr) throw insErr; } + +export interface CaseLite { + id: string; + case_number: string; + title: string; +} + +export async function fetchAccessibleCases(): Promise { + const { data } = await supabase + .from("cases") + .select("id,case_number,title") + .order("opened_at", { ascending: false }) + .limit(500); + return (data ?? []) as CaseLite[]; +} + +export async function fetchCaseCustomValues(caseId: string): Promise> { + const { data } = await supabase + .from("case_field_values") + .select("value, field:custom_case_fields(key)") + .eq("case_id", caseId); + const map: Record = {}; + (data ?? []).forEach((row: any) => { + const k = row.field?.key; + if (k) map[k] = row.value ?? ""; + }); + return map; +}