update notif
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export class PagerCreatedEvent {
|
||||
constructor(public readonly restaurantId: string) {}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
||||
import { PagerCreatedEvent } from '../events/pager.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 PagerListeners {
|
||||
private readonly logger = new Logger(PagerListeners.name);
|
||||
|
||||
constructor(
|
||||
private readonly adminService: AdminRepository,
|
||||
private readonly notificationService: NotificationService,
|
||||
) {}
|
||||
|
||||
@OnEvent(PagerCreatedEvent.name)
|
||||
async handlePagerCreated(event: PagerCreatedEvent) {
|
||||
try {
|
||||
this.logger.log(`Pager created event received: ${event.restaurantId}`);
|
||||
// get admnin os restuaraant that have pager permissuins
|
||||
const admins = await this.adminService.findAdminsWithPermission(event.restaurantId, Permission.MANAGE_PAGER);
|
||||
const recipients = admins.map(admin => ({
|
||||
restaurantId: event.restaurantId,
|
||||
adminId: admin.id,
|
||||
}));
|
||||
await this.notificationService.sendNotification({
|
||||
message: {
|
||||
subject: NotifTitleEnum.PAGER_CREATED,
|
||||
body: `Your pager has created successfully`,
|
||||
smsText: `Your pager has created successfully`,
|
||||
pushNotif: {
|
||||
title: `Your pager has created successfully`,
|
||||
body: `Your pager has created successfully`,
|
||||
icon: `Your pager has created successfully`,
|
||||
action: {
|
||||
type: `Your pager has created successfully`,
|
||||
url: `Your pager has 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),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import { Pager } from '../entities/pager.entity';
|
||||
import { PagerStatus } from '../interface/pager';
|
||||
import { ulid } from 'ulid';
|
||||
import { Admin } from '../../admin/entities/admin.entity';
|
||||
import { PagerCreatedEvent } from '../events/pager.events';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
|
||||
@Injectable()
|
||||
export class PagerService {
|
||||
@@ -14,6 +16,7 @@ export class PagerService {
|
||||
private readonly em: EntityManager,
|
||||
// @Inject(forwardRef(() => PagerGateway))
|
||||
// private readonly pagerGateway: PagerGateway,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
) {}
|
||||
|
||||
async create(
|
||||
@@ -65,10 +68,10 @@ export class PagerService {
|
||||
await this.em.persistAndFlush(pager);
|
||||
|
||||
// Populate relations before emitting
|
||||
await this.em.populate(pager, ['restaurant', 'user']);
|
||||
|
||||
await this.em.populate(pager, ['restaurant']);
|
||||
console.log('pager', pager);
|
||||
// Emit socket event for new pager
|
||||
// this.pagerGateway.emitPagerCreated(pager);
|
||||
this.eventEmitter.emit(PagerCreatedEvent.name, new PagerCreatedEvent(pager.restaurant.id));
|
||||
|
||||
return pager;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user