1
0
Fork 0
fahrPool/web/app/api/notifications/dispatch/route.ts

39 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Response> {
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);
}