Use message queue for emails (#1446)

This commit is contained in:
Luke Vella 2024-11-30 19:00:56 +00:00 committed by GitHub
parent 673fc79801
commit a452e5b764
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 150 additions and 35 deletions

View file

@ -42,6 +42,7 @@
"@trpc/next": "^10.13.0",
"@trpc/react-query": "^10.13.0",
"@trpc/server": "^10.13.0",
"@upstash/qstash": "^2.7.17",
"@upstash/ratelimit": "^1.2.1",
"@vercel/functions": "^1.0.2",
"@vercel/kv": "^2.0.0",

View file

@ -0,0 +1,33 @@
import * as Sentry from "@sentry/nextjs";
import { verifySignatureAppRouter } from "@upstash/qstash/nextjs";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { getEmailClient } from "@/utils/emails";
const emailClient = getEmailClient();
export const POST = async (req: NextRequest) => {
/**
* We need to call verifySignatureAppRouter inside the route handler
* to avoid the build breaking when env vars are not set.
*/
return verifySignatureAppRouter(async (req: NextRequest) => {
const body = await req.json();
// TODO: Add validation for templateName and options
try {
await emailClient.sendTemplate(body.templateName, body.options);
return NextResponse.json({ success: true });
} catch (error) {
Sentry.captureException(error);
return NextResponse.json(
{ success: false, error: "Failed to send email" },
{ status: 500 },
);
}
})(req);
};