Files
SerpentRace/SerpentRace_Backend/src/Application/Chat/queries/ChatHistoryQueryHandlers.ts
T

142 lines
4.8 KiB
TypeScript

import { GetChatHistoryQuery, GetArchivedChatsQuery } from './ChatQueries';
import { IChatRepository } from '../../../Domain/IRepository/IChatRepository';
import { IChatArchiveRepository } from '../../../Domain/IRepository/IChatArchiveRepository';
import { Message } from '../../../Domain/Chat/ChatAggregate';
import { logAuth, logError, logWarning } from '../../Services/Logger';
interface ChatHistoryResult {
chatId: string;
messages: Message[];
isArchived: boolean;
chatInfo: {
type: string;
name: string | null;
gameId: string | null;
users: string[];
};
}
export class GetChatHistoryQueryHandler {
constructor(
private chatRepository: IChatRepository,
private chatArchiveRepository: IChatArchiveRepository
) {}
async execute(query: GetChatHistoryQuery): Promise<ChatHistoryResult | null> {
try {
// First try to find active chat
const chat = await this.chatRepository.findById(query.chatId);
if (chat) {
// Check authorization
if (!chat.users.includes(query.userId)) {
logWarning('Unauthorized chat history access attempt', {
chatId: query.chatId,
userId: query.userId
});
return null;
}
logAuth('Chat history retrieved', query.userId, {
chatId: query.chatId,
messageCount: chat.messages.length,
isArchived: false
});
return {
chatId: query.chatId,
messages: chat.messages,
isArchived: false,
chatInfo: {
type: chat.type,
name: chat.name,
gameId: chat.gameId,
users: chat.users
}
};
}
// Try to find in archives
const archives = await this.chatArchiveRepository.findByChatId(query.chatId);
const userArchive = archives.find(archive =>
archive.participants.includes(query.userId)
);
if (userArchive) {
logAuth('Archived chat history retrieved', query.userId, {
chatId: query.chatId,
messageCount: userArchive.archivedMessages.length,
isArchived: true
});
return {
chatId: query.chatId,
messages: userArchive.archivedMessages,
isArchived: true,
chatInfo: {
type: userArchive.chatType,
name: userArchive.chatName,
gameId: userArchive.gameId,
users: userArchive.participants
}
};
}
logWarning('Chat history not found', {
chatId: query.chatId,
userId: query.userId
});
return null;
} catch (error) {
logError('GetChatHistoryQueryHandler error', error as Error);
return null;
}
}
}
export class GetArchivedChatsQueryHandler {
constructor(private chatArchiveRepository: IChatArchiveRepository) {}
async execute(query: GetArchivedChatsQuery): Promise<ChatHistoryResult[]> {
try {
let archives: any[] = [];
if (query.gameId) {
// Get archived game chats
archives = await this.chatArchiveRepository.findByGameId(query.gameId);
} else {
// Get all archived chats for user (would need different query)
// For now, return empty - this would need a new repository method
archives = [];
}
const result = archives
.filter(archive => archive.participants.includes(query.userId))
.map(archive => ({
chatId: archive.chatId,
messages: archive.archivedMessages,
isArchived: true,
chatInfo: {
type: archive.chatType,
name: archive.chatName,
gameId: archive.gameId,
users: archive.participants
}
}));
logAuth('Archived chats retrieved', query.userId, {
count: result.length,
gameId: query.gameId
});
return result;
} catch (error) {
logError('GetArchivedChatsQueryHandler error', error as Error);
return [];
}
}
}