This commit is contained in:
2025-12-12 22:42:58 +03:30
parent 10e13c7901
commit 0434329db6
5 changed files with 42 additions and 36 deletions
@@ -3,19 +3,12 @@ import { EntityManager } from '@mikro-orm/postgresql';
import { Notification } from '../entities/notification.entity';
import { NotificationPreferenceService } from './notification-preference.service';
import { NotificationQueueService } from './notification-queue.service';
import { NotificationsGateway } from '../notifications.gateway';
import { NotificationQueueJob } from '../interfaces/notification-queue.interface';
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
import { User } from '../../users/entities/user.entity';
import { NotifChannelEnum, NotifRequest, NotifTitleEnum } from '../interfaces/notification.interface';
export interface SendNotificationParams {
restaurantId: string;
userId?: string;
title: NotifTitleEnum;
content: string;
idempotencyKey?: string;
}
@Injectable()
export class NotificationService {
private readonly logger = new Logger(NotificationService.name);
@@ -24,22 +17,23 @@ export class NotificationService {
private readonly em: EntityManager,
private readonly preferenceService: NotificationPreferenceService,
private readonly queueService: NotificationQueueService,
private readonly notificationGateway: NotificationsGateway,
) {}
async sendNotification(params: NotifRequest): Promise<Notification[]> {
const { recipients, message, metadata } = params;
const { recipients, message, metadata, restaurantId } = params;
// create Database notifications
const notifications = await this.createAdminBulkNotifications(
recipients.map(recipient => ({
restaurantId: recipient.restaurantId,
restaurantId,
title: message.subject,
content: message.body,
adminId: 'adminId' in recipient ? recipient.adminId : null,
userId: 'userId' in recipient ? recipient.userId : null,
})),
);
const restaurantId = recipients[0].restaurantId;
// get admin prefrences
const preference = await this.preferenceService.findByRestaurantAndType(restaurantId, message.subject);
@@ -48,6 +42,17 @@ export class NotificationService {
return notifications;
}
// send in app notification
if (preference?.channels?.includes(NotifChannelEnum.IN_APP)) {
recipients.forEach(recipient => {
if ('adminId' in recipient) {
this.notificationGateway.sendInAppNotification({ adminId: recipient.adminId, restaurantId }, 'notification', {
title: message.subject,
content: message.body,
});
}
});
}
// const job: NotificationQueueJob = {
// restaurantId,
// userId,