Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add resend subscriptions

Marc Seitz9976575db830
diff --git a/app/(auth)/auth/confirm-email-change/[token]/page.tsx b/app/(auth)/auth/confirm-email-change/[token]/page.tsx
index 431f6f25..aa97d6c3 100644
--- a/app/(auth)/auth/confirm-email-change/[token]/page.tsx
+++ b/app/(auth)/auth/confirm-email-change/[token]/page.tsx
@@ -8,9 +8,8 @@ import { waitUntil } from "@vercel/functions";
import { hashToken } from "@/lib/api/auth/token";
import prisma from "@/lib/prisma";
import { redis } from "@/lib/redis";
-import { sendEmail } from "@/lib/resend";
+import { sendEmail, subscribe, unsubscribe } from "@/lib/resend";
import { CustomUser } from "@/lib/types";
-import { subscribe, unsubscribe } from "@/lib/unsend";
import EmailUpdated from "@/components/emails/email-updated";
diff --git a/lib/resend.ts b/lib/resend.ts
index 3e40899a..80adc85e 100644
--- a/lib/resend.ts
+++ b/lib/resend.ts
@@ -3,6 +3,7 @@ import { JSXElementConstructor, ReactElement } from "react";
import { render, toPlainText } from "@react-email/render";
import { Resend } from "resend";
+import prisma from "@/lib/prisma";
import { log, nanoid } from "@/lib/utils";
export const resend = process.env.RESEND_API_KEY
@@ -94,3 +95,53 @@ export const sendEmail = async ({
throw exception; // Rethrow the caught exception
}
};
+
+export const subscribe = async (email: string): Promise<void> => {
+ if (!resend) {
+ console.error("RESEND_API_KEY is not set in the .env. Skipping.");
+ return;
+ }
+
+ const { data, error } = await resend.contacts.create({
+ email,
+ });
+
+ if (error || !data?.id) {
+ console.error("Failed to create contact:", error);
+ return;
+ }
+
+ await prisma.user.update({
+ where: {
+ email,
+ },
+ data: {
+ contactId: data.id,
+ },
+ });
+};
+
+export const unsubscribe = async (email: string): Promise<void> => {
+ if (!resend) {
+ console.error("RESEND_API_KEY is not set in the .env. Skipping.");
+ return;
+ }
+
+ if (!email || email === "") {
+ return;
+ }
+
+ const user = await prisma.user.findUnique({
+ where: { email },
+ select: { contactId: true, email: true },
+ });
+
+ if (!user?.contactId || !user.email) {
+ return;
+ }
+
+ await resend.contacts.update({
+ email: user.email,
+ unsubscribed: true,
+ });
+};
diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts
index 86108846..55cbdfd4 100644
--- a/pages/api/auth/[...nextauth].ts
+++ b/pages/api/auth/[...nextauth].ts
@@ -16,7 +16,7 @@ import { sendWelcomeEmail } from "@/lib/emails/send-welcome";
import hanko from "@/lib/hanko";
import prisma from "@/lib/prisma";
import { CreateUserEmailProps, CustomUser } from "@/lib/types";
-import { subscribe } from "@/lib/unsend";
+import { subscribe } from "@/lib/resend";
import { log } from "@/lib/utils";
import { generateChecksum } from "@/lib/utils/generate-checksum";
import { getIpAddress } from "@/lib/utils/ip";
diff --git a/pages/api/teams/[teamId]/index.ts b/pages/api/teams/[teamId]/index.ts
index b87ed1dd..44f1f41c 100644
--- a/pages/api/teams/[teamId]/index.ts
+++ b/pages/api/teams/[teamId]/index.ts
@@ -12,7 +12,7 @@ import prisma from "@/lib/prisma";
import { redis } from "@/lib/redis";
import { CustomUser } from "@/lib/types";
-import { unsubscribe } from "@/lib/unsend";
+import { unsubscribe } from "@/lib/resend";
import { authOptions } from "../../auth/[...nextauth]";