From 3bd51b89bf16a35f83d3a21a3194e99c33532115 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Fri, 12 Dec 2025 23:51:52 +0330 Subject: [PATCH] update --- .../interfaces/notification.interface.ts | 6 +- .../services/notification.service.ts | 24 +++++--- src/modules/orders/events/order.events.ts | 7 +++ .../orders/listeners/order.listeners.ts | 58 +++++++++++++++++++ src/modules/orders/orders.module.ts | 7 ++- .../pager/listeners/notification.listeners.ts | 6 +- 6 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 src/modules/orders/events/order.events.ts create mode 100644 src/modules/orders/listeners/order.listeners.ts diff --git a/src/modules/notifications/interfaces/notification.interface.ts b/src/modules/notifications/interfaces/notification.interface.ts index c6488ef..1b7a929 100644 --- a/src/modules/notifications/interfaces/notification.interface.ts +++ b/src/modules/notifications/interfaces/notification.interface.ts @@ -18,12 +18,12 @@ export enum NotifTitleEnum { export type recipientType = { userId: string } | { adminId: string }; export interface NotifRequestMessage { - subject: NotifTitleEnum; - body: string; + title: NotifTitleEnum; + content: string; smsText: string; pushNotif: { title: string; - body: string; + content: string; icon: string; action: { type: string; //view order diff --git a/src/modules/notifications/services/notification.service.ts b/src/modules/notifications/services/notification.service.ts index 7c934a2..99830a1 100644 --- a/src/modules/notifications/services/notification.service.ts +++ b/src/modules/notifications/services/notification.service.ts @@ -27,18 +27,18 @@ export class NotificationService { const notifications = await this.createAdminBulkNotifications( recipients.map(recipient => ({ restaurantId, - title: message.subject, - content: message.body, + title: message.title, + content: message.content, adminId: 'adminId' in recipient ? recipient.adminId : null, userId: 'userId' in recipient ? recipient.userId : null, })), ); // get admin prefrences - const preference = await this.preferenceService.findByRestaurantAndType(restaurantId, message.subject); + const preference = await this.preferenceService.findByRestaurantAndType(restaurantId, message.title); if (preference?.channels?.length === 0) { - this.logger.warn(`Notification type is NONE for restaurant ${restaurantId}, title ${message.subject}`); + this.logger.warn(`Notification type is NONE for restaurant ${restaurantId}, title ${message.title}`); return notifications; } @@ -49,8 +49,8 @@ export class NotificationService { this.notificationGateway.sendInAppNotification( { adminId: recipient.adminId, restaurantId }, { - subject: message.subject, - body: message.body, + subject: message.title, + body: message.content, }, ); } @@ -83,7 +83,7 @@ export class NotificationService { // await this.queueService.addPushNotification(job); // } - this.logger.log(`Queued notification for restaurant ${restaurantId}, title ${message.subject}`); + this.logger.log(`Queued notification for restaurant ${restaurantId}, title ${message.title}`); return notifications; } @@ -162,4 +162,14 @@ export class NotificationService { }, ); } + async findByAdminAndRestaurant(adminId: string, restaurantId: string, limit = 50): Promise { + return this.em.find( + Notification, + { admin: { id: adminId }, restaurant: { id: restaurantId } }, + { + orderBy: { createdAt: 'DESC' }, + limit, + }, + ); + } } diff --git a/src/modules/orders/events/order.events.ts b/src/modules/orders/events/order.events.ts new file mode 100644 index 0000000..0b3485e --- /dev/null +++ b/src/modules/orders/events/order.events.ts @@ -0,0 +1,7 @@ +export class OrderCreatedEvent { + constructor( + public readonly orderId: string, + public readonly restaurantId: string, + public readonly orderNumber: string, + ) {} +} diff --git a/src/modules/orders/listeners/order.listeners.ts b/src/modules/orders/listeners/order.listeners.ts new file mode 100644 index 0000000..23c0926 --- /dev/null +++ b/src/modules/orders/listeners/order.listeners.ts @@ -0,0 +1,58 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { OnEvent } from '@nestjs/event-emitter'; +import { AdminRepository } from 'src/modules/admin/repositories/admin.repository'; +import { OrderCreatedEvent } from '../events/order.events'; +import { Permission } from 'src/common/enums/permission.enum'; +import { NotifTitleEnum } from 'src/modules/notifications/interfaces/notification.interface'; +import { NotificationService } from 'src/modules/notifications/services/notification.service'; + +@Injectable() +export class OrderListeners { + private readonly logger = new Logger(OrderListeners.name); + + constructor( + private readonly adminService: AdminRepository, + private readonly notificationService: NotificationService, + ) {} + + @OnEvent(OrderCreatedEvent.name) + async handleOrderCreated(event: OrderCreatedEvent) { + try { + this.logger.log( + `Order created event received: ${event.orderId} for restaurant: ${event.restaurantId} and order number: ${event.orderNumber}`, + ); + // get admnin os restuaraant that have pager permissuins + const admins = await this.adminService.findAdminsWithPermission(event.restaurantId, Permission.MANAGE_ORDERS); + const recipients = admins.map(admin => ({ + adminId: admin.id, + })); + await this.notificationService.sendNotification({ + restaurantId: event.restaurantId, + message: { + title: NotifTitleEnum.ORDER_CREATED, + content: `Order ${event.orderNumber} has been created successfully`, + smsText: `Order ${event.orderNumber} has been created successfully`, + pushNotif: { + title: `Order ${event.orderNumber} has been created successfully`, + content: `Order ${event.orderNumber} has been created successfully`, + icon: `Order ${event.orderNumber} has been created successfully`, + action: { + type: `Order ${event.orderNumber} has been created successfully`, + url: `Order ${event.orderNumber} has been created successfully`, + }, + }, + }, + recipients, + metadata: { + priority: 1, + retries: 3, + }, + }); + } catch (error) { + this.logger.error( + `Failed to send notification for pager created event: ${event.restaurantId}`, + error instanceof Error ? error.stack : String(error), + ); + } + } +} diff --git a/src/modules/orders/orders.module.ts b/src/modules/orders/orders.module.ts index 1dad11f..a64387e 100644 --- a/src/modules/orders/orders.module.ts +++ b/src/modules/orders/orders.module.ts @@ -15,6 +15,9 @@ import { AuthModule } from '../auth/auth.module'; import { PaymentsModule } from '../payments/payments.module'; import { JwtModule } from '@nestjs/jwt'; import { OrderRepository } from './repositories/order.repository'; +import { OrderListeners } from './listeners/order.listeners'; +import { AdminModule } from '../admin/admin.module'; +import { NotificationsModule } from '../notifications/notifications.module'; @Module({ imports: [ @@ -24,9 +27,11 @@ import { OrderRepository } from './repositories/order.repository'; AuthModule, PaymentsModule, JwtModule, + AdminModule, + NotificationsModule, ], controllers: [OrdersController], - providers: [OrdersService, OrderRepository], + providers: [OrdersService, OrderRepository, OrderListeners], exports: [OrderRepository], }) export class OrdersModule {} diff --git a/src/modules/pager/listeners/notification.listeners.ts b/src/modules/pager/listeners/notification.listeners.ts index 6f7b7d3..2aa7531 100644 --- a/src/modules/pager/listeners/notification.listeners.ts +++ b/src/modules/pager/listeners/notification.listeners.ts @@ -27,12 +27,12 @@ export class PagerListeners { await this.notificationService.sendNotification({ restaurantId: event.restaurantId, message: { - subject: NotifTitleEnum.PAGER_CREATED, - body: `Your pager has created successfully`, + title: NotifTitleEnum.PAGER_CREATED, + content: `Your pager has created successfully`, smsText: `Your pager has created successfully`, pushNotif: { title: `Your pager has created successfully`, - body: `Your pager has created successfully`, + content: `Your pager has created successfully`, icon: `Your pager has created successfully`, action: { type: `Your pager has created successfully`,