Skip to content

Commit ec03ab6

Browse files
fix: make vapid optional to prevent api failure at initialisation (calcom#24469)
* fix: added optional check for VAPID keys * fix: added extra check for vapid to prevent runtime error * fix: added consistent logger across the file * Apply suggestion from @keithwillcode --------- Co-authored-by: Keith Williams <keithwillcode@gmail.com>
1 parent c81b814 commit ec03ab6

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

packages/features/notifications/sendNotification.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1+
import { Logger } from "@nestjs/common";
12
import webpush from "web-push";
23

4+
const logger = new Logger("WebPush");
5+
let isVapidConfigured = false;
6+
37
const vapidKeys = {
48
publicKey: process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY || "",
59
privateKey: process.env.VAPID_PRIVATE_KEY || "",
610
};
711

8-
// The mail to email address should be the one at which push service providers can reach you. It can also be a URL.
9-
webpush.setVapidDetails("mailto:support@cal.com", vapidKeys.publicKey, vapidKeys.privateKey);
12+
if (vapidKeys.publicKey && vapidKeys.privateKey) {
13+
try {
14+
// The mail to email address should be the one at which push service providers can reach you. It can also be a URL.
15+
webpush.setVapidDetails("mailto:support@cal.com", vapidKeys.publicKey, vapidKeys.privateKey);
16+
logger.log("VAPID keys loaded. Web push enabled.");
17+
isVapidConfigured = true;
18+
} catch (err) {
19+
logger.error("Failed to initialize web push", err);
20+
}
21+
} else {
22+
logger.warn("Missing VAPID keys. Web push notifications are disabled.");
23+
}
1024

1125
type Subscription = {
1226
endpoint: string;
@@ -35,6 +49,10 @@ export const sendNotification = async ({
3549
requireInteraction?: boolean;
3650
type?: string;
3751
}) => {
52+
if (!isVapidConfigured) {
53+
logger.error("Cannot send notification. VAPID keys not configured.");
54+
return;
55+
}
3856
try {
3957
const payload = JSON.stringify({
4058
title,
@@ -50,6 +68,6 @@ export const sendNotification = async ({
5068
});
5169
await webpush.sendNotification(subscription, payload);
5270
} catch (error) {
53-
console.error("Error sending notification", error);
71+
logger.error("Error sending notification", error);
5472
}
5573
};

0 commit comments

Comments
 (0)