mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-23 11:17:26 +02:00
✨ Support locale in message queue (#1447)
This commit is contained in:
parent
a452e5b764
commit
7c5f4e3692
4 changed files with 31 additions and 24 deletions
|
@ -1,33 +1,28 @@
|
||||||
import * as Sentry from "@sentry/nextjs";
|
import * as Sentry from "@sentry/nextjs";
|
||||||
import { verifySignatureAppRouter } from "@upstash/qstash/nextjs";
|
|
||||||
import type { NextRequest } from "next/server";
|
import type { NextRequest } from "next/server";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
import { getEmailClient } from "@/utils/emails";
|
import { getEmailClient } from "@/utils/emails";
|
||||||
|
|
||||||
const emailClient = getEmailClient();
|
import { verifySignature } from "./verify-signature";
|
||||||
|
|
||||||
export const POST = async (req: NextRequest) => {
|
export const POST = verifySignature(async (req: NextRequest) => {
|
||||||
/**
|
const body = await req.json();
|
||||||
* 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
|
// TODO: Add validation for templateName and options
|
||||||
|
|
||||||
try {
|
const emailClient = getEmailClient(body.locale);
|
||||||
await emailClient.sendTemplate(body.templateName, body.options);
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
try {
|
||||||
} catch (error) {
|
await emailClient.sendTemplate(body.templateName, body.options);
|
||||||
Sentry.captureException(error);
|
|
||||||
|
|
||||||
return NextResponse.json(
|
return NextResponse.json({ success: true });
|
||||||
{ success: false, error: "Failed to send email" },
|
} catch (error) {
|
||||||
{ status: 500 },
|
Sentry.captureException(error);
|
||||||
);
|
|
||||||
}
|
return NextResponse.json(
|
||||||
})(req);
|
{ success: false, error: "Failed to send email" },
|
||||||
};
|
{ status: 500 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
12
apps/web/src/app/api/send-email/verify-signature.ts
Normal file
12
apps/web/src/app/api/send-email/verify-signature.ts
Normal 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);
|
||||||
|
}
|
|
@ -5,5 +5,5 @@ export function createQstashClient() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Client({ token: process.env.QSTASH_TOKEN! });
|
return new Client({ token: process.env.QSTASH_TOKEN });
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,7 +83,7 @@ export class EmailClient {
|
||||||
queue
|
queue
|
||||||
.enqueueJSON({
|
.enqueueJSON({
|
||||||
url: absoluteUrl("/api/send-email"),
|
url: absoluteUrl("/api/send-email"),
|
||||||
body: { templateName, options },
|
body: { locale: this.config.locale, templateName, options },
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
Sentry.captureException(new Error("Failed to queue email"));
|
Sentry.captureException(new Error("Failed to queue email"));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue