diff --git a/app/api/familyguard-flyer/route.ts b/app/api/familyguard-flyer/route.ts new file mode 100644 index 0000000..9cc48fd --- /dev/null +++ b/app/api/familyguard-flyer/route.ts @@ -0,0 +1,46 @@ +import { NextResponse } from "next/server"; +import { sendeFlyerBenachrichtigung, sendeFlyerLink } from "@/lib/mailer"; +import { createServiceClient } from "@/lib/supabase"; + +const FLYER_DOWNLOAD_URL = "/downloads/MBO_FamilyGuard_Flyer_01.pdf"; + +function isValidEmail(email: string): boolean { + return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); +} + +export async function POST(request: Request) { + let body: { email?: string; dsgvoEinwilligung?: boolean }; + try { + body = await request.json(); + } catch { + return NextResponse.json({ ok: false, error: "Ungültige Anfrage" }, { status: 400 }); + } + + const email = body.email?.trim() ?? ""; + if (!isValidEmail(email)) { + return NextResponse.json( + { ok: false, error: "Bitte geben Sie eine gültige E-Mail-Adresse ein" }, + { status: 400 } + ); + } + if (body.dsgvoEinwilligung !== true) { + return NextResponse.json( + { ok: false, error: "Bitte stimmen Sie der Datenverarbeitung zu" }, + { status: 400 } + ); + } + + try { + const db = createServiceClient(); + await db.from("flyer_downloads").insert({ email, dsgvo_einwilligung: true }); + } catch (err) { + console.error("[FamilyGuard-Flyer] Supabase insert error:", err); + } + + await sendeFlyerBenachrichtigung({ email }); + sendeFlyerLink({ email }).catch((err) => + console.error("[FamilyGuard-Flyer] Flyer-Mail an Interessenten fehlgeschlagen:", err) + ); + + return NextResponse.json({ ok: true, downloadUrl: FLYER_DOWNLOAD_URL }); +}