"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GetUserChatsQueryHandler = void 0; const Logger_1 = require("../../Services/Logger"); class GetUserChatsQueryHandler { constructor(chatRepository, chatArchiveRepository) { this.chatRepository = chatRepository; this.chatArchiveRepository = chatArchiveRepository; } async execute(query) { try { const result = []; // Get active chats const activeChats = await this.chatRepository.findActiveChatsForUser(query.userId); result.push(...activeChats.map(chat => ({ id: chat.id, type: chat.type, name: chat.name, gameId: chat.gameId, users: chat.users, lastActivity: chat.lastActivity, isArchived: false, messageCount: chat.messages.length, unreadCount: this.calculateUnreadMessages(chat, query.userId) }))); // Get archived chats if requested if (query.includeArchived) { const userActiveChats = await this.chatRepository.findByUserId(query.userId); const archivedChatIds = userActiveChats .filter(chat => chat.archiveDate !== null) .map(chat => chat.id); const archives = await Promise.all(archivedChatIds.map(id => this.chatArchiveRepository.findByChatId(id))); archives.forEach(archiveArray => { archiveArray.forEach(archive => { if (archive.participants.includes(query.userId)) { result.push({ id: archive.chatId, type: archive.chatType, name: archive.chatName, gameId: archive.gameId, users: archive.participants, lastActivity: archive.archivedAt, isArchived: true, messageCount: archive.archivedMessages.length, unreadCount: 0 // Archived chats have no unread messages }); } }); }); } (0, Logger_1.logAuth)('User chats retrieved', query.userId, { activeCount: activeChats.length, totalCount: result.length, includeArchived: query.includeArchived }); return result.sort((a, b) => { if (!a.lastActivity) return 1; if (!b.lastActivity) return -1; return new Date(b.lastActivity).getTime() - new Date(a.lastActivity).getTime(); }); } catch (error) { (0, Logger_1.logError)('GetUserChatsQueryHandler error', error); return []; } } calculateUnreadMessages(chat, userId) { // Simple implementation - count messages from other users // In production, you'd store lastSeen timestamp per user per chat return chat.messages.filter(msg => msg.userid !== userId).length; } } exports.GetUserChatsQueryHandler = GetUserChatsQueryHandler; //# sourceMappingURL=GetUserChatsQueryHandler.js.map