Support locale in message queue (#1447)

This commit is contained in:
Luke Vella 2024-11-30 19:16:19 +00:00 committed by GitHub
parent a452e5b764
commit 7c5f4e3692
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 31 additions and 24 deletions

View file

@ -1,33 +1,28 @@
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();
import { verifySignature } from "./verify-signature";
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();
export const POST = verifySignature(async (req: NextRequest) => {
const body = await req.json();
// TODO: Add validation for templateName and options
// TODO: Add validation for templateName and options
try {
await emailClient.sendTemplate(body.templateName, body.options);
const emailClient = getEmailClient(body.locale);
return NextResponse.json({ success: true });
} catch (error) {
Sentry.captureException(error);
try {
await emailClient.sendTemplate(body.templateName, body.options);
return NextResponse.json(
{ success: false, error: "Failed to send email" },
{ status: 500 },
);
}
})(req);
};
return NextResponse.json({ success: true });
} catch (error) {
Sentry.captureException(error);
return NextResponse.json(
{ success: false, error: "Failed to send email" },
{ status: 500 },
);
}
});

View file

@ -0,0 +1,12 @@
import { verifySignatureAppRouter } from "@upstash/qstash/dist/nextjs";
import type { NextRequest } from "next/server";
export async function verifySignature(
handler: (req: NextRequest) => Promise<Response>,
) {
/**
* We need to call verifySignatureAppRouter inside the route handler
* to avoid the build breaking when env vars are not set.
*/
return (req: NextRequest) => verifySignatureAppRouter(handler)(req);
}

View file

@ -5,5 +5,5 @@ export function createQstashClient() {
return null;
}
return new Client({ token: process.env.QSTASH_TOKEN! });
return new Client({ token: process.env.QSTASH_TOKEN });
}

View file

@ -83,7 +83,7 @@ export class EmailClient {
queue
.enqueueJSON({
url: absoluteUrl("/api/send-email"),
body: { templateName, options },
body: { locale: this.config.locale, templateName, options },
})
.catch(() => {
Sentry.captureException(new Error("Failed to queue email"));