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.

Finalize resend package

Steven Tey136c3b8fa40b
diff --git a/apps/web/app/(ee)/api/cron/welcome-user/route.ts b/apps/web/app/(ee)/api/cron/welcome-user/route.ts
index 65718e3ce1..d9e9677245 100644
--- a/apps/web/app/(ee)/api/cron/welcome-user/route.ts
+++ b/apps/web/app/(ee)/api/cron/welcome-user/route.ts
@@ -26,7 +26,6 @@ export async function POST(req: Request) {
name: true,
email: true,
partners: true,
- projects: true,
},
});
@@ -40,33 +39,26 @@ export async function POST(req: Request) {
}
const isPartner = user.partners.length > 0;
- const isWorkspaceOwner = user.projects.length > 0;
await Promise.all([
- isPartner &&
- subscribe({
- email: user.email,
- name: user.name || undefined,
- audience: "partners.dub.co",
- }),
- isWorkspaceOwner &&
- subscribe({
- email: user.email,
- name: user.name || undefined,
- audience: "app.dub.co",
- }),
+ subscribe({
+ email: user.email,
+ name: user.name || undefined,
+ audience: isPartner ? "partners.dub.co" : "app.dub.co",
+ }),
// TODO: Add partners.dub.co welcome email
- isWorkspaceOwner &&
- sendEmail({
- email: user.email,
- replyTo: "[email protected]",
- subject: "Welcome to Dub!",
- react: WelcomeEmail({
+ isPartner
+ ? undefined
+ : sendEmail({
email: user.email,
- name: user.name,
+ replyTo: "[email protected]",
+ subject: "Welcome to Dub!",
+ react: WelcomeEmail({
+ email: user.email,
+ name: user.name,
+ }),
+ variant: "marketing",
}),
- variant: "marketing",
- }),
]);
return new Response("Welcome email sent and user subscribed.", {
diff --git a/apps/web/scripts/update-subscribers.ts b/apps/web/scripts/update-subscribers.ts
index bd0c323136..b77b0be179 100644
--- a/apps/web/scripts/update-subscribers.ts
+++ b/apps/web/scripts/update-subscribers.ts
@@ -1,6 +1,7 @@
+import { RESEND_AUDIENCES } from "@dub/email/resend/constants";
import { prisma } from "@dub/prisma";
-// import { resend, RESEND_AUDIENCES } from "@dub/email/resend";
import "dotenv-flow/config";
+import { Resend } from "resend";
async function main() {
const partnerUsers = await prisma.user.findMany({
@@ -12,9 +13,68 @@ async function main() {
none: {},
},
},
+ orderBy: {
+ createdAt: "asc",
+ },
+ skip: 100,
+ take: 100,
});
console.log(`Found ${partnerUsers.length} partner users`);
+
+ for (const user of partnerUsers) {
+ if (!user.email) {
+ console.log(`Skipping ${user.id} because they have no email`);
+ continue;
+ }
+
+ await Promise.all([
+ subscribe({
+ email: user.email,
+ name: user.name,
+ audience: "partners.dub.co",
+ }),
+ unsubscribe({
+ email: user.email,
+ }),
+ ]);
+
+ console.log(
+ `Subscribed ${user.email} to partners.dub.co and unsubscribed from app.dub.co`,
+ );
+ }
}
main();
+
+const resend = new Resend(process.env.RESEND_API_KEY);
+
+const subscribe = async ({
+ email,
+ name,
+ audience,
+}: {
+ email: string;
+ name: string | null;
+ audience: keyof typeof RESEND_AUDIENCES;
+}) => {
+ const audienceId = RESEND_AUDIENCES[audience];
+ return await resend.contacts.create({
+ email,
+ ...(name && {
+ firstName: name.split(" ")[0],
+ lastName: name.split(" ").slice(1).join(" "),
+ }),
+ audienceId,
+ });
+};
+
+const unsubscribe = async ({ email }: { email: string }) => {
+ // TODO: Update this to support partners.dub.co in the future
+ const audienceId = RESEND_AUDIENCES["app.dub.co"];
+
+ return await resend.contacts.remove({
+ email,
+ audienceId,
+ });
+};
diff --git a/packages/email/src/index.ts b/packages/email/src/index.ts
index 226041536b..ef035188b2 100644
--- a/packages/email/src/index.ts
+++ b/packages/email/src/index.ts
@@ -1,6 +1,7 @@
-import { resend, sendEmailViaResend } from "./resend";
+import { resend } from "./resend";
import { ResendEmailOptions } from "./resend/types";
import { sendViaNodeMailer } from "./send-via-nodemailer";
+import { sendEmailViaResend } from "./send-via-resend";
export const sendEmail = async (opts: ResendEmailOptions) => {
if (resend) {
diff --git a/packages/email/src/resend/client.ts b/packages/email/src/resend/client.ts
new file mode 100644
index 0000000000..7ed3e93912
--- /dev/null
+++ b/packages/email/src/resend/client.ts
@@ -0,0 +1,5 @@
+import { Resend } from "resend";
+
+export const resend = process.env.RESEND_API_KEY
+ ? new Resend(process.env.RESEND_API_KEY)
+ : null;
diff --git a/packages/email/src/resend/constants.ts b/packages/email/src/resend/constants.ts
new file mode 100644
index 0000000000..b70df0ae88
--- /dev/null
+++ b/packages/email/src/resend/constants.ts
@@ -0,0 +1,10 @@
+export const RESEND_AUDIENCES = {
+ "app.dub.co": "f5ff0661-4234-43f6-b0ca-a3f3682934e3",
+ "partners.dub.co": "6caf6898-941a-45b6-a59f-d0780c3004ac",
+};
+
+export const VARIANT_TO_FROM_MAP = {
+ primary: "Dub.co <[email protected]>",
+ notifications: "Dub.co <[email protected]>",
+ marketing: "Steven from Dub.co <[email protected]>",
+};
diff --git a/packages/email/src/resend/index.ts b/packages/email/src/resend/index.ts
index 62b8ea2332..59449c6ff7 100644
--- a/packages/email/src/resend/index.ts
+++ b/packages/email/src/resend/index.ts
@@ -1,55 +1,3 @@
-import { Resend } from "resend";
-import { ResendEmailOptions } from "./types";
-
-export const resend = process.env.RESEND_API_KEY
- ? new Resend(process.env.RESEND_API_KEY)
- : null;
-
-const VARIANT_TO_FROM_MAP = {
- primary: "Dub.co <[email protected]>",
- notifications: "Dub.co <[email protected]>",
- marketing: "Steven from Dub.co <[email protected]>",
-};
-
-// Send email using Resend (Recommended for production)
-export const sendEmailViaResend = async (opts: ResendEmailOptions) => {
- if (!resend) {
- console.info(
- "RESEND_API_KEY is not set in the .env. Skipping sending email.",
- );
- return;
- }
-
- const {
- email,
- from,
- variant = "primary",
- bcc,
- replyTo,
- subject,
- text,
- react,
- scheduledAt,
- } = opts;
-
- return await resend.emails.send({
- to: email,
- from: from || VARIANT_TO_FROM_MAP[variant],
- bcc: bcc,
- replyTo: replyTo || "[email protected]",
- subject,
- text,
- react,
- scheduledAt,
- ...(variant === "marketing" && {
- headers: {
- "List-Unsubscribe": "https://app.dub.co/account/settings",
- },
- }),
- });
-};
-
-export const RESEND_AUDIENCES = {
- "app.dub.co": "f5ff0661-4234-43f6-b0ca-a3f3682934e3",
- "partners.dub.co": "6caf6898-941a-45b6-a59f-d0780c3004ac",
-};
+export * from "./client";
+export * from "./subscribe";
+export * from "./unsubscribe";
diff --git a/packages/email/src/resend/subscribe.ts b/packages/email/src/resend/subscribe.ts
index d06320198b..177e8622e8 100644
--- a/packages/email/src/resend/subscribe.ts
+++ b/packages/email/src/resend/subscribe.ts
@@ -1,4 +1,5 @@
-import { resend, RESEND_AUDIENCES } from ".";
+import { resend } from "./client";
+import { RESEND_AUDIENCES } from "./constants";
export async function subscribe({
email,
@@ -10,7 +11,9 @@ export async function subscribe({
audience?: keyof typeof RESEND_AUDIENCES;
}) {
if (!resend) {
- console.error("Resend client is not initialized. This may be due to a missing or invalid RESEND_API_KEY in the .env file. Skipping.");
+ console.error(
+ "Resend client is not initialized. This may be due to a missing or invalid RESEND_API_KEY in the .env file. Skipping.",
+ );
return;
}
diff --git a/packages/email/src/resend/unsubscribe.ts b/packages/email/src/resend/unsubscribe.ts
index 48a63e5bce..8a351e9d3c 100644
--- a/packages/email/src/resend/unsubscribe.ts
+++ b/packages/email/src/resend/unsubscribe.ts
@@ -1,8 +1,11 @@
-import { resend, RESEND_AUDIENCES } from ".";
+import { resend } from "./client";
+import { RESEND_AUDIENCES } from "./constants";
export async function unsubscribe({ email }: { email: string }) {
if (!resend) {
- console.error("Resend client is not properly initialized. Skipping operation.");
+ console.error(
+ "Resend client is not properly initialized. Skipping operation.",
+ );
return;
}
diff --git a/packages/email/src/send-via-resend.ts b/packages/email/src/send-via-resend.ts
new file mode 100644
index 0000000000..22e222c9af
--- /dev/null
+++ b/packages/email/src/send-via-resend.ts
@@ -0,0 +1,41 @@
+import { resend } from "./resend";
+import { VARIANT_TO_FROM_MAP } from "./resend/constants";
+import { ResendEmailOptions } from "./resend/types";
+
+// Send email using Resend (Recommended for production)
+export const sendEmailViaResend = async (opts: ResendEmailOptions) => {
+ if (!resend) {
+ console.info(
+ "RESEND_API_KEY is not set in the .env. Skipping sending email.",
+ );
+ return;
+ }
+
+ const {
+ email,
+ from,
+ variant = "primary",
+ bcc,
+ replyTo,
+ subject,
+ text,
+ react,
+ scheduledAt,
+ } = opts;
+
+ return await resend.emails.send({
+ to: email,
+ from: from || VARIANT_TO_FROM_MAP[variant],
+ bcc: bcc,
+ replyTo: replyTo || "[email protected]",
+ subject,
+ text,
+ react,
+ scheduledAt,
+ ...(variant === "marketing" && {
+ headers: {
+ "List-Unsubscribe": "https://app.dub.co/account/settings",
+ },
+ }),
+ });
+};