Skip to content

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

refactor: notificatons

Marc Seitz5d711003032e
diff --git a/lib/api/notification-helper.ts b/lib/api/notification-helper.ts
new file mode 100644
index 00000000..1f46af54
--- /dev/null
+++ b/lib/api/notification-helper.ts
@@ -0,0 +1,37 @@
+import { getTriggerClient } from "@/trigger";
+import { log } from "@/lib/utils";
+
+export default async function sendNotification({ viewId }: { viewId: string }) {
+ const client = getTriggerClient();
+
+ if (!client) {
+ /** If client does not exist, use fetch to send notifications */
+ return await 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: viewId }),
+ },
+ )
+ .then(() => {})
+ .catch((error) => {
+ log({
+ message: `Failed to fetch notifications job in _/api/views_ route. \n\n Error: ${error} \n\n*Metadata*: \`{viewId: ${viewId}}\``,
+ type: "error",
+ mention: true,
+ });
+ });
+ }
+
+ /** If client exists, use trigger to send notifications */
+ return await client.sendEvent({
+ name: "link.viewed",
+ payload: {
+ viewId: viewId,
+ },
+ });
+}
diff --git a/pages/api/views.ts b/pages/api/views.ts
index 1c9778d6..d6bac30b 100644
--- a/pages/api/views.ts
+++ b/pages/api/views.ts
@@ -4,7 +4,7 @@ import { checkPassword, log } from "@/lib/utils";
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";
+import sendNotification from "@/lib/api/notification-helper";
export default async function handle(
req: NextApiRequest,
@@ -244,23 +244,7 @@ export default async function handle(
if (link.enableNotification) {
console.time("sendemail");
- 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 }),
- })
- .then(() => {})
- .catch((error) => {
- log({
- message: `Failed to fetch notifications job in _/api/views_ route for linkId: ${linkId}. \n\n Error: ${error} \n\n*Metadata*: \`{viewId: ${newView.id}}\``,
- type: "error",
- mention: true,
- });
- });
-
+ await sendNotification({ viewId: newView.id });
console.timeEnd("sendemail");
}
diff --git a/trigger.ts b/trigger.ts
index b802654c..a3415bb8 100644
--- a/trigger.ts
+++ b/trigger.ts
@@ -5,3 +5,20 @@ export const client = new TriggerClient({
apiKey: process.env.TRIGGER_API_KEY,
apiUrl: process.env.TRIGGER_API_URL,
});
+
+export function getTriggerClient(): TriggerClient | null {
+ const triggerKey = process.env.TRIGGER_API_KEY;
+ const triggerUrl = process.env.TRIGGER_API_URL;
+
+ if (!triggerKey || !triggerUrl) {
+ return null;
+ }
+
+ const client = new TriggerClient({
+ id: "papermark-dev-Ojwu",
+ apiKey: process.env.TRIGGER_API_KEY,
+ apiUrl: process.env.TRIGGER_API_URL,
+ });
+
+ return client;
+}