Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add email notification for visited document
Marc Seitz435dc26b90e1
diff --git a/components/emails/viewed-document.tsx b/components/emails/viewed-document.tsxnew file mode 100644index 00000000..1076a0b2--- /dev/null+++ b/components/emails/viewed-document.tsx@@ -0,0 +1,86 @@+import React from "react";+import {+ Body,+ Button,+ Container,+ Head,+ Heading,+ Html,+ Preview,+ Section,+ Text,+ Tailwind,+ Hr,+} from "@react-email/components";++export default function ViewedDocument({+ documentId = "123",+ documentName = "Pitchdeck",+ viewerEmail = "[email protected]",+}: {+ documentId: string;+ documentName: string;+ viewerEmail: string | null;+}) {+ return (+ <Html>+ <Head />+ <Preview>Invalid Domain Configuration</Preview>+ <Tailwind>+ <Body className="bg-white my-auto mx-auto font-sans">+ <Container className="my-10 mx-auto p-5 w-[465px]">+ <Heading className="text-2xl font-normal text-center p-0 mt-4 mb-8 mx-0">+ <span className="font-bold tracking-tighter">Papermark</span>+ </Heading>+ <Heading className="mx-0 my-7 p-0 text-center text-xl font-semibold text-black">+ New Document Visitor+ </Heading>+ <Text className="text-sm leading-6 text-black">+ Your document{" "}+ <span className="font-semibold">{documentName}</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 insights like time-spent per+ page and total duration for this document on Papermark.+ </Text>+ <Section className="my-8 text-center">+ <Button+ pX={20}+ pY={12}+ className="bg-black rounded text-white text-xs font-semibold no-underline text-center"+ href={`https://www.papermark.io/documents/${documentId}`}+ >+ See my document 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="no-underline text-gray-400 hover:text-gray-400 visited: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>+ </Section>+ </Container>+ </Body>+ </Tailwind>+ </Html>+ );+}diff --git a/lib/emails/send-viewed-document.ts b/lib/emails/send-viewed-document.tsnew file mode 100644index 00000000..688a951b--- /dev/null+++ b/lib/emails/send-viewed-document.ts@@ -0,0 +1,17 @@+import { sendEmail } from "@/lib/resend";+import ViewedDocumentEmail from "@/components/emails/viewed-document";++export const sendViewedDocumentEmail = async (+ email: string,+ documentId: string,+ documentName : string,+ viewerEmail: string | null,+) => {+ const emailTemplate = ViewedDocumentEmail({ documentId, documentName, viewerEmail });+ await sendEmail({+ to: email,+ subject: `Your document has been viewed: ${documentName}`,+ react: emailTemplate,+ test: process.env.NODE_ENV === "development",+ });+};diff --git a/pages/api/views.ts b/pages/api/views.tsindex ccde0311..df8f51ee 100644--- a/pages/api/views.ts+++ b/pages/api/views.ts@@ -3,6 +3,7 @@ import prisma from "@/lib/prisma";import { checkPassword, log } from "@/lib/utils";import { analytics, identifyUser, trackAnalytics } from "@/lib/analytics";import { CustomUser } from "@/lib/types";+import { sendViewedDocumentEmail } from "@/lib/emails/send-viewed-document";export default async function handle(req: NextApiRequest,@@ -68,6 +69,8 @@ export default async function handle(include: {document: {select: {+ ownerId: true,+ name: true,versions: {where: { isPrimary: true },orderBy: { createdAt: "desc" },@@ -90,6 +93,26 @@ export default async function handle(viewerEmail: email,});+ // get document owner email+ const userEmail = await prisma.user.findUnique({+ where: {+ id: newView.document.ownerId,+ },+ select: {+ email: true,+ },+ });++ if (userEmail) {+ // send email to document owner that document has been viewed+ await sendViewedDocumentEmail(+ userEmail.email as string,+ documentId,+ newView.document.name,+ );+ }+// check if document version has multiple pages, if so, return the pagesif (newView.document.versions[0].hasPages) {const pages = await prisma.documentPage.findMany({