import { Request, Response } from 'express'; import { DyFM_HttpCallType } from '@futdevpro/fsm-dynamo'; import { DyFM_msgModule_settings, DyFM_Msg_Message, DyFM_Msg_Conversation, DyFM_Msg_Status, DyFM_Msg_Reaction, } from '@futdevpro/fsm-dynamo/messaging'; import { DyNTS_Endpoint_Params } from '../../../_models/control-models/endpoint-params.control-model'; import { DyNTS_AuthService } from '../../../_services/core/auth.service'; import { DyNTS_GlobalService } from '../../../_services/core/global.service'; import { DyNTS_Controller } from '../../../_services/route/controller.service'; import { DyNTS_Msg_Message_DataService } from './msg-message.data-service'; import { DyNTS_Msg_Conversation_DataService } from './msg-conversation.data-service'; import { DyNTS_Msg_Events_Service } from './msg-events.service'; import { DyNTS_Msg_Main_ControlService } from './msg-main.control-service'; /** * Messaging Controller * Handles HTTP endpoints for messaging operations */ export class DyNTS_Msg_Controller extends DyNTS_Controller { static getInstance(): DyNTS_Msg_Controller { return DyNTS_Msg_Controller.getSingletonInstance(); } private authService: DyNTS_AuthService; private eventsService = DyNTS_Msg_Events_Service.getInstance(); private msgControlService = DyNTS_Msg_Main_ControlService.getInstance(); setupEndpoints(): void { try { this.authService = DyNTS_GlobalService.getAuthService(); } catch (error) { // Auth service not available - use default auth service const { DyNTS_Default_AuthService } = require('../defaults/_services/default-auth.service'); this.authService = DyNTS_Default_AuthService.getInstance(); } this.endpoints = [ // Get user's conversations new DyNTS_Endpoint_Params({ name: 'getConversations', type: DyFM_HttpCallType.get, endpoint: DyFM_msgModule_settings.endPoints.getConversations, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const userId = this.authService.getIssuerFromRequest(req); const conversationService = new DyNTS_Msg_Conversation_DataService({ issuer, }); const conversations = await conversationService.getUserConversations(userId); res.send(conversations); }, ], }), // Get single conversation new DyNTS_Endpoint_Params({ name: 'getConversation', type: DyFM_HttpCallType.get, endpoint: DyFM_msgModule_settings.endPoints.getConversation, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const conversationService = new DyNTS_Msg_Conversation_DataService({ issuer, }); await conversationService.getDataById(req.params.conversationId); res.send(conversationService.data); }, ], }), // Create conversation new DyNTS_Endpoint_Params({ name: 'createConversation', type: DyFM_HttpCallType.post, endpoint: DyFM_msgModule_settings.endPoints.createConversation, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const creatorId = this.authService.getIssuerFromRequest(req); const conversation = await this.msgControlService.createConversation( req.body, creatorId, issuer ); res.status(201).send(conversation); }, ], }), // Get messages for conversation new DyNTS_Endpoint_Params({ name: 'getMessages', type: DyFM_HttpCallType.get, endpoint: DyFM_msgModule_settings.endPoints.getMessages, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const conversationId = req.params.conversationId; const limit = req.query.limit ? parseInt(req.query.limit as string) : undefined; const skip = req.query.skip ? parseInt(req.query.skip as string) : undefined; const messageService = new DyNTS_Msg_Message_DataService({ issuer, }); const messages = await messageService.getConversationMessages( conversationId, limit, skip ); res.send(messages); }, ], }), // Send message new DyNTS_Endpoint_Params({ name: 'sendMessage', type: DyFM_HttpCallType.post, endpoint: DyFM_msgModule_settings.endPoints.sendMessage, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const conversationId = req.params.conversationId; const senderId = this.authService.getIssuerFromRequest(req); const message = await this.msgControlService.sendMessage( conversationId, req.body, senderId, issuer ); res.status(201).send(message); }, ], }), // Edit message new DyNTS_Endpoint_Params({ name: 'editMessage', type: DyFM_HttpCallType.patch, endpoint: DyFM_msgModule_settings.endPoints.editMessage, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const messageId = req.params.messageId; const userId = this.authService.getIssuerFromRequest(req); const message = await this.msgControlService.editMessage( messageId, req.body.content, userId, issuer ); res.send(message); }, ], }), // Delete message new DyNTS_Endpoint_Params({ name: 'deleteMessage', type: DyFM_HttpCallType.delete, endpoint: DyFM_msgModule_settings.endPoints.deleteMessage, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const messageId = req.params.messageId; const userId = this.authService.getIssuerFromRequest(req); await this.msgControlService.deleteMessage( messageId, userId, issuer ); res.send({ success: true }); }, ], }), // Mark messages as read new DyNTS_Endpoint_Params({ name: 'markAsRead', type: DyFM_HttpCallType.post, endpoint: DyFM_msgModule_settings.endPoints.markAsRead, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const userId = this.authService.getIssuerFromRequest(req); const messageIds: string[] = req.body.messageIds || []; const result = await this.msgControlService.markMessagesAsRead( messageIds, userId, issuer ); res.send(result); }, ], }), // Add reaction new DyNTS_Endpoint_Params({ name: 'addReaction', type: DyFM_HttpCallType.post, endpoint: DyFM_msgModule_settings.endPoints.addReaction, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const messageId = req.params.messageId; const { emoji } = req.body; const userId = this.authService.getIssuerFromRequest(req); const message = await this.msgControlService.addReaction( messageId, emoji, userId, issuer ); res.send(message); }, ], }), // Remove reaction new DyNTS_Endpoint_Params({ name: 'removeReaction', type: DyFM_HttpCallType.delete, endpoint: DyFM_msgModule_settings.endPoints.removeReaction, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const messageId = req.params.messageId; const { emoji } = req.params; const userId = this.authService.getIssuerFromRequest(req); const message = await this.msgControlService.removeReaction( messageId, emoji, userId, issuer ); res.send(message); }, ], }), // Update conversation new DyNTS_Endpoint_Params({ name: 'updateConversation', type: DyFM_HttpCallType.patch, endpoint: DyFM_msgModule_settings.endPoints.updateConversation, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const conversationId = req.params.conversationId; const userId = this.authService.getIssuerFromRequest(req); const conversation = await this.msgControlService.updateConversation( conversationId, req.body, userId, issuer ); res.send(conversation); }, ], }), // Delete conversation new DyNTS_Endpoint_Params({ name: 'deleteConversation', type: DyFM_HttpCallType.delete, endpoint: DyFM_msgModule_settings.endPoints.deleteConversation, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const conversationId = req.params.conversationId; const userId = this.authService.getIssuerFromRequest(req); await this.msgControlService.deleteConversation( conversationId, userId, issuer ); res.send({ success: true }); }, ], }), // Add participant new DyNTS_Endpoint_Params({ name: 'addParticipant', type: DyFM_HttpCallType.post, endpoint: DyFM_msgModule_settings.endPoints.addParticipant, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const conversationId = req.params.conversationId; const { userId: newUserId, role = 'member' } = req.body; const requesterId = this.authService.getIssuerFromRequest(req); await this.msgControlService.addParticipant( conversationId, newUserId, role, requesterId, issuer ); res.send({ success: true }); }, ], }), // Remove participant new DyNTS_Endpoint_Params({ name: 'removeParticipant', type: DyFM_HttpCallType.delete, endpoint: DyFM_msgModule_settings.endPoints.removeParticipant, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const conversationId = req.params.conversationId; const userIdToRemove = req.params.userId; const requesterId = this.authService.getIssuerFromRequest(req); await this.msgControlService.removeParticipant( conversationId, userIdToRemove, requesterId, issuer ); res.send({ success: true }); }, ], }), // Get agent process new DyNTS_Endpoint_Params({ name: 'getAgentProcess', type: DyFM_HttpCallType.get, endpoint: DyFM_msgModule_settings.endPoints.getAgentProcess, tasks: [ async (req: Request, res: Response, issuer: string): Promise => { const messageId = req.params.messageId; const agentProcessSteps = await this.msgControlService.getAgentProcessSteps( messageId, issuer ); res.send(agentProcessSteps); }, ], }), ]; } }