import { NextResponse, type NextRequest } from "next/server"; import { createAdminClient } from "@/lib/supabase/admin"; // 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 }); } const supabase = createAdminClient(); const { data, error } = await supabase.rpc("apply_due_pending_changes"); if (error) { console.error("apply_due_pending_changes failed:", error); return NextResponse.json({ error: "Interner Fehler." }, { status: 500 }); } return NextResponse.json({ applied: data }); }