Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

offload notify to qstash

Steven Tey31691ffbddd5
diff --git a/apps/web/app/api/cron/notify/route.tsx b/apps/web/app/api/cron/notify/route.tsx
new file mode 100644
index 0000000000..324d9be8ab
--- /dev/null
+++ b/apps/web/app/api/cron/notify/route.tsx
@@ -0,0 +1,31 @@
+import { receiver } from "@/lib/cron";
+import { sendEmail } from "emails";
+import APIKeyCreated from "emails/api-key-created";
+
+export async function POST(req: Request) {
+ const body = await req.json();
+ if (process.env.VERCEL === "1") {
+ const isValid = await receiver.verify({
+ signature: req.headers.get("Upstash-Signature") || "",
+ body: JSON.stringify(body),
+ });
+ if (!isValid) {
+ return new Response("Unauthorized", { status: 401 });
+ }
+ }
+
+ const { type, props } = body;
+
+ if (type === "API_KEY_CREATED") {
+ await sendEmail({
+ email: props.email,
+ subject: "New API Key Created",
+ react: APIKeyCreated({
+ email: props.email,
+ apiKeyName: props.apiKeyName,
+ }),
+ });
+ }
+
+ return new Response("Notification sent.", { status: 200 });
+}
diff --git a/apps/web/app/api/user/tokens/route.ts b/apps/web/app/api/user/tokens/route.ts
index efaeb71b19..b923de0a2d 100644
--- a/apps/web/app/api/user/tokens/route.ts
+++ b/apps/web/app/api/user/tokens/route.ts
@@ -1,6 +1,7 @@
import { hashToken, withSession } from "@/lib/auth";
+import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
-import { nanoid } from "@dub/utils";
+import { APP_DOMAIN_WITH_NGROK, nanoid } from "@dub/utils";
import { sendEmail } from "emails";
import APIKeyCreated from "emails/api-key-created";
import { NextResponse } from "next/server";
@@ -48,13 +49,15 @@ export const POST = withSession(async ({ req, session }) => {
userId: session.user.id,
},
}),
- sendEmail({
- email: session.user.email,
- subject: "New API Key Created",
- react: APIKeyCreated({
- email: session.user.email,
- apiKeyName: name,
- }),
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/notify`,
+ body: {
+ type: "API_KEY_CREATED",
+ props: {
+ email: session.user.email,
+ apiKeyName: name,
+ },
+ },
}),
]);
return NextResponse.json({ token });