Skip to content

Commit

Permalink
fix(api): handle failing to send push notification (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanbook authored Jul 30, 2024
1 parent d6e0d05 commit 8b618d8
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Injectable } from "@nestjs/common";
import { sendNotification, setVapidDetails } from "web-push";

import { Logger } from "src/core/logging";
import { getRequiredStringConfig } from "src/utils/config.helper";

import { NotificationSubscriptionService } from "../../domain/services/notification-subscription.service";
import { INotification } from "../../types";

@Injectable()
export class NotificationWebPushGateway {
private logger = new Logger(NotificationWebPushGateway.name);

constructor(
private readonly notificationSubscriptionService: NotificationSubscriptionService,
) {
Expand All @@ -29,12 +32,20 @@ export class NotificationWebPushGateway {
return false;
}

const result = await sendNotification(
pushSubscription,
JSON.stringify(notification),
);

return result.statusCode < 400;
try {
const result = await sendNotification(
pushSubscription,
JSON.stringify(notification),
);

return result.statusCode < 400;
} catch (error) {
this.logger.error("Unable to send push notification", {
error,
notification,
});
return false;
}
}

public async sendWebPush(
Expand Down

0 comments on commit 8b618d8

Please sign in to comment.