public UpdateRecipientsResponse UpdateRecipients(UpdateRecipientsRequest request) { VerifyPermission(Permissions.Send, Modules.MassMessageTool); request.IpAddress = HttpContext.Current.Request.UserHostAddress; var validationResult = _massMessageQueries.GetValidationResult(request); if (!validationResult.IsValid) { return(ValidationErrorResponse <UpdateRecipientsResponse>(validationResult)); } var response = _massMessageCommands.UpdateRecipients(request); return(response); }
public ValidationResult GetValidationResult(UpdateRecipientsRequest request) { return(new UpdateMassMessageRecipientsValidator(_messagingRepository).Validate(request)); }
public UpdateRecipientsResponse UpdateRecipients(UpdateRecipientsRequest request) { var validationResult = _massMessageQueries.GetValidationResult(request); if (!validationResult.IsValid) { throw new RegoValidationException(validationResult); } using (var scope = CustomTransactionScope.GetTransactionScope()) { if (!request.Id.HasValue) { DeleteUnsentMassMessages(); } MassMessage massMessage; if (request.Id.HasValue) { massMessage = _messagingRepository.MassMessages .Include(x => x.Recipients.Select(y => y.Language)) .Single(x => x.Id == request.Id); } else { massMessage = new MassMessage { Id = Guid.NewGuid(), AdminId = _actorInfoProvider.Actor.Id }; _messagingRepository.MassMessages.Add(massMessage); } massMessage.IpAddress = request.IpAddress; if (request.UpdateRecipientsType == UpdateRecipientsType.SelectSingle) { massMessage.Recipients.Add(_messagingRepository.Players .Include(x => x.Language) .Single(x => x.Id == request.PlayerId)); } else if (request.UpdateRecipientsType == UpdateRecipientsType.UnselectSingle) { massMessage.Recipients.Remove(massMessage.Recipients.Single(x => x.Id == request.PlayerId)); } else if (request.UpdateRecipientsType == UpdateRecipientsType.RecipientsListUnselectAll) { massMessage.Recipients.Clear(); } else { var playerIds = _massMessageQueries.CreateMassMessagePlayerQuery(request.SearchPlayersRequest) .Select(x => x.Id); if (request.UpdateRecipientsType == UpdateRecipientsType.SearchResultSelectAll) { var players = _messagingRepository.Players .Include(x => x.Language) .Where(x => playerIds.Contains(x.Id)); foreach (var player in players) { massMessage.Recipients.Add(player); } } else { var recipients = massMessage.Recipients.Where(x => playerIds.Contains(x.Id)).ToArray(); foreach (var recipient in recipients) { massMessage.Recipients.Remove(recipient); } } } _messagingRepository.SaveChanges(); scope.Complete(); IEnumerable <Interface.Data.Language> languages = null; if (massMessage.Recipients.Any()) { var languageEntities = massMessage.Recipients.GroupBy(x => x.LanguageCode).Select(x => x.First().Language); languages = Mapper.Map <List <Interface.Data.Language> >(languageEntities); } return(new UpdateRecipientsResponse { Id = massMessage.Id, HasRecipients = massMessage.Recipients.Any(), Languages = languages }); } }