Skip to content

Commit

Permalink
fix: rewrite cron to interface like a function rather than a webhook (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfrancisco authored Jan 15, 2025
1 parent f05f6fb commit a9560c9
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions apps/engine/src/app/api/email/cron/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down Expand Up @@ -57,10 +46,5 @@ export async function POST(request: NextRequest) {
})
);

return NextResponse.json(
{},
{
status: 200,
}
);
return Response.json({ success: true });
}

0 comments on commit a9560c9

Please sign in to comment.