feat: working on push notifications journey

This commit is contained in:
Abdalhamid Alhamad
2024-12-24 12:10:49 +03:00
parent c7470302bd
commit 3719498c2f
35 changed files with 1508 additions and 56 deletions

View File

@ -0,0 +1,44 @@
import { Injectable } from '@nestjs/common';
import { DeviceService } from '~/auth/services';
import { PageOptionsRequestDto } from '~/core/dtos';
import { Notification } from '../entities';
import { NotificationsRepository } from '../repositories';
import { FirebaseService } from './firebase.service';
@Injectable()
export class NotificationsService {
constructor(
private readonly deviceService: DeviceService,
private readonly firebaseService: FirebaseService,
private readonly notificationRepository: NotificationsRepository,
) {}
async sendPushNotification(userId: string, title: string, body: string) {
// Get the device tokens for the user
const tokens = await this.deviceService.getTokens(userId);
if (!tokens.length) {
return;
}
// Send the notification
return this.firebaseService.sendNotification(tokens, title, body);
}
async getNotifications(userId: string, pageOptionsDto: PageOptionsRequestDto) {
const [[notifications, count], unreadCount] = await Promise.all([
this.notificationRepository.getNotifications(userId, pageOptionsDto),
this.notificationRepository.getUnreadNotificationsCount(userId),
]);
return { notifications, count, unreadCount };
}
createNotification(notification: Partial<Notification>) {
return this.notificationRepository.createNotification(notification);
}
markAsRead(userId: string) {
return this.notificationRepository.markAsRead(userId);
}
}