import { DyFM_Error, DyFM_DBFilter } from '@futdevpro/fsm-dynamo'; import { DyFM_Msg_Conversation, DyFM_msgConversation_dataParams, DyFM_Msg_Participant, } from '@futdevpro/fsm-dynamo/messaging'; import { DyNTS_DataService } from '../../../_services/base/data.service'; import { DyNTS_global_settings } from '../../../_collections/global-settings.const'; /** * Conversation Data Service * Handles CRUD operations for conversations */ export class DyNTS_Msg_Conversation_DataService extends DyNTS_DataService { constructor( set: { conversation?: DyFM_Msg_Conversation; issuer: string; } ) { super( new DyFM_Msg_Conversation(set?.conversation), DyFM_msgConversation_dataParams, set.issuer ); } /** * Get conversations for a user */ async getUserConversations(userId: string): Promise { try { await this.dataDBService .find({ 'participants.userId': userId, __deleted: { $exists: false }, } as DyFM_DBFilter) .then((res: DyFM_Msg_Conversation[]) => { this.dataList = res.sort((a, b) => (b.lastMessageAt?.getTime() || 0) - (a.lastMessageAt?.getTime() || 0)); }); return this.dataList; } catch (error) { throw new DyFM_Error({ ...this.getDefaultErrorSettings('getUserConversations', error), errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-MSG-GUC`, userMessage: 'Failed to retrieve user conversations.', }); } } /** * Add participant to conversation */ async addParticipant( conversationId: string, participant: DyFM_Msg_Participant ): Promise { try { const conversation = await this.getDataById(conversationId); if (!conversation) { throw new Error('Conversation not found'); } if (!conversation.participants) { conversation.participants = []; } // Check if participant already exists const exists = conversation.participants.some( p => p.userId === participant.userId ); if (!exists) { conversation.participants.push(participant); await this.saveData(conversation); } } catch (error) { throw new DyFM_Error({ ...this.getDefaultErrorSettings('addParticipant', error), errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-MSG-AP`, userMessage: 'Failed to add participant to conversation.', }); } } /** * Remove participant from conversation */ async removeParticipant( conversationId: string, userId: string ): Promise { try { const conversation = await this.getDataById(conversationId); if (!conversation) { throw new Error('Conversation not found'); } conversation.participants = conversation.participants?.filter( p => p.userId !== userId ) || []; await this.saveData(conversation); } catch (error) { throw new DyFM_Error({ ...this.getDefaultErrorSettings('removeParticipant', error), errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-MSG-RP`, userMessage: 'Failed to remove participant from conversation.', }); } } /** * Update last message info */ async updateLastMessage( conversationId: string, messageId: string, messagePreview: string ): Promise { try { const conversation = await this.getDataById(conversationId); if (!conversation) { throw new Error('Conversation not found'); } conversation.lastMessageId = messageId; conversation.lastMessageAt = new Date(); conversation.lastMessagePreview = messagePreview; await this.saveData(conversation); } catch (error) { throw new DyFM_Error({ ...this.getDefaultErrorSettings('updateLastMessage', error), errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-MSG-ULM`, userMessage: 'Failed to update conversation last message.', }); } } }