public async Task ExecuteAsync(RequestTutorCommand message, CancellationToken token) { Tutor?tutor = null; if (message.TutorId.HasValue) { if (message.UserId == message.TutorId.Value) { throw new ArgumentException("You cannot request tutor to yourself"); } tutor = await _tutorRepository.LoadAsync(message.TutorId.Value, token); } var user = await _userRepository.LoadAsync(message.UserId, token); var lead = new Lead(message.Course, message.LeadText, message.Referer, user, tutor, message.UtmSource); await _leadRepository.AddAsync(lead, token); var tutorsIds = new List <long>(); if (message.MoreTutors) { var needToSendToMoreTutors = await _leadRepository.NeedToSendMoreTutorsAsync(message.UserId, token); if (needToSendToMoreTutors) { var t = await _tutorRepository.GetTutorsByCourseAsync(message.Course, message.UserId, user.Country, token); tutorsIds.AddRange(t); } } if (tutor != null) { tutorsIds.Add(tutor.Id); } foreach (var userId in tutorsIds.Distinct()) { var users = new[] { userId, message.UserId }; var chatRoom = await _chatRoomRepository.GetOrAddChatRoomAsync(users, token); if (chatRoom.Extra == null) { chatRoom.Extra = new ChatRoomAdmin(chatRoom); } chatRoom.Extra.Lead = lead; await _chatRoomRepository.UpdateAsync(chatRoom, token); var chatMessage = new ChatTextMessage(user, message.ChatText, chatRoom); chatRoom.AddMessage(chatMessage); await _chatRoomRepository.UpdateAsync(chatRoom, token); await _chatMessageRepository.AddAsync(chatMessage, token); } }
public async Task ExecuteAsync(SendChatFileMessageCommand message, CancellationToken token) { var users = message.ToUsersId.ToList(); users.Add(message.UserSendingId); var chatRoom = await _chatRoomRepository.GetChatRoomAsync(users, token); if (chatRoom == null) { chatRoom = new ChatRoom(users.Select(s => _userRepository.Load(s)).ToList()); await _chatRoomRepository.AddAsync(chatRoom, token); } var user = _userRepository.Load(message.UserSendingId); var chatMessage = new ChatAttachmentMessage(user, message.Blob, chatRoom); chatRoom.AddMessage(chatMessage); await _chatRoomRepository.UpdateAsync(chatRoom, token); await _chatMessageRepository.AddAsync(chatMessage, token); var id = chatMessage.Id; await _blobProvider.MoveAsync(message.Blob, $"{chatRoom.Id}/{id}", token); }
public async Task ExecuteAsync(SendChatTextMessageCommand message, CancellationToken token) { //var users = message.ToUsersId.ToList(); //users.Add(message.UserSendingId); var users = new[] { message.ToUsersId, message.UserSendingId }; var chatRoom = await _chatRoomRepository.GetChatRoomAsync(users, token); if (chatRoom == null) { var userSending = await _userRepository.LoadAsync(message.UserSendingId, token); var userReceiving = await _userRepository.LoadAsync(message.ToUsersId, token); if (userReceiving.Tutor == null) { throw new ArgumentException("sending a message not to tutor"); } chatRoom = new ChatRoom(new List <User>() { userSending, userReceiving }); await _chatRoomRepository.AddAsync(chatRoom, token); } var user = _userRepository.Load(message.UserSendingId); var chatMessage = new ChatTextMessage(user, message.Message, chatRoom); chatRoom.AddMessage(chatMessage); await _chatRoomRepository.UpdateAsync(chatRoom, token); await _chatMessageRepository.AddAsync(chatMessage, token); // need this in order to get id from nhibernate }
public async Task ExecuteAsync(ChangeConversationStatusCommand message, CancellationToken token) { var chatRoom = await _repository.GetChatRoomAsync(message.Identifier, token); if (chatRoom.Extra == null) { chatRoom.Extra = new ChatRoomAdmin(chatRoom); } chatRoom.Extra.Status = message.Status; await _repository.UpdateAsync(chatRoom, token); }
public async Task ExecuteAsync(ChangeConversationAssignCommand message, CancellationToken token) { var chatRoom = await _repository.GetChatRoomAsync(message.Identifier, token); if (chatRoom.Extra == null) { chatRoom.Extra = new ChatRoomAdmin(chatRoom); } if (string.IsNullOrEmpty(message.AssignTo)) { chatRoom.Extra.AssignTo = null; } else { chatRoom.Extra.AssignTo = message.AssignTo; } await _repository.UpdateAsync(chatRoom, token); }