feat: implement support chat functionality with new module, service, controller, DTOs, and schema updates.

This commit is contained in:
pradeepkumar
2026-03-05 06:37:07 +05:30
parent c1fa1f4aab
commit 399b8aa937
10 changed files with 530 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ import { Server, Socket } from 'socket.io';
import { JwtService } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { MessagesService } from './messages.service';
import { SupportChatService } from '../support-chat/support-chat.service';
import { CreateMessageDto } from './dto';
import { Logger } from '@nestjs/common';
@@ -35,6 +36,7 @@ export class MessagesGateway implements OnGatewayConnection, OnGatewayDisconnect
constructor(
private readonly messagesService: MessagesService,
private readonly supportChatService: SupportChatService,
private readonly jwtService: JwtService,
private readonly configService: ConfigService,
) {}
@@ -266,6 +268,103 @@ export class MessagesGateway implements OnGatewayConnection, OnGatewayDisconnect
}
}
// ==========================================
// SUPPORT CHAT EVENTS
// ==========================================
/**
* Join a support chat room
*/
@SubscribeMessage('support_join')
async handleSupportJoin(
@ConnectedSocket() client: AuthenticatedSocket,
@MessageBody() data: { chatId: string },
) {
if (!client.userId) {
return { error: 'Unauthorized' };
}
try {
const isAdmin = client.userRole === 'ADMIN';
await this.supportChatService.getChat(data.chatId, client.userId, isAdmin);
client.join(`support:${data.chatId}`);
this.logger.log(`User ${client.userId} joined support chat ${data.chatId}`);
return { success: true };
} catch (error) {
return { error: error.message };
}
}
/**
* Leave a support chat room
*/
@SubscribeMessage('support_leave')
handleSupportLeave(
@ConnectedSocket() client: AuthenticatedSocket,
@MessageBody() data: { chatId: string },
) {
client.leave(`support:${data.chatId}`);
this.logger.log(`User ${client.userId} left support chat ${data.chatId}`);
return { success: true };
}
/**
* Send a message in a support chat via WebSocket
*/
@SubscribeMessage('support_send_message')
async handleSupportSendMessage(
@ConnectedSocket() client: AuthenticatedSocket,
@MessageBody() data: { chatId: string; content: string },
) {
if (!client.userId) {
return { error: 'Unauthorized' };
}
try {
const senderRole = client.userRole === 'ADMIN' ? 'ADMIN' : 'USER';
const message = await this.supportChatService.sendMessage(
data.chatId,
client.userId,
senderRole,
data.content,
);
// Broadcast to all clients in the support chat room
this.server.to(`support:${data.chatId}`).emit('support_new_message', message);
return { success: true, message };
} catch (error) {
return { error: error.message };
}
}
/**
* Support chat typing indicators
*/
@SubscribeMessage('support_typing_start')
handleSupportTypingStart(
@ConnectedSocket() client: AuthenticatedSocket,
@MessageBody() data: { chatId: string },
) {
if (!client.userId) return;
client.to(`support:${data.chatId}`).emit('support_typing_start', {
userId: client.userId,
chatId: data.chatId,
});
}
@SubscribeMessage('support_typing_stop')
handleSupportTypingStop(
@ConnectedSocket() client: AuthenticatedSocket,
@MessageBody() data: { chatId: string },
) {
if (!client.userId) return;
client.to(`support:${data.chatId}`).emit('support_typing_stop', {
userId: client.userId,
chatId: data.chatId,
});
}
/**
* Broadcast user online/offline status
*/

View File

@@ -5,10 +5,12 @@ import { MessagesController } from './messages.controller';
import { MessagesService } from './messages.service';
import { MessagesGateway } from './messages.gateway';
import { PrismaModule } from '../prisma/prisma.module';
import { SupportChatModule } from '../support-chat/support-chat.module';
@Module({
imports: [
PrismaModule,
SupportChatModule,
ConfigModule,
JwtModule.registerAsync({
imports: [ConfigModule],