Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
refactor(view-notification): kick off a new function
Marc Seitzb1b4d1e1f31a
diff --git a/lib/emails/send-viewed-document.ts b/lib/emails/send-viewed-document.tsindex 2c82009e..ade06360 100644--- a/lib/emails/send-viewed-document.ts+++ b/lib/emails/send-viewed-document.ts@@ -1,14 +1,13 @@import { sendEmail } from "@/lib/resend";import ViewedDocumentEmail from "@/components/emails/viewed-document";-import prisma from "@/lib/prisma";export const sendViewedDocumentEmail = async ({- ownerId,+ ownerEmail,documentId,documentName,viewerEmail,}: {- ownerId: string;+ ownerEmail: string | null;documentId: string;documentName: string;viewerEmail: string | null;@@ -19,17 +18,12 @@ export const sendViewedDocumentEmail = async ({viewerEmail,});try {- const owner = await prisma.user.findUnique({- where: { id: ownerId },- select: { email: true },- });-- if (!owner || !owner.email) {+ if (!ownerEmail) {throw new Error("Document Owner not found");}const data = await sendEmail({- to: owner.email,+ to: ownerEmail,subject: `Your document has been viewed: ${documentName}`,react: emailTemplate,test: process.env.NODE_ENV === "development",diff --git a/pages/api/jobs/send-notification.ts b/pages/api/jobs/send-notification.tsindex 9b2b4b63..6f95b66b 100644--- a/pages/api/jobs/send-notification.ts+++ b/pages/api/jobs/send-notification.ts@@ -1,6 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import prisma from "@/lib/prisma";import { sendViewedDocumentEmail } from "@/lib/emails/send-viewed-document";+import { log } from "@/lib/utils";export const config = {maxDuration: 60,@@ -26,24 +27,40 @@ export default async function handle(return;}- // POST /api/jobs/send-notification- try {- const { viewId } = req.body as {- viewId: string;+ const { viewId } = req.body as {+ viewId: string;+ };++ let view: {+ viewerEmail: string | null;+ linkId: string;+ document: {+ id: string;+ name: string;+ owner: {+ email: string | null;+ };};+ } | null;+ try {// Fetch the link to verify the settings- const view = await prisma.view.findUnique({+ view = await prisma.view.findUnique({where: {id: viewId,},select: {viewerEmail: true,+ linkId: true,document: {select: {id: true,name: true,- ownerId: true,+ owner: {+ select: {+ email: true,+ },+ },},},},@@ -53,10 +70,21 @@ export default async function handle(res.status(404).json({ message: "View / Document not found." });return;}+ } catch (error) {+ log({+ message: `Failed to find view for viewId: ${viewId}. \n\n Error: ${error}`,+ type: "error",+ mention: true,+ });+ res.status(500).json({ message: (error as Error).message });+ return;+ }+ // POST /api/jobs/send-notification+ try {// send email to document owner that documentawait sendViewedDocumentEmail({- ownerId: view.document.ownerId,+ ownerEmail: view.document.owner.email,documentId: view.document.id,documentName: view.document.name,viewerEmail: view.viewerEmail,@@ -65,6 +93,11 @@ export default async function handle(res.status(200).json({ message: "Successfully sent notification", viewId });return;} catch (error) {+ log({+ message: `Failed to send email in _/api/views_ route for linkId: ${view.linkId}. \n\n Error: ${error} \n\n*Metadata*: \`{ownerId: ${view.document.ownerId}, viewId: ${viewId}}\``,+ type: "error",+ mention: true,+ });return res.status(500).json({ message: (error as Error).message });}}diff --git a/pages/api/views.ts b/pages/api/views.tsindex cb534503..26826e1c 100644--- a/pages/api/views.ts+++ b/pages/api/views.ts@@ -244,30 +244,15 @@ export default async function handle(if (link.enableNotification) {console.time("sendemail");- sendViewedDocumentEmail({- ownerId: ownerId,- documentId,- documentName: documentName,- viewerEmail: email,- })- .then((res) => {- if (!res.success) {- log({- message: `Failed to send email in _/api/views_ route for linkId: ${linkId}. \n\n Error: ${res.error} \n\n*Metadata*: \`{ownerId: ${ownerId}, viewId: ${newView.id}}\``,- type: "error",- mention: true,- });- }- })- .catch((error) => {- // This catch block will only be triggered if there's an uncaught exception in sendViewedDocumentEmail.- // Given the refactored design, it's less likely to be used, but it's good practice to keep it for catching unexpected errors.- log({- message: `Unexpected error in _/api/views_ route for ${linkId}. \n\n Error: ${error} \n\n*Metadata*: \`{ownerId: ${ownerId}, viewId: ${newView.id}}\``,- type: "error",- mention: true,- });- });+ fetch(`${process.env.NEXTAUTH_URL}/api/jobs/send-notification`, {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ Authorization: `Bearer ${process.env.INTERNAL_API_KEY}`,+ },+ body: JSON.stringify({ viewId: newView.id }),+ });+console.timeEnd("sendemail");}