import { NextResponse, type NextRequest } from "next/server"; import { asSystem } from "@/lib/db"; import { callFunction } from "@/lib/db/rpc"; // Applies effective-dated changes (Versetzung/Beförderung/Karenz/Reorg/Daten // ändern with a future "Wirksam ab" date) once their date has arrived — see // apply_due_pending_changes() in supabase/migrations. Runs as a Vercel Cron // job (see vercel.json), not on behalf of any HR user, so it authenticates // via a shared secret rather than a Supabase session and uses the // service-role client (the one legitimate server-only use case for it). export async function GET(request: NextRequest) { const authHeader = request.headers.get("authorization"); if (!process.env.CRON_SECRET || authHeader !== `Bearer ${process.env.CRON_SECRET}`) { return NextResponse.json({ error: "Nicht autorisiert." }, { status: 401 }); } // Kein privilegierter Zugang mehr: derselbe Datenbankbenutzer ohne // BYPASSRLS wie überall. apply_due_pending_changes ist SECURITY DEFINER // und prüft selbst, was sie tut — der Dienstschlüssel, der RLS aushebelte, // ist damit entfallen. try { const applied = await asSystem((tx) => callFunction(tx, "apply_due_pending_changes")); return NextResponse.json({ applied }); } catch (err) { console.error("apply_due_pending_changes failed:", err); return NextResponse.json({ error: "Interner Fehler." }, { status: 500 }); } }