71 lines
3.1 KiB
JavaScript
71 lines
3.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.CreateChatCommandHandler = void 0;
|
|
const ChatAggregate_1 = require("../../../Domain/Chat/ChatAggregate");
|
|
const UserAggregate_1 = require("../../../Domain/User/UserAggregate");
|
|
const Logger_1 = require("../../Services/Logger");
|
|
class CreateChatCommandHandler {
|
|
constructor(chatRepository, userRepository) {
|
|
this.chatRepository = chatRepository;
|
|
this.userRepository = userRepository;
|
|
}
|
|
async execute(command) {
|
|
try {
|
|
// Validate creator exists
|
|
const creator = await this.userRepository.findById(command.createdBy);
|
|
if (!creator) {
|
|
throw new Error('Creator not found');
|
|
}
|
|
// For group chats, check if creator is premium
|
|
if (command.type === 'group' && creator.state !== UserAggregate_1.UserState.VERIFIED_PREMIUM) {
|
|
throw new Error('Premium subscription required to create groups');
|
|
}
|
|
// Validate all target users exist
|
|
const targetUsers = await Promise.all(command.userIds.map(id => this.userRepository.findById(id)));
|
|
if (targetUsers.some(user => !user)) {
|
|
throw new Error('One or more target users not found');
|
|
}
|
|
// For direct chats, check if already exists
|
|
if (command.type === 'direct' && command.userIds.length === 1) {
|
|
const existingChats = await this.chatRepository.findByUserId(command.createdBy);
|
|
const existingDirectChat = existingChats.find(chat => chat.type === ChatAggregate_1.ChatType.DIRECT &&
|
|
chat.users.length === 2 &&
|
|
chat.users.includes(command.userIds[0]));
|
|
if (existingDirectChat) {
|
|
return existingDirectChat;
|
|
}
|
|
}
|
|
// For game chats, check if already exists
|
|
if (command.type === 'game' && command.gameId) {
|
|
const existingGameChat = await this.chatRepository.findByGameId(command.gameId);
|
|
if (existingGameChat) {
|
|
return existingGameChat;
|
|
}
|
|
}
|
|
// Create chat
|
|
const chatData = {
|
|
type: command.type,
|
|
name: command.name,
|
|
gameId: command.gameId,
|
|
createdBy: command.createdBy,
|
|
users: [command.createdBy, ...command.userIds],
|
|
messages: [],
|
|
lastActivity: new Date()
|
|
};
|
|
const chat = await this.chatRepository.create(chatData);
|
|
(0, Logger_1.logAuth)('Chat created successfully', command.createdBy, {
|
|
chatId: chat.id,
|
|
chatType: command.type,
|
|
participantCount: chat.users.length,
|
|
gameId: command.gameId
|
|
});
|
|
return chat;
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('CreateChatCommandHandler error', error);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
exports.CreateChatCommandHandler = CreateChatCommandHandler;
|
|
//# sourceMappingURL=CreateChatCommandHandler.js.map
|