This commit is contained in:
2025-12-12 23:51:52 +03:30
parent 2815c503ff
commit 3bd51b89bf
6 changed files with 94 additions and 14 deletions
@@ -18,12 +18,12 @@ export enum NotifTitleEnum {
export type recipientType = { userId: string } | { adminId: string }; export type recipientType = { userId: string } | { adminId: string };
export interface NotifRequestMessage { export interface NotifRequestMessage {
subject: NotifTitleEnum; title: NotifTitleEnum;
body: string; content: string;
smsText: string; smsText: string;
pushNotif: { pushNotif: {
title: string; title: string;
body: string; content: string;
icon: string; icon: string;
action: { action: {
type: string; //view order type: string; //view order
@@ -27,18 +27,18 @@ export class NotificationService {
const notifications = await this.createAdminBulkNotifications( const notifications = await this.createAdminBulkNotifications(
recipients.map(recipient => ({ recipients.map(recipient => ({
restaurantId, restaurantId,
title: message.subject, title: message.title,
content: message.body, content: message.content,
adminId: 'adminId' in recipient ? recipient.adminId : null, adminId: 'adminId' in recipient ? recipient.adminId : null,
userId: 'userId' in recipient ? recipient.userId : null, userId: 'userId' in recipient ? recipient.userId : null,
})), })),
); );
// get admin prefrences // 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) { 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; return notifications;
} }
@@ -49,8 +49,8 @@ export class NotificationService {
this.notificationGateway.sendInAppNotification( this.notificationGateway.sendInAppNotification(
{ adminId: recipient.adminId, restaurantId }, { adminId: recipient.adminId, restaurantId },
{ {
subject: message.subject, subject: message.title,
body: message.body, body: message.content,
}, },
); );
} }
@@ -83,7 +83,7 @@ export class NotificationService {
// await this.queueService.addPushNotification(job); // 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; return notifications;
} }
@@ -162,4 +162,14 @@ export class NotificationService {
}, },
); );
} }
async findByAdminAndRestaurant(adminId: string, restaurantId: string, limit = 50): Promise<Notification[]> {
return this.em.find(
Notification,
{ admin: { id: adminId }, restaurant: { id: restaurantId } },
{
orderBy: { createdAt: 'DESC' },
limit,
},
);
}
} }
@@ -0,0 +1,7 @@
export class OrderCreatedEvent {
constructor(
public readonly orderId: string,
public readonly restaurantId: string,
public readonly orderNumber: string,
) {}
}
@@ -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),
);
}
}
}
+6 -1
View File
@@ -15,6 +15,9 @@ import { AuthModule } from '../auth/auth.module';
import { PaymentsModule } from '../payments/payments.module'; import { PaymentsModule } from '../payments/payments.module';
import { JwtModule } from '@nestjs/jwt'; import { JwtModule } from '@nestjs/jwt';
import { OrderRepository } from './repositories/order.repository'; 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({ @Module({
imports: [ imports: [
@@ -24,9 +27,11 @@ import { OrderRepository } from './repositories/order.repository';
AuthModule, AuthModule,
PaymentsModule, PaymentsModule,
JwtModule, JwtModule,
AdminModule,
NotificationsModule,
], ],
controllers: [OrdersController], controllers: [OrdersController],
providers: [OrdersService, OrderRepository], providers: [OrdersService, OrderRepository, OrderListeners],
exports: [OrderRepository], exports: [OrderRepository],
}) })
export class OrdersModule {} export class OrdersModule {}
@@ -27,12 +27,12 @@ export class PagerListeners {
await this.notificationService.sendNotification({ await this.notificationService.sendNotification({
restaurantId: event.restaurantId, restaurantId: event.restaurantId,
message: { message: {
subject: NotifTitleEnum.PAGER_CREATED, title: NotifTitleEnum.PAGER_CREATED,
body: `Your pager has created successfully`, content: `Your pager has created successfully`,
smsText: `Your pager has created successfully`, smsText: `Your pager has created successfully`,
pushNotif: { pushNotif: {
title: `Your pager has created successfully`, title: `Your pager has created successfully`,
body: `Your pager has created successfully`, content: `Your pager has created successfully`,
icon: `Your pager has created successfully`, icon: `Your pager has created successfully`,
action: { action: {
type: `Your pager has created successfully`, type: `Your pager has created successfully`,