Skip to content

Commit

Permalink
refactor: notificatons
Browse files Browse the repository at this point in the history
  • Loading branch information
mfts committed Mar 12, 2024
1 parent 84d6c07 commit 5d71100
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
37 changes: 37 additions & 0 deletions lib/api/notification-helper.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});
}
20 changes: 2 additions & 18 deletions pages/api/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
}

Expand Down
17 changes: 17 additions & 0 deletions trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 5d71100

Please sign in to comment.