From b5b1ab2a970fc684917b511c8fdb600da4096a90 Mon Sep 17 00:00:00 2001 From: Tomas Francisco <4301103+tomasfrancisco@users.noreply.github.com> Date: Wed, 15 Jan 2025 13:28:58 +0100 Subject: [PATCH] fix: rewrite cron to interface like a function rather than a webhook --- apps/engine/src/app/api/email/cron/route.ts | 28 +++++---------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/apps/engine/src/app/api/email/cron/route.ts b/apps/engine/src/app/api/email/cron/route.ts index 16c6f09..19167b5 100644 --- a/apps/engine/src/app/api/email/cron/route.ts +++ b/apps/engine/src/app/api/email/cron/route.ts @@ -2,30 +2,19 @@ import { serverEnv } from '@/env/server-env'; import { api } from '@ds-project/api/service'; import { sendEmail } from '@ds-project/email'; import type { NextRequest } from 'next/server'; -import { NextResponse } from 'next/server'; -import { Webhook } from 'standardwebhooks'; /** * Checks the scheduled emails in the database and sends the emails that are due */ export async function POST(request: NextRequest) { - const wh = new Webhook(serverEnv.CRON_SECRET); - const payload = await request.text(); - const headers = Object.fromEntries(request.headers); - + const authHeader = request.headers.get('authorization'); // Verify the request is coming from an authorized source - try { - wh.verify(payload, headers); - } catch (error) { - return NextResponse.json( - { error }, - { - status: 401, - } - ); + if (authHeader !== `Bearer ${serverEnv.CRON_SECRET}`) { + return new Response('Unauthorized', { + status: 401, + }); } - const dueEmailJobs = await api.jobs.getDueEmailList(); console.log(`👀 ${dueEmailJobs.length} due email jobs found.`); @@ -57,10 +46,5 @@ export async function POST(request: NextRequest) { }) ); - return NextResponse.json( - {}, - { - status: 200, - } - ); + return Response.json({ success: true }); }