forked from fahrPool/fahrPool
36 lines
1.7 KiB
TypeScript
36 lines
1.7 KiB
TypeScript
import { renderToBuffer, type DocumentProps } from "@react-pdf/renderer";
|
|
import { createElement, type ReactElement } from "react";
|
|
import type { SupabaseClient } from "@supabase/supabase-js";
|
|
import { loadAuftrag, type Auftrag } from "./queries";
|
|
import { loadAuftragFotoBuffers, loadUnterschriftBuffer } from "./fotos";
|
|
import { buildProtokollBasisdaten } from "./protokoll";
|
|
import { ProtokollDocument } from "./ProtokollDocument";
|
|
import { verkleinereFoto } from "./foto-verkleinern";
|
|
|
|
// Gemeinsam fuer PDF-Download (Nutzer-Client, RLS) und Mailversand
|
|
// (Admin-Client im Dispatcher). null = Auftrag fehlt oder ist nicht
|
|
// abgeschlossen; Render-Fehler werden geworfen, damit der Aufrufer sie
|
|
// unterscheiden kann (Route: 500, Dispatcher: Retry).
|
|
export async function erzeugeProtokollPdf(
|
|
client: SupabaseClient,
|
|
auftragId: string
|
|
): Promise<{ auftrag: Auftrag; buffer: Buffer } | null> {
|
|
const auftrag = await loadAuftrag(auftragId, client);
|
|
if (!auftrag || auftrag.status !== "abgeschlossen") {
|
|
return null;
|
|
}
|
|
|
|
const [fotos, unterschriftBuffer] = await Promise.all([
|
|
loadAuftragFotoBuffers(auftragId, client),
|
|
loadUnterschriftBuffer(auftrag.unterschrift_storage_path, client),
|
|
]);
|
|
const verkleinert = await Promise.all(fotos.map(verkleinereFoto));
|
|
|
|
const props = { ...buildProtokollBasisdaten(auftrag), fotos: verkleinert, unterschriftBuffer };
|
|
// @react-pdf/renderer v4 typt ProtokollDocument nicht als ReactElement<DocumentProps>,
|
|
// obwohl es einen <Document>-Baum liefert -- bekannte Typluecke der Lib.
|
|
const element = createElement(ProtokollDocument, { props }) as unknown as ReactElement<DocumentProps>;
|
|
const buffer = await renderToBuffer(element);
|
|
return { auftrag, buffer };
|
|
}
|