forked from fahrPool/fahrPool
63 lines
2.9 KiB
TypeScript
63 lines
2.9 KiB
TypeScript
import { renderToBuffer, type DocumentProps } from "@react-pdf/renderer";
|
|
import { createElement, type ReactElement } from "react";
|
|
import type { SupabaseClient } from "@supabase/supabase-js";
|
|
import { loadAuftragStrikt, type Auftrag } from "./queries";
|
|
import { loadAuftragFotoBuffers, loadUnterschriftBuffer } from "./fotos";
|
|
import { buildProtokollBasisdaten } from "./protokoll";
|
|
import { ProtokollDocument } from "./ProtokollDocument";
|
|
import { verkleinereFoto } from "./foto-verkleinern";
|
|
import { ladeFirmaZumAuftrag, type FirmaBranding } from "@/lib/firma/queries";
|
|
import { STANDARD_FARBE_PRIMAER } from "@/lib/firma/farben";
|
|
|
|
// 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; firma: FirmaBranding } | null> {
|
|
const auftrag = await loadAuftragStrikt(auftragId, client);
|
|
if (!auftrag || auftrag.status !== "abgeschlossen") {
|
|
return null;
|
|
}
|
|
|
|
const firma = (await ladeFirmaZumAuftrag(client, auftragId)) ?? { name: "", logo_pfad: null, farbe_primaer: null };
|
|
|
|
const [fotos, unterschriftBuffer, logo] = await Promise.all([
|
|
loadAuftragFotoBuffers(auftragId, client),
|
|
loadUnterschriftBuffer(auftrag.unterschrift_storage_path, client),
|
|
ladeLogoFuerPdf(client, firma.logo_pfad),
|
|
]);
|
|
const verkleinert = await Promise.all(fotos.map(verkleinereFoto));
|
|
|
|
const props = {
|
|
...buildProtokollBasisdaten(auftrag),
|
|
fotos: verkleinert,
|
|
unterschriftBuffer,
|
|
firma: { name: firma.name, farbe: firma.farbe_primaer ?? STANDARD_FARBE_PRIMAER, logo },
|
|
};
|
|
// @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, firma };
|
|
}
|
|
|
|
// Das Logo ist Schmuck: fehlt es oder ist es ein SVG (von @react-pdf nicht
|
|
// zuverlaessig darstellbar), wird es weggelassen statt das Protokoll scheitern zu lassen.
|
|
async function ladeLogoFuerPdf(
|
|
client: SupabaseClient,
|
|
pfad: string | null
|
|
): Promise<{ buffer: Buffer; format: "png" | "jpg" } | null> {
|
|
if (!pfad) return null;
|
|
const format = pfad.endsWith(".png") ? "png" : pfad.endsWith(".jpg") ? "jpg" : null;
|
|
if (!format) return null;
|
|
const { data, error } = await client.storage.from("firmen-logos").download(pfad);
|
|
if (error || !data) {
|
|
console.warn("[protokoll] Firmenlogo nicht ladbar, PDF ohne Logo:", pfad, error?.message);
|
|
return null;
|
|
}
|
|
return { buffer: Buffer.from(await data.arrayBuffer()), format };
|
|
}
|