Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add view notifications to dataroom
Marc Seitzb8760e9eac9d
diff --git a/components/emails/viewed-dataroom.tsx b/components/emails/viewed-dataroom.tsxnew file mode 100644index 00000000..82953b9b--- /dev/null+++ b/components/emails/viewed-dataroom.tsx@@ -0,0 +1,89 @@+import React from "react";++import {+ Body,+ Button,+ Container,+ Head,+ Hr,+ Html,+ Preview,+ Section,+ Tailwind,+ Text,+} from "@react-email/components";++export default function ViewedDataroom({+ dataroomId = "123",+ dataroomName = "Example Dataroom",+ viewerEmail,+}: {+ dataroomId: string;+ dataroomName: string;+ viewerEmail: string | null;+}) {+ return (+ <Html>+ <Head />+ <Preview>See who visited your dataroom</Preview>+ <Tailwind>+ <Body className="mx-auto my-auto bg-white font-sans">+ <Container className="mx-auto my-10 w-[465px] p-5">+ <Text className="mx-0 mb-8 mt-4 p-0 text-center text-2xl font-normal">+ <span className="font-bold tracking-tighter">Papermark</span>+ </Text>+ <Text className="mx-0 my-7 p-0 text-center text-xl font-semibold text-black">+ New Dataroom Visitor+ </Text>+ <Text className="text-sm leading-6 text-black">+ Your dataroom{" "}+ <span className="font-semibold">{dataroomName}</span> was just+ viewed by{" "}+ <span className="font-semibold">+ {viewerEmail ? `${viewerEmail}` : `someone`}+ </span>+ .+ </Text>+ <Text className="text-sm leading-6 text-black">+ You can get the detailed engagement analytics like time-spent per+ document page and total duration for this dataroom on Papermark.+ </Text>+ <Section className="my-8 text-center">+ <Button+ className="rounded bg-black text-center text-xs font-semibold text-white no-underline"+ href={`https://www.papermark.io/datarooms/${dataroomId}`}+ style={{ padding: "12px 20px" }}+ >+ See my dataroom insights+ </Button>+ </Section>+ <Text className="text-sm leading-6 text-black">+ Stay informed, stay ahead with Papermark.+ </Text>+ <Hr />+ <Section className="mt-8 text-gray-400">+ <Text className="text-xs">+ © {new Date().getFullYear()}{" "}+ <a+ href="https://www.papermark.io"+ className="text-gray-400 no-underline visited:text-gray-400 hover:text-gray-400"+ target="_blank"+ >+ papermark.io+ </a>+ </Text>+ <Text className="text-xs">+ If you have any feedback or questions about this email, simply+ reply to it. I'd love to hear from you!+ </Text>+ <Text className="text-xs">+ To stop email notifications for this link, edit the link and+ uncheck "Receive email notification".+ </Text>+ </Section>+ </Container>+ </Body>+ </Tailwind>+ </Html>+ );+}diff --git a/lib/emails/send-viewed-dataroom.ts b/lib/emails/send-viewed-dataroom.tsnew file mode 100644index 00000000..7392d456--- /dev/null+++ b/lib/emails/send-viewed-dataroom.ts@@ -0,0 +1,46 @@+import ViewedDataroomEmail from "@/components/emails/viewed-dataroom";++import { sendEmail } from "@/lib/resend";++export const sendViewedDataroomEmail = async ({+ ownerEmail,+ dataroomId,+ dataroomName,+ viewerEmail,+ teamMembers,+}: {+ ownerEmail: string | null;+ dataroomId: string;+ dataroomName: string;+ viewerEmail: string | null;+ teamMembers?: string[];+}) => {+ const emailTemplate = ViewedDataroomEmail({+ dataroomId,+ dataroomName,+ viewerEmail,+ });+ try {+ if (!ownerEmail) {+ throw new Error("Dataroom Admin not found");+ }++ let subjectLine: string = `Your dataroom has been viewed: ${dataroomName}`;+ if (viewerEmail) {+ subjectLine = `${viewerEmail} viewed the dataroom: ${dataroomName}`;+ }++ const data = await sendEmail({+ to: ownerEmail,+ cc: teamMembers,+ subject: subjectLine,+ react: emailTemplate,+ test: process.env.NODE_ENV === "development",+ system: true,+ });++ return { success: true, data };+ } catch (error) {+ return { success: false, error };+ }+};diff --git a/pages/api/jobs/send-notification.ts b/pages/api/jobs/send-notification.tsindex f2637849..146e8b72 100644--- a/pages/api/jobs/send-notification.ts+++ b/pages/api/jobs/send-notification.ts@@ -1,5 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";+import { sendViewedDataroomEmail } from "@/lib/emails/send-viewed-dataroom";import { sendViewedDocumentEmail } from "@/lib/emails/send-viewed-document";import prisma from "@/lib/prisma";import { log } from "@/lib/utils";@@ -33,6 +34,7 @@ export default async function handle(};let view: {+ viewType: "DOCUMENT_VIEW" | "DATAROOM_VIEW";viewerEmail: string | null;linkId: string;document: {@@ -44,15 +46,21 @@ export default async function handle(email: string | null;};} | null;+ dataroom: {+ teamId: string | null;+ id: string;+ name: string;+ } | null;} | null;try {- // Fetch the link to verify the settings+ // Fetch the view with dataview = await prisma.view.findUnique({where: {id: viewId,},select: {+ viewType: true,viewerEmail: true,linkId: true,document: {@@ -68,16 +76,23 @@ export default async function handle(},},},+ dataroom: {+ select: {+ teamId: true,+ id: true,+ name: true,+ },+ },},});if (!view) {- res.status(404).json({ message: "View / Document not found." });+ res.status(404).json({ message: "View not found." });return;}} catch (error) {log({- message: `Failed to find view for viewId: ${viewId}. \n\n Error: ${error}`,+ message: `Failed to find document / dataroom view for viewId: ${viewId}. \n\n Error: ${error}`,type: "error",mention: true,});@@ -85,13 +100,17 @@ export default async function handle(return;}- // Update the view to mark it as viewed+ // Get all team members who are admins or managers to be notifiedconst users = await prisma.userTeam.findMany({where: {role: { in: ["ADMIN", "MANAGER"] },- teamId: view.document!.teamId!,+ teamId:+ view.viewType === "DOCUMENT_VIEW"+ ? view.document!.teamId!+ : view.dataroom!.teamId!,},select: {+ role: true,user: {select: {email: true,@@ -102,16 +121,32 @@ export default async function handle(// POST /api/jobs/send-notificationtry {- // send email to document owner that document- await sendViewedDocumentEmail({- ownerEmail: view.document!.owner.email,- documentId: view.document!.id,- documentName: view.document!.name,- viewerEmail: view.viewerEmail,- teamMembers: users- .map((user) => user.user.email!)- .filter((email) => email !== view.document!.owner.email),- });+ if (view.viewType === "DOCUMENT_VIEW") {+ // send email to document owner that document+ await sendViewedDocumentEmail({+ ownerEmail: view.document!.owner.email,+ documentId: view.document!.id,+ documentName: view.document!.name,+ viewerEmail: view.viewerEmail,+ teamMembers: users+ .map((user) => user.user.email!)+ .filter((email) => email !== view.document!.owner.email),+ });+ } else {+ // prettier-ignore+ const adminEmail = users+ .find((user) => user.role === "ADMIN")?.user.email;+ // send email to dataroom owner that dataroom+ await sendViewedDataroomEmail({+ ownerEmail: adminEmail!,+ dataroomId: view.dataroom!.id,+ dataroomName: view.dataroom!.name,+ viewerEmail: view.viewerEmail,+ teamMembers: users+ .map((user) => user.user.email!)+ .filter((email) => email !== adminEmail),+ });+ }res.status(200).json({ message: "Successfully sent notification", viewId });return;diff --git a/pages/api/views-dataroom.ts b/pages/api/views-dataroom.tsindex 577063e4..69e5d6b3 100644--- a/pages/api/views-dataroom.ts+++ b/pages/api/views-dataroom.ts@@ -1,7 +1,9 @@import { NextApiRequest, NextApiResponse } from "next";+import { waitUntil } from "@vercel/functions";import { parsePageId } from "notion-utils";+import sendNotification from "@/lib/api/notification-helper";import { sendVerificationEmail } from "@/lib/emails/send-email-verification";import { getFile } from "@/lib/files/get-file";import { newId } from "@/lib/id-helper";@@ -10,6 +12,11 @@ import prisma from "@/lib/prisma";import { parseSheet } from "@/lib/sheet";import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";+export const config = {+ // in order to enable `waitUntil` function+ supportsResponseStreaming: true,+};+export default async function handle(req: NextApiRequest,res: NextApiResponse,@@ -269,6 +276,12 @@ export default async function handle(});console.timeEnd("create-view");+ if (link.enableNotification) {+ console.time("sendemail");+ waitUntil(sendNotification({ viewId: newDataroomView.id }));+ console.timeEnd("sendemail");+ }+const returnObject = {message: "Dataroom View recorded",viewId: newDataroomView.id,