update
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<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),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
|
||||
@@ -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`,
|
||||
|
||||
Reference in New Issue
Block a user