forked from fahrPool/fahrPool
fix: PDF-Pfad unterscheidet Fehler von nicht gefunden, sharp lazy, PNG-Alpha weiss, SMTP_SECURE case-insensitive
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2787de8445
commit
b3e360f530
|
|
@ -36,23 +36,23 @@ export async function POST(request: Request): Promise<Response> {
|
|||
return NextResponse.json({ fehler: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const admin = createAdminClient();
|
||||
const handler: Record<string, Handler> = {};
|
||||
const waCfg = getWhatsAppConfig();
|
||||
if (waCfg) {
|
||||
handler.auftrag_erstellt = whatsappHandler(waCfg);
|
||||
}
|
||||
const mailCfg = getEmailConfig();
|
||||
if (mailCfg) {
|
||||
handler.protokoll_abgeschlossen = protokollMailHandler(admin, mailCfg);
|
||||
}
|
||||
|
||||
if (Object.keys(handler).length === 0) {
|
||||
if (!waCfg && !mailCfg) {
|
||||
console.warn("[notifications] kein Kanal konfiguriert – Outbox wird nicht verarbeitet");
|
||||
return NextResponse.json({ verarbeitet: 0, uebersprungen: true });
|
||||
}
|
||||
|
||||
try {
|
||||
const admin = createAdminClient();
|
||||
if (waCfg) {
|
||||
handler.auftrag_erstellt = whatsappHandler(waCfg);
|
||||
}
|
||||
if (mailCfg) {
|
||||
handler.protokoll_abgeschlossen = protokollMailHandler(admin, mailCfg);
|
||||
}
|
||||
const ergebnis = await verarbeiteOutbox(admin, handler);
|
||||
return NextResponse.json(ergebnis);
|
||||
} catch (e) {
|
||||
|
|
|
|||
|
|
@ -60,4 +60,17 @@ describe("verkleinereFoto", () => {
|
|||
const ergebnis = await verkleinereFoto(kaputt);
|
||||
expect(ergebnis).toBe(kaputt);
|
||||
});
|
||||
|
||||
it("legt transparente PNG-Pixel auf weissen Hintergrund (nicht schwarz)", async () => {
|
||||
const png = await sharp({
|
||||
create: { width: 100, height: 100, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } },
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
const ergebnis = await verkleinereFoto(foto(png, "png"));
|
||||
const { data } = await sharp(ergebnis.buffer).raw().toBuffer({ resolveWithObject: true });
|
||||
expect(data[0]).toBeGreaterThan(240);
|
||||
expect(data[1]).toBeGreaterThan(240);
|
||||
expect(data[2]).toBeGreaterThan(240);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import sharp from "sharp";
|
||||
import type { ProtokollFoto } from "./protokoll";
|
||||
|
||||
export const MAX_KANTE_PX = 1600;
|
||||
|
|
@ -10,9 +9,12 @@ export const JPEG_QUALITAET = 80;
|
|||
// das Original einbetten als das Protokoll scheitern lassen.
|
||||
export async function verkleinereFoto(foto: ProtokollFoto): Promise<ProtokollFoto> {
|
||||
try {
|
||||
// Lazy, damit ein nicht ladbares natives Binary nur das Verkleinern betrifft.
|
||||
const { default: sharp } = await import("sharp");
|
||||
const buffer = await sharp(foto.buffer)
|
||||
.rotate()
|
||||
.resize({ width: MAX_KANTE_PX, height: MAX_KANTE_PX, fit: "inside", withoutEnlargement: true })
|
||||
.flatten({ background: "#ffffff" })
|
||||
.jpeg({ quality: JPEG_QUALITAET })
|
||||
.toBuffer();
|
||||
return { ...foto, buffer, format: "jpg" };
|
||||
|
|
|
|||
|
|
@ -106,10 +106,13 @@ export async function loadUnterschriftUrl(storagePath: string | null): Promise<s
|
|||
// nicht als externe Links (siehe ProtokollDocument.tsx).
|
||||
export async function loadAuftragFotoBuffers(auftragId: string, client?: SupabaseClient): Promise<ProtokollFoto[]> {
|
||||
const supabase = client ?? (await createClient());
|
||||
const { data } = await supabase
|
||||
const { data, error } = await supabase
|
||||
.from("auftrag_fotos")
|
||||
.select("position, storage_path, uploaded_at, beschreibung")
|
||||
.eq("auftrag_id", auftragId);
|
||||
if (error) {
|
||||
throw new Error(`Fotoliste laden fehlgeschlagen: ${error.message}`);
|
||||
}
|
||||
|
||||
if (!data || data.length === 0) {
|
||||
return [];
|
||||
|
|
@ -118,10 +121,12 @@ export async function loadAuftragFotoBuffers(auftragId: string, client?: Supabas
|
|||
const sortiert = sortiereUndBeschrifteFotos(data);
|
||||
|
||||
const fotos = await Promise.all(
|
||||
sortiert.map(async (foto): Promise<ProtokollFoto | null> => {
|
||||
const { data: blob } = await supabase.storage.from("auftrag-fotos").download(foto.storage_path);
|
||||
if (!blob) {
|
||||
return null;
|
||||
sortiert.map(async (foto): Promise<ProtokollFoto> => {
|
||||
const { data: blob, error: downloadFehler } = await supabase.storage
|
||||
.from("auftrag-fotos")
|
||||
.download(foto.storage_path);
|
||||
if (downloadFehler || !blob) {
|
||||
throw new Error(`Foto-Download fehlgeschlagen (${foto.storage_path}): ${downloadFehler?.message ?? "keine Daten"}`);
|
||||
}
|
||||
const buffer = Buffer.from(await blob.arrayBuffer());
|
||||
const format: "png" | "jpg" = blob.type === "image/png" ? "png" : "jpg";
|
||||
|
|
@ -129,7 +134,7 @@ export async function loadAuftragFotoBuffers(auftragId: string, client?: Supabas
|
|||
})
|
||||
);
|
||||
|
||||
return fotos.filter((foto): foto is ProtokollFoto => foto !== null);
|
||||
return fotos;
|
||||
}
|
||||
|
||||
// Analoger Rohdaten-Loader fuer die Unterschrift (siehe loadUnterschriftUrl
|
||||
|
|
@ -140,9 +145,9 @@ export async function loadUnterschriftBuffer(storagePath: string | null, client?
|
|||
return null;
|
||||
}
|
||||
const supabase = client ?? (await createClient());
|
||||
const { data: blob } = await supabase.storage.from("auftrag-unterschriften").download(storagePath);
|
||||
if (!blob) {
|
||||
return null;
|
||||
const { data: blob, error } = await supabase.storage.from("auftrag-unterschriften").download(storagePath);
|
||||
if (error || !blob) {
|
||||
throw new Error(`Unterschrift-Download fehlgeschlagen: ${error?.message ?? "keine Daten"}`);
|
||||
}
|
||||
return Buffer.from(await blob.arrayBuffer());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,95 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import type { SupabaseClient } from "@supabase/supabase-js";
|
||||
import { erzeugeProtokollPdf } from "./protokoll-pdf";
|
||||
import { loadAuftragFotoBuffers, loadUnterschriftBuffer } from "./fotos";
|
||||
import { loadAuftragStrikt } from "./queries";
|
||||
|
||||
type Antwort = { data: unknown; error: { message: string } | null };
|
||||
|
||||
// Minimaler Fake: .from(t).select().eq().maybeSingle() / awaitbar, storage.from(b).download(p)
|
||||
function fakeClient(opts: {
|
||||
auftraege?: Antwort;
|
||||
fotos?: Antwort;
|
||||
download?: (bucket: string, pfad: string) => Antwort;
|
||||
}): SupabaseClient {
|
||||
const antwort = (a: Antwort | undefined): Antwort => a ?? { data: null, error: null };
|
||||
const query = (a: Antwort) => {
|
||||
const q: Record<string, unknown> = {
|
||||
select: () => q,
|
||||
eq: () => q,
|
||||
maybeSingle: async () => a,
|
||||
then: (res: (v: Antwort) => unknown) => Promise.resolve(a).then(res),
|
||||
};
|
||||
return q;
|
||||
};
|
||||
return {
|
||||
from: (tabelle: string) => query(antwort(tabelle === "auftraege" ? opts.auftraege : opts.fotos)),
|
||||
storage: {
|
||||
from: (bucket: string) => ({
|
||||
download: async (pfad: string) => (opts.download ?? (() => ({ data: null, error: null })))(bucket, pfad),
|
||||
}),
|
||||
},
|
||||
} as unknown as SupabaseClient;
|
||||
}
|
||||
|
||||
const FEHLER = { message: "boom" };
|
||||
const FOTOZEILE = { position: "front", storage_path: "a/front.jpg", uploaded_at: "2026-01-01T09:00:00Z", beschreibung: null };
|
||||
const BLOB = { arrayBuffer: async () => new ArrayBuffer(4), type: "image/jpeg" };
|
||||
|
||||
describe("loadAuftragStrikt", () => {
|
||||
it("wirft bei Abfragefehler", async () => {
|
||||
await expect(loadAuftragStrikt("a1", fakeClient({ auftraege: { data: null, error: FEHLER } }))).rejects.toThrow(/boom/);
|
||||
});
|
||||
|
||||
it("liefert null, wenn der Auftrag nicht existiert", async () => {
|
||||
expect(await loadAuftragStrikt("a1", fakeClient({ auftraege: { data: null, error: null } }))).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("erzeugeProtokollPdf (Fehlerunterscheidung)", () => {
|
||||
it("liefert null bei fehlendem Auftrag", async () => {
|
||||
expect(await erzeugeProtokollPdf(fakeClient({ auftraege: { data: null, error: null } }), "a1")).toBeNull();
|
||||
});
|
||||
|
||||
it("liefert null bei nicht abgeschlossenem Auftrag", async () => {
|
||||
const client = fakeClient({ auftraege: { data: { id: "a1", status: "in_bearbeitung" }, error: null } });
|
||||
expect(await erzeugeProtokollPdf(client, "a1")).toBeNull();
|
||||
});
|
||||
|
||||
it("wirft bei Lesefehler des Auftrags (kein null)", async () => {
|
||||
await expect(
|
||||
erzeugeProtokollPdf(fakeClient({ auftraege: { data: null, error: FEHLER } }), "a1")
|
||||
).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadAuftragFotoBuffers (strikt)", () => {
|
||||
it("wirft, wenn die Fotoliste nicht geladen werden kann", async () => {
|
||||
await expect(loadAuftragFotoBuffers("a1", fakeClient({ fotos: { data: null, error: FEHLER } }))).rejects.toThrow(/Fotoliste/);
|
||||
});
|
||||
|
||||
it("wirft, wenn ein Foto-Download fehlschlaegt", async () => {
|
||||
const client = fakeClient({
|
||||
fotos: { data: [FOTOZEILE], error: null },
|
||||
download: () => ({ data: null, error: FEHLER }),
|
||||
});
|
||||
await expect(loadAuftragFotoBuffers("a1", client)).rejects.toThrow(/Foto-Download/);
|
||||
});
|
||||
|
||||
it("liefert Fotos bei Erfolg und [] ohne Fotos", async () => {
|
||||
const ok = fakeClient({ fotos: { data: [FOTOZEILE], error: null }, download: () => ({ data: BLOB, error: null }) });
|
||||
expect(await loadAuftragFotoBuffers("a1", ok)).toHaveLength(1);
|
||||
expect(await loadAuftragFotoBuffers("a1", fakeClient({ fotos: { data: [], error: null } }))).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadUnterschriftBuffer (strikt)", () => {
|
||||
it("liefert null ohne Pfad", async () => {
|
||||
expect(await loadUnterschriftBuffer(null, fakeClient({}))).toBeNull();
|
||||
});
|
||||
|
||||
it("wirft, wenn der Download bei gesetztem Pfad fehlschlaegt", async () => {
|
||||
const client = fakeClient({ download: () => ({ data: null, error: FEHLER }) });
|
||||
await expect(loadUnterschriftBuffer("a/sig.png", client)).rejects.toThrow(/Unterschrift/);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
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 { loadAuftragStrikt, type Auftrag } from "./queries";
|
||||
import { loadAuftragFotoBuffers, loadUnterschriftBuffer } from "./fotos";
|
||||
import { buildProtokollBasisdaten } from "./protokoll";
|
||||
import { ProtokollDocument } from "./ProtokollDocument";
|
||||
|
|
@ -15,7 +15,7 @@ export async function erzeugeProtokollPdf(
|
|||
client: SupabaseClient,
|
||||
auftragId: string
|
||||
): Promise<{ auftrag: Auftrag; buffer: Buffer } | null> {
|
||||
const auftrag = await loadAuftrag(auftragId, client);
|
||||
const auftrag = await loadAuftragStrikt(auftragId, client);
|
||||
if (!auftrag || auftrag.status !== "abgeschlossen") {
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -107,6 +107,16 @@ export async function loadAuftrag(id: string, client?: SupabaseClient): Promise<
|
|||
return (data as unknown as Auftrag) ?? null;
|
||||
}
|
||||
|
||||
// Strikte Variante fuer die PDF-Erzeugung: null nur, wenn der Auftrag nicht
|
||||
// existiert; Abfragefehler werden geworfen (Retry statt "nicht gefunden").
|
||||
export async function loadAuftragStrikt(id: string, client: SupabaseClient): Promise<Auftrag | null> {
|
||||
const { data, error } = await client.from("auftraege").select(AUFTRAG_SELECT).eq("id", id).maybeSingle();
|
||||
if (error) {
|
||||
throw new Error(`Auftrag laden fehlgeschlagen: ${error.message}`);
|
||||
}
|
||||
return (data as unknown as Auftrag) ?? null;
|
||||
}
|
||||
|
||||
export async function findeFolgeauftrag(vorgaengerId: string): Promise<{ id: string } | null> {
|
||||
const supabase = await createClient();
|
||||
const { data } = await supabase
|
||||
|
|
|
|||
|
|
@ -60,4 +60,12 @@ describe("getEmailConfig", () => {
|
|||
process.env.SMTP_SECURE = "yes";
|
||||
expect(getEmailConfig()?.secure).toBe(false);
|
||||
});
|
||||
|
||||
it("SMTP_SECURE ist unabhaengig von Gross-/Kleinschreibung", () => {
|
||||
setzePflicht();
|
||||
for (const wert of ["TRUE", "True", " true "]) {
|
||||
process.env.SMTP_SECURE = wert;
|
||||
expect(getEmailConfig()?.secure).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ export function getEmailConfig(): EmailConfig | null {
|
|||
return {
|
||||
host,
|
||||
port,
|
||||
secure: process.env.SMTP_SECURE === "true",
|
||||
secure: process.env.SMTP_SECURE?.trim().toLowerCase() === "true",
|
||||
from,
|
||||
auth: user && pass ? { user, pass } : null,
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue