Skip to content

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

feat: add contact id to user table

Marc Seitz6369555c4b27
diff --git a/lib/unsend.ts b/lib/unsend.ts
new file mode 100644
index 00000000..b7510868
--- /dev/null
+++ b/lib/unsend.ts
@@ -0,0 +1,61 @@
+import { Unsend } from "unsend";
+
+import prisma from "@/lib/prisma";
+
+const unsend = new Unsend(
+ process.env.UNSEND_API_KEY,
+ process.env.UNSEND_BASE_URL,
+);
+
+const contactBookId = process.env.UNSEND_CONTACT_BOOK_ID as string;
+
+const subscribe = async (email: string) => {
+ if (!contactBookId) {
+ console.error("UNSEND_CONTACT_BOOK_ID is not set in the .env. Skipping.");
+ return;
+ }
+
+ const contactId = await unsend.contacts.create(contactBookId, {
+ email,
+ });
+
+ if (!contactId.data?.contactId) {
+ throw new Error("Failed to add user to contact book");
+ }
+
+ await prisma.user.update({
+ where: {
+ email,
+ },
+ data: {
+ contactId: contactId.data.contactId,
+ },
+ });
+};
+
+const unsubscribe = async (email: string): Promise<void> => {
+ if (!contactBookId) {
+ console.error("UNSEND_CONTACT_BOOK_ID is not set in the .env. Skipping.");
+ return;
+ }
+
+ if (!email || email === "") {
+ return;
+ }
+
+ const user = await prisma.user.findUnique({
+ where: { email },
+ select: { contactId: true },
+ });
+
+ if (!user?.contactId) {
+ return;
+ }
+
+ await unsend.contacts.update(contactBookId, user.contactId, {
+ subscribed: false,
+ });
+};
+
+export default unsend;
+export { subscribe, unsubscribe };
diff --git a/package-lock.json b/package-lock.json
index eebfa744..ccb52b3d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -44,15 +44,15 @@
"@radix-ui/react-toggle": "^1.1.2",
"@radix-ui/react-toggle-group": "^1.1.2",
"@radix-ui/react-tooltip": "^1.1.8",
- "@react-email/components": "^0.0.32",
+ "@react-email/components": "^0.0.33",
"@sindresorhus/slugify": "^2.2.1",
"@stripe/stripe-js": "^4.10.0",
"@tailwindcss/typography": "^0.5.16",
"@tanstack/react-table": "^8.21.2",
"@teamhanko/passkeys-next-auth-provider": "^0.2.7",
"@tremor/react": "^3.18.7",
- "@trigger.dev/react-hooks": "3.3.16",
- "@trigger.dev/sdk": "3.3.16",
+ "@trigger.dev/react-hooks": "^3.3.16",
+ "@trigger.dev/sdk": "^3.3.16",
"@tus/s3-store": "^1.9.0",
"@tus/server": "^1.10.2",
"@tus/utils": "^0.5.1",
@@ -120,6 +120,7 @@
"ts-pattern": "^5.6.2",
"tus-js-client": "^4.3.1",
"ua-parser-js": "^1.0.40",
+ "unsend": "^1.3.0",
"use-debounce": "^10.0.4",
"vaul": "^1.1.2",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz",
@@ -127,7 +128,7 @@
},
"devDependencies": {
"@tailwindcss/forms": "^0.5.10",
- "@trigger.dev/build": "3.3.16",
+ "@trigger.dev/build": "^3.3.16",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/bcryptjs": "^2.4.6",
"@types/fluent-ffmpeg": "^2.1.27",
@@ -5296,9 +5297,9 @@
}
},
"node_modules/@react-email/components": {
- "version": "0.0.32",
- "resolved": "https://registry.npmjs.org/@react-email/components/-/components-0.0.32.tgz",
- "integrity": "sha512-+1Wv7PyVgWfLoj5W0+CvBsJMIfMI6ibcFcIPXNkb2lhKQQASgxSoAedRL1rH0CCaBo6+63tg8y4baHzJonfZbw==",
+ "version": "0.0.33",
+ "resolved": "https://registry.npmjs.org/@react-email/components/-/components-0.0.33.tgz",
+ "integrity": "sha512-/GKdT3YijT1iEWPAXF644jr12w5xVgzUr0zlbZGt2KOkGeFHNZUCL5UtRopmnjrH/Fayf8Gjv6q/4E2cZgDtdQ==",
"dependencies": {
"@react-email/body": "0.0.11",
"@react-email/button": "0.0.19",
@@ -5315,7 +5316,7 @@
"@react-email/link": "0.0.12",
"@react-email/markdown": "0.0.14",
"@react-email/preview": "0.0.12",
- "@react-email/render": "1.0.4",
+ "@react-email/render": "1.0.5",
"@react-email/row": "0.0.12",
"@react-email/section": "0.0.16",
"@react-email/tailwind": "1.0.4",
@@ -5329,9 +5330,9 @@
}
},
"node_modules/@react-email/components/node_modules/@react-email/render": {
- "version": "1.0.4",
- "resolved": "https://registry.npmjs.org/@react-email/render/-/render-1.0.4.tgz",
- "integrity": "sha512-8ZXi89d8igBDE6W3zlHBa3GEDWKEUFDAa7i8MvVxnRViQuvsRbibK3ltuPgixxRI5+HgGNCSreBHQKZCkhUdyw==",
+ "version": "1.0.5",
+ "resolved": "https://registry.npmjs.org/@react-email/render/-/render-1.0.5.tgz",
+ "integrity": "sha512-CA69HYXPk21HhtAXATIr+9JJwpDNmAFCvdMUjWmeoD1+KhJ9NAxusMRxKNeibdZdslmq3edaeOKGbdQ9qjK8LQ==",
"dependencies": {
"html-to-text": "9.0.5",
"prettier": "3.4.2",
@@ -16330,6 +16331,30 @@
"resolved": "https://registry.npmjs.org/unionize/-/unionize-2.2.0.tgz",
"integrity": "sha512-lHXiL6LPVuRYBGCLOdUd4GMHoAGqM0HtYHAZcA6pUEiwN1nk+LEYlh8bud7saeL0bkFntJzCPEPVVJeFm3Cqsg=="
},
+ "node_modules/unsend": {
+ "version": "1.3.0",
+ "resolved": "https://registry.npmjs.org/unsend/-/unsend-1.3.0.tgz",
+ "integrity": "sha512-gIHryNcZTkQcOWYxqpuJwtJ7/wWxPbit/wuDjKa27LN21KlFs4uaCQ/w+uP4jDcdCgbW9P1zvOv8akzyYyLCGA==",
+ "dependencies": {
+ "@react-email/render": "^0.0.14",
+ "react": "^18.3.1"
+ }
+ },
+ "node_modules/unsend/node_modules/@react-email/render": {
+ "version": "0.0.14",
+ "resolved": "https://registry.npmjs.org/@react-email/render/-/render-0.0.14.tgz",
+ "integrity": "sha512-vZD59BHp1HOatJAWydTOdgN8QbNgsjCOfmPcT9ShwisBIu3Rw8pvUVoWsrAqfHfzc4vcNTGnbt6/8mMa8LcsvQ==",
+ "dependencies": {
+ "html-to-text": "9.0.5",
+ "js-beautify": "^1.14.11",
+ "react": "^18.2.0",
+ "react-dom": "^18.2.0",
+ "react-promise-suspense": "0.3.4"
+ },
+ "engines": {
+ "node": ">=18.0.0"
+ }
+ },
"node_modules/update-browserslist-db": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.1.0.tgz",
@@ -16989,6 +17014,21 @@
"peerDependencies": {
"zod": "^3.18.0"
}
+ },
+ "node_modules/react-email/node_modules/@next/swc-win32-ia32-msvc": {
+ "version": "14.2.24",
+ "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.24.tgz",
+ "integrity": "sha512-9KuS+XUXM3T6v7leeWU0erpJ6NsFIwiTFD5nzNg8J5uo/DMIPvCp3L1Ao5HjbHX0gkWPB1VrKoo/Il4F0cGK2Q==",
+ "cpu": [
+ "ia32"
+ ],
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">= 10"
+ }
}
}
}
diff --git a/package.json b/package.json
index ff271130..10ba73d1 100644
--- a/package.json
+++ b/package.json
@@ -55,15 +55,15 @@
"@radix-ui/react-toggle": "^1.1.2",
"@radix-ui/react-toggle-group": "^1.1.2",
"@radix-ui/react-tooltip": "^1.1.8",
- "@react-email/components": "^0.0.32",
+ "@react-email/components": "^0.0.33",
"@sindresorhus/slugify": "^2.2.1",
"@stripe/stripe-js": "^4.10.0",
"@tailwindcss/typography": "^0.5.16",
"@tanstack/react-table": "^8.21.2",
"@teamhanko/passkeys-next-auth-provider": "^0.2.7",
"@tremor/react": "^3.18.7",
- "@trigger.dev/react-hooks": "3.3.16",
- "@trigger.dev/sdk": "3.3.16",
+ "@trigger.dev/react-hooks": "^3.3.16",
+ "@trigger.dev/sdk": "^3.3.16",
"@tus/s3-store": "^1.9.0",
"@tus/server": "^1.10.2",
"@tus/utils": "^0.5.1",
@@ -131,6 +131,7 @@
"ts-pattern": "^5.6.2",
"tus-js-client": "^4.3.1",
"ua-parser-js": "^1.0.40",
+ "unsend": "^1.3.0",
"use-debounce": "^10.0.4",
"vaul": "^1.1.2",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz",
@@ -138,7 +139,7 @@
},
"devDependencies": {
"@tailwindcss/forms": "^0.5.10",
- "@trigger.dev/build": "3.3.16",
+ "@trigger.dev/build": "^3.3.16",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/bcryptjs": "^2.4.6",
"@types/fluent-ffmpeg": "^2.1.27",
@@ -161,4 +162,4 @@
"react-pdf": "8.0.2"
}
}
-}
\ No newline at end of file
+}
diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts
index 93738099..3647f1e4 100644
--- a/pages/api/auth/[...nextauth].ts
+++ b/pages/api/auth/[...nextauth].ts
@@ -11,6 +11,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 { generateChecksum } from "@/lib/utils/generate-checksum";
const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;
@@ -129,6 +130,10 @@ export const authOptions: NextAuthOptions = {
});
await sendWelcomeEmail(params);
+
+ if (message.user.email) {
+ await subscribe(message.user.email);
+ }
},
async signIn(message) {
await identifyUser(message.user.email ?? message.user.id);
diff --git a/pages/api/teams/[teamId]/index.ts b/pages/api/teams/[teamId]/index.ts
index 8c31af7c..a5fdb32b 100644
--- a/pages/api/teams/[teamId]/index.ts
+++ b/pages/api/teams/[teamId]/index.ts
@@ -10,6 +10,7 @@ import prisma from "@/lib/prisma";
import { cancelSubscription } from "@/lib/stripe";
import { isOldAccount } from "@/lib/stripe/utils";
import { CustomUser } from "@/lib/types";
+import { unsubscribe } from "@/lib/unsend";
import { authOptions } from "../../auth/[...nextauth]";
@@ -215,6 +216,8 @@ export default async function handle(
// delete subscription, if exists on team
team.stripeId &&
cancelSubscription(team.stripeId, isOldAccount(team.plan)),
+ // delete user from contact book
+ unsubscribe((session.user as CustomUser).email ?? ""),
// delete user, if no other teams
userTeams.length === 1 &&
prisma.user.delete({
diff --git a/prisma/migrations/20250217000000_add_contactid_to_user/migration.sql b/prisma/migrations/20250217000000_add_contactid_to_user/migration.sql
new file mode 100644
index 00000000..8f2e24df
--- /dev/null
+++ b/prisma/migrations/20250217000000_add_contactid_to_user/migration.sql
@@ -0,0 +1,3 @@
+-- AlterTable
+ALTER TABLE "User" ADD COLUMN "contactId" TEXT;
+
diff --git a/prisma/migrations/20250217000000_remove_link_column/migration.sql b/prisma/migrations/20250217000000_remove_link_column/migration.sql
new file mode 100644
index 00000000..7f2bbb85
--- /dev/null
+++ b/prisma/migrations/20250217000000_remove_link_column/migration.sql
@@ -0,0 +1,3 @@
+-- AlterTable
+ALTER TABLE "Link" DROP COLUMN "screenShieldPercentage";
+
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 2bc0d588..6f92e41a 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -50,6 +50,7 @@ model User {
teams UserTeam[]
domains Domain[]
conversations Conversation[]
+ contactId String?
plan String @default("free")
stripeId String? @unique // Stripe subscription / customer ID
subscriptionId String? @unique // Stripe subscription ID
@@ -255,7 +256,6 @@ model Link {
enableFeedback Boolean? @default(false) // Optional give user a option to enable the reactions toolbar
enableQuestion Boolean? @default(false) // Optional give user a option to enable the question feedback
enableScreenshotProtection Boolean? @default(false) // Optional give user a option to enable the screenshot protection
- screenShieldPercentage Int? // Optional give user a option to enable the screen shield percentage
feedback Feedback?
enableAgreement Boolean? @default(false) // Optional give user a option to enable the terms and conditions
agreement Agreement? @relation(fields: [agreementId], references: [id], onDelete: SetNull)