Files
Calbook/app/api/cron/sync/route.ts

33 lines
1.0 KiB
TypeScript

export const dynamic = "force-dynamic";
import crypto from "crypto";
import { fail, ok } from "@/lib/api";
import { syncAllEnabledCalendars } from "@/lib/services/caldav";
import { runAppointmentReminders } from "@/lib/services/reminders";
function safeSecretMatch(expected: string, provided?: string | null) {
if (!provided) return false;
const expectedBytes = Buffer.from(expected, "utf8");
const providedBytes = Buffer.from(provided, "utf8");
if (expectedBytes.length !== providedBytes.length) return false;
return crypto.timingSafeEqual(expectedBytes, providedBytes);
}
export async function POST(req: Request) {
const secret = process.env.CRON_SECRET;
if (!secret) {
return fail("CRON_SECRET ist nicht konfiguriert", 503);
}
const provided = req.headers.get("x-cron-secret");
if (!safeSecretMatch(secret, provided)) {
return fail("Nicht erlaubt", 403);
}
const [results, reminders] = await Promise.all([
syncAllEnabledCalendars(),
runAppointmentReminders()
]);
return ok({ results, reminders });
}