34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { runReminderSweep, verifyCronBearer } from "@/lib/cron";
|
|
import { safeError } from "@/lib/errors";
|
|
|
|
/**
|
|
* POST /api/cron/send-reminders
|
|
*
|
|
* Machine entry point for the daily reminder sweep. Same auth
|
|
* (bearer token in CRON_BEARER_TOKEN) and the same response
|
|
* contract as /api/cron/issue-monthly.
|
|
*
|
|
* Schedule: 09:00 Europe/Zurich daily. Picks invoices that are
|
|
* past their due date and haven't received the corresponding
|
|
* reminder level yet; sends one email per invoice per run.
|
|
*/
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function POST(request: Request) {
|
|
if (!verifyCronBearer(request.headers.get("authorization"))) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
try {
|
|
const { runId, summary } = await runReminderSweep({
|
|
triggeredBy: "cron",
|
|
});
|
|
return NextResponse.json({ runId, ...summary });
|
|
} catch (e) {
|
|
return NextResponse.json(
|
|
{ error: safeError(e, "Reminder sweep failed.") },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|