import { NextResponse } from "next/server"; import { timingSafeEqual } from "crypto"; import { createAdminClient } from "@/lib/supabase/admin"; import { getWhatsAppConfig } from "@/lib/notifications/config"; import { verarbeiteOutbox } from "@/lib/notifications/dispatch"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; function sichererVergleich(a: string, b: string): boolean { const bufA = Buffer.from(a); const bufB = Buffer.from(b); if (bufA.length !== bufB.length) return false; return timingSafeEqual(bufA, bufB); } export async function POST(request: Request): Promise { const secret = process.env.NOTIFICATION_DISPATCH_SECRET; if (!secret) { console.error("[notifications] NOTIFICATION_DISPATCH_SECRET nicht gesetzt"); return new NextResponse("Fehlkonfiguration", { status: 500 }); } const auth = request.headers.get("authorization") ?? ""; const token = auth.startsWith("Bearer ") ? auth.slice(7) : ""; if (!token || !sichererVergleich(token, secret)) { return new NextResponse("Unauthorized", { status: 401 }); } const cfg = getWhatsAppConfig(); if (!cfg) { console.warn("[notifications] WhatsApp nicht konfiguriert – Outbox wird nicht verarbeitet"); return NextResponse.json({ verarbeitet: 0, uebersprungen: true }); } const ergebnis = await verarbeiteOutbox(createAdminClient(), cfg); return NextResponse.json(ergebnis); }