Skip to content

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

feat: add subscribe toggle

Marc Seitza1770a908807
diff --git a/components/account/update-subscription.tsx b/components/account/update-subscription.tsx
index 10756861..1dbaa5a1 100644
--- a/components/account/update-subscription.tsx
+++ b/components/account/update-subscription.tsx
@@ -1,13 +1,12 @@
-import { useOptimisticUpdate } from "../hooks/use-optimistic-update";
-import { Switch } from "../ui/switch";
+import { useOptimisticUpdate } from "@/components/hooks/use-optimistic-update";
+import { Switch } from "@/components/ui/switch";
-// TODO: MAIL SUBSCRIPTION FOR LATER USE
export const UpdateMailSubscribe = () => {
const { data, isLoading, update } = useOptimisticUpdate<{
subscribed: boolean;
}>("/api/user/subscribe", {
loading: "Updating email preferences...",
- success: `Your ${process.env.NEXT_PUBLIC_BASE_URL} email preferences has been updated!`,
+ success: "Your email preferences have been updated!",
error: "Failed to update email preferences. Please try again.",
});
@@ -25,7 +24,8 @@ export const UpdateMailSubscribe = () => {
return (
<div className="flex items-center gap-x-2">
<Switch
- checked={data?.subscribed}
+ checked={data?.subscribed ?? true}
+ disabled={isLoading}
onCheckedChange={(checked: boolean) => {
update(() => subscribe(checked), { subscribed: checked });
}}
diff --git a/pages/account/general.tsx b/pages/account/general.tsx
index 528fe777..16966f10 100644
--- a/pages/account/general.tsx
+++ b/pages/account/general.tsx
@@ -5,10 +5,11 @@ import { useSession } from "next-auth/react";
import { toast } from "sonner";
import { AccountHeader } from "@/components/account/account-header";
+import { UpdateMailSubscribe } from "@/components/account/update-subscription";
import UploadAvatar from "@/components/account/upload-avatar";
+import { UpgradePlanModalWithDiscount } from "@/components/billing/upgrade-plan-modal-with-discount";
import AppLayout from "@/components/layouts/app";
import { Form } from "@/components/ui/form";
-import { UpgradePlanModalWithDiscount } from "@/components/billing/upgrade-plan-modal-with-discount";
import { PlanEnum } from "@/ee/stripe/constants";
import { usePlan } from "@/lib/swr/use-billing";
@@ -78,7 +79,7 @@ const ProfilePage: NextPage = () => {
/>
<Form
title="Your Email"
- description="This will be the email you use to log in to Papermark and receive notification. A confirmation is required for changes."
+ description="This will be the email you use to log in to Papermark and receive notifications. A confirmation is required for changes."
inputAttrs={{
name: "email",
placeholder: "[email protected]",
@@ -86,10 +87,8 @@ const ProfilePage: NextPage = () => {
type: "email",
}}
defaultValue={session?.user?.email ?? ""}
- // TODO: MAIL SUBSCRIPTION
- // helpText={<UpdateMailSubscribe />}
validate={validateEmail}
- helpText=""
+ helpText={<UpdateMailSubscribe />}
handleSubmit={(data) =>
fetch("/api/account", {
method: "PATCH",
diff --git a/pages/api/user/subscribe.ts b/pages/api/user/subscribe.ts
new file mode 100644
index 00000000..8e298ba1
--- /dev/null
+++ b/pages/api/user/subscribe.ts
@@ -0,0 +1,88 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { getServerSession } from "next-auth";
+
+import { errorhandler } from "@/lib/errorHandler";
+import prisma from "@/lib/prisma";
+import { resend, subscribe, unsubscribe } from "@/lib/resend";
+import { CustomUser } from "@/lib/types";
+
+import { authOptions } from "../auth/[...nextauth]";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const user = session.user as CustomUser;
+
+ if (!user.email) {
+ return res.status(400).json({ error: "User email not found" });
+ }
+
+ try {
+ if (req.method === "GET") {
+ // Get subscription status
+ const dbUser = await prisma.user.findUnique({
+ where: { id: user.id },
+ select: { email: true },
+ });
+
+ if (!dbUser || !dbUser.email || !resend) {
+ // No contact created yet, default to subscribed (opt-out model)
+ return res.status(200).json({ subscribed: true });
+ }
+
+ // Fetch contact from Resend to get actual subscription status
+ const { data: contact, error } = await resend.contacts.get({
+ email: dbUser.email,
+ });
+
+ if (error || !contact?.unsubscribed) {
+ // Contact not found or not unsubscribed, default to subscribed
+ return res.status(200).json({ subscribed: true });
+ }
+
+ return res.status(200).json({ subscribed: !contact.unsubscribed });
+ }
+
+ if (req.method === "POST") {
+ const dbUser = await prisma.user.findUnique({
+ where: { id: user.id },
+ select: { email: true },
+ });
+
+ if (!dbUser || !dbUser?.email) {
+ return res.status(400).json({ error: "User email not found" });
+ }
+
+ await subscribe(dbUser.email);
+
+ return res.status(200).json({ subscribed: true });
+ }
+
+ if (req.method === "DELETE") {
+ const dbUser = await prisma.user.findUnique({
+ where: { id: user.id },
+ select: { email: true },
+ });
+
+ if (!dbUser || !dbUser?.email) {
+ return res.status(400).json({ error: "User email not found" });
+ }
+
+ await unsubscribe(dbUser.email);
+
+ return res.status(200).json({ subscribed: false });
+ }
+
+ res.setHeader("Allow", ["GET", "POST", "DELETE"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ } catch (error) {
+ errorhandler(error, res);
+ }
+}