Skip to content

Commit 43d8941

Browse files
Johan BookJohan Book
Johan Book
authored and
Johan Book
committed
feat(api): enable web push notifications
1 parent b0a986c commit 43d8941

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

services/api/src/core/notifications/client/gateways/notification.web-push.gateway.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,35 @@ export class NotificationWebPushGateway {
1818
);
1919
}
2020

21-
public async sendWebPush(profileId: number, notification: INotification) {
21+
private async sendWebPushToProfile(
22+
profileId: number,
23+
notification: INotification,
24+
): Promise<boolean> {
2225
const pushSubscription =
2326
this.notificationSubscriptionService.getSubscription(profileId);
2427

2528
if (!pushSubscription) {
26-
return;
29+
return false;
2730
}
2831

29-
await sendNotification(pushSubscription, JSON.stringify(notification));
32+
const result = await sendNotification(
33+
pushSubscription,
34+
JSON.stringify(notification),
35+
);
36+
37+
return result.statusCode < 400;
38+
}
39+
40+
public async sendWebPush(
41+
profileIds: number[],
42+
notification: INotification,
43+
): Promise<Record<number, boolean>> {
44+
const result: Record<number, boolean> = {};
45+
46+
for (const profileId of profileIds) {
47+
await this.sendWebPushToProfile(profileId, notification);
48+
}
49+
50+
return result;
3051
}
3152
}

services/api/src/core/notifications/domain/services/notification.service.ts

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Profile } from "src/core/profiles";
1010
import { getRequiredStringConfig } from "src/utils/config.helper";
1111

1212
import { NotificationGateway } from "../../client/gateways/notification.gateway";
13+
import { NotificationWebPushGateway } from "../../client/gateways/notification.web-push.gateway";
1314
import { Notification } from "../../infrastructure/entities/notification.entity";
1415
import { INotification } from "../../types";
1516

@@ -24,6 +25,7 @@ export class NotificationService {
2425
@InjectRepository(Notification)
2526
private readonly notifications: Repository<Notification>,
2627
private readonly notificationGateway: NotificationGateway,
28+
private readonly notificationWebPushGateway: NotificationWebPushGateway,
2729
@InjectRepository(OrganizationMembership)
2830
private readonly organizationMemberships: Repository<OrganizationMembership>,
2931
@InjectRepository(Profile)
@@ -61,6 +63,10 @@ export class NotificationService {
6163
});
6264
await this.notifications.save(newNotifications);
6365

66+
// TODO: Do not send web push if web socket succeded
67+
const profileIds = memberships.map((membership) => membership.profile.id);
68+
await this.notificationWebPushGateway.sendWebPush(profileIds, notification);
69+
6470
const userIds = memberships.map((membership) => membership.profile.userId);
6571
await this.notifyUsers(userIds, notification);
6672
}
@@ -72,6 +78,9 @@ export class NotificationService {
7278
where: { id: In(profileIds) },
7379
});
7480

81+
// TODO: Do not send web push if web socket succeded
82+
await this.notificationWebPushGateway.sendWebPush(profileIds, notification);
83+
7584
const ids = profiles.map((profile) => profile.userId);
7685

7786
await this.notifyUsers(ids, notification);

0 commit comments

Comments
 (0)