chat notification

This commit is contained in:
2026-02-28 09:13:12 +03:30
parent aa81da6477
commit b29bf380ce
2 changed files with 66 additions and 3 deletions
+66 -1
View File
@@ -6,6 +6,8 @@ import { Order } from 'src/modules/order/entities/order.entity';
import { NotificationService } from 'src/modules/notification/services/notification.service';
import { NotifChannelEnum, NotifTitle } from 'src/modules/notification/interfaces/notification.interface';
import { ChatCreatedByAdminEvent } from '../events/chat.events';
import { PermissionEnum } from 'src/common/enums/permission.enum';
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
@Injectable()
export class ChatListeners {
@@ -14,7 +16,8 @@ export class ChatListeners {
constructor(
private readonly em: EntityManager,
private readonly notificationService: NotificationService,
) {}
private readonly adminRepository: AdminRepository,
) { }
@OnEvent(ChatCreatedByAdminEvent.name)
async handleChatCreatedByAdmin(event: ChatCreatedByAdminEvent) {
@@ -57,6 +60,46 @@ export class ChatListeners {
}
}
@OnEvent(ChatCreatedByAdminEvent.name)
async handleChatCreatedByUser(event: ChatCreatedByAdminEvent) {
try {
this.logger.log(
`Chat created by admin: refId=${event.refId}, chatId=${event.chatId}`,
);
const adminIds = await this.findAdminByRefId(event.refId);
if (adminIds.length === 0) {
this.logger.warn(
`No admins found for refId=${event.refId}, skipping notification`,
);
return;
}
const body = 'پیام جدید از پشتیبانی برای شما ارسال شده است.';
await this.notificationService.sendNotification({
channels: [NotifChannelEnum.SMS],
recipients: adminIds.map(id => ({ adminId: id })),
message: {
title: NotifTitle.NEW_TICKET,
content: body,
sms: {
templateId: '',
parameters: {},
},
},
});
this.logger.log(
`Notification sent to user ${adminIds} for admin chat on refId=${event.refId}`,
);
} catch (error) {
this.logger.error(
`Failed to send notification for chat created by admin: refId=${event.refId}`,
error instanceof Error ? error.stack : String(error),
);
}
}
/**
* Find the user associated with refId: first search in requests, then in orders.
*/
@@ -81,4 +124,26 @@ export class ChatListeners {
return null;
}
private async findAdminByRefId(refId: string): Promise<string[]> {
const admins = await this.adminRepository.findAdminsWithPermission(
PermissionEnum.NEW_REQUEST_NOTIFICATION,
);
if (admins.length) {
return admins.map(admin => admin.id);
}
const order = await this.em.findOne(
Order,
{ id: refId },
{ populate: ['designer'], fields: ['id', 'designer'] },
);
if (order?.designer?.id) {
return [order?.designer?.id]
}
return [];
}
}
@@ -13,8 +13,6 @@ export enum NotifTitle {
NEW_TICKET = 'newTicket',
NEW_ORDER_MESSAGE = 'newOrderMessage',
NEW_REQUEST_MESSAGE = 'newRequestMessage',
NEW_REQUEST_COMMENT = 'newRequestComment',
NEW_REQUEST_LIKE = 'newRequestLike',
}
export type recipientType = { userId: string } | { adminId: string };