Skip to content

Commit

Permalink
fix(api): handle failing to send push notification
Browse files Browse the repository at this point in the history
  • Loading branch information
johanbook committed Jul 30, 2024
1 parent d6e0d05 commit b4fa39b
Showing 1 changed file with 13 additions and 5 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,17 @@ export class NotificationWebPushGateway {
return false;
}

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

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

public async sendWebPush(
Expand Down

0 comments on commit b4fa39b

Please sign in to comment.