feat: Add an admin endpoint to retrieve the total count of unread support messages.

This commit is contained in:
pradeepkumar
2026-03-19 04:19:55 +05:30
parent c3a3df704e
commit 4d5a3fc36d
4 changed files with 73 additions and 6 deletions

View File

@@ -9,6 +9,8 @@ import {
UseGuards,
HttpCode,
HttpStatus,
Inject,
forwardRef,
} from '@nestjs/common';
import {
ApiTags,
@@ -24,13 +26,18 @@ import { JwtAuthGuard } from '../auth/guards';
import { CurrentUser } from '../auth/decorators';
import { Roles } from '../auth/decorators/roles.decorator';
import { UserRole, SupportChatStatus } from '@prisma/client';
import { MessagesGateway } from '../messages/messages.gateway';
@ApiTags('Support Chat')
@Controller('support-chat')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth('JWT-auth')
export class SupportChatController {
constructor(private readonly supportChatService: SupportChatService) {}
constructor(
private readonly supportChatService: SupportChatService,
@Inject(forwardRef(() => MessagesGateway))
private readonly messagesGateway: MessagesGateway,
) {}
// ==========================================
// GET OR CREATE SUPPORT CHAT (User/Agent)
@@ -95,7 +102,24 @@ export class SupportChatController {
await this.supportChatService.getChat(chatId, userId, isAdmin);
const senderRole = role === UserRole.ADMIN ? 'ADMIN' : 'USER';
return this.supportChatService.sendMessage(chatId, userId, senderRole, dto.content);
const message = await this.supportChatService.sendMessage(chatId, userId, senderRole, dto.content);
// Emit WebSocket events for real-time updates
this.messagesGateway.server.to(`support:${chatId}`).emit('support_new_message', message);
// If user/agent sent the message, notify admins about unread count
if (senderRole !== 'ADMIN') {
const unreadTotal = await this.supportChatService.getAdminUnreadTotal();
this.messagesGateway.server.to('admin_room').emit('support_unread_update', { unreadTotal });
}
// If admin sent the message, notify the user directly
if (senderRole === 'ADMIN') {
const chat = await this.supportChatService.getChat(chatId, userId, true);
this.messagesGateway.sendToUser(chat.userId, 'support_new_message', message);
}
return message;
}
// ==========================================
@@ -109,7 +133,26 @@ export class SupportChatController {
@Param('id') chatId: string,
@CurrentUser('role') role: UserRole,
) {
return this.supportChatService.markAsRead(chatId, role);
const result = await this.supportChatService.markAsRead(chatId, role);
// If admin marked as read, update sidebar badge for all admins
if (role === UserRole.ADMIN) {
const unreadTotal = await this.supportChatService.getAdminUnreadTotal();
this.messagesGateway.server.to('admin_room').emit('support_unread_update', { unreadTotal });
}
return result;
}
// ==========================================
// ADMIN: GET TOTAL UNREAD COUNT
// ==========================================
@Get('admin/unread-total')
@Roles(UserRole.ADMIN)
@ApiOperation({ summary: 'Admin: Get total unread support message count' })
@ApiResponse({ status: 200, description: 'Total unread count returned' })
async getUnreadTotal() {
return this.supportChatService.getAdminUnreadTotal();
}
// ==========================================