Skip to content

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

refactor: remove await from document-view notification

Marc Seitze234fe138f7b
diff --git a/lib/emails/send-viewed-document.ts b/lib/emails/send-viewed-document.ts
index 5cd1b5fe..2c82009e 100644
--- a/lib/emails/send-viewed-document.ts
+++ b/lib/emails/send-viewed-document.ts
@@ -1,13 +1,14 @@
import { sendEmail } from "@/lib/resend";
import ViewedDocumentEmail from "@/components/emails/viewed-document";
+import prisma from "@/lib/prisma";
export const sendViewedDocumentEmail = async ({
- email,
+ ownerId,
documentId,
documentName,
viewerEmail,
}: {
- email: string;
+ ownerId: string;
documentId: string;
documentName: string;
viewerEmail: string | null;
@@ -18,15 +19,23 @@ export const sendViewedDocumentEmail = async ({
viewerEmail,
});
try {
+ const owner = await prisma.user.findUnique({
+ where: { id: ownerId },
+ select: { email: true },
+ });
+
+ if (!owner || !owner.email) {
+ throw new Error("Document Owner not found");
+ }
+
const data = await sendEmail({
- to: email,
+ to: owner.email,
subject: `Your document has been viewed: ${documentName}`,
react: emailTemplate,
test: process.env.NODE_ENV === "development",
system: true,
});
- // If sendEmail is successful, return the data
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.ts
index 7207f3be..9b2b4b63 100644
--- a/pages/api/jobs/send-notification.ts
+++ b/pages/api/jobs/send-notification.ts
@@ -43,11 +43,7 @@ export default async function handle(
select: {
id: true,
name: true,
- owner: {
- select: {
- email: true,
- },
- },
+ ownerId: true,
},
},
},
@@ -60,7 +56,7 @@ export default async function handle(
// send email to document owner that document
await sendViewedDocumentEmail({
- email: view.document.owner.email as string,
+ ownerId: view.document.ownerId,
documentId: view.document.id,
documentName: view.document.name,
viewerEmail: view.viewerEmail,
diff --git a/pages/api/views.ts b/pages/api/views.ts
index c7748136..e68c3d26 100644
--- a/pages/api/views.ts
+++ b/pages/api/views.ts
@@ -2,10 +2,10 @@ import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
import { checkPassword, log } from "@/lib/utils";
import { trackAnalytics } from "@/lib/analytics";
-import { client } from "@/trigger";
import { newId } from "@/lib/id-helper";
import { sendVerificationEmail } from "@/lib/emails/send-email-verification";
import { getFile } from "@/lib/files/get-file";
+import { sendViewedDocumentEmail } from "@/lib/emails/send-viewed-document";
export default async function handle(
req: NextApiRequest,
@@ -257,12 +257,31 @@ export default async function handle(
console.timeEnd("track-analytics");
if (link.enableNotification) {
- // trigger link viewed event to trigger send-notification job
console.time("sendemail");
- await client.sendEvent({
- name: "link.viewed",
- payload: { viewId: newView.id },
- });
+ 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,
+ });
+ });
console.timeEnd("sendemail");
}