Esempio n. 1
0
 public async Task GetPollVotedUsers()
 {
     var chat = fillTestDbHelper.Chats.FirstOrDefault();
     var chatUser = chat.ChatUsers.FirstOrDefault();
     var poll = new PollDto
     {
         ConversationId = chat.Id,
         ConversationType = ConversationType.Chat,
         Title = "Poll",
         CreatorId = chatUser.UserId,
         Options = new List<PollOptionDto>
         {
             new PollOptionDto
             {
                 Description = "desc",
                 OptionId = 1
             },
             new PollOptionDto
             {
                 Description = "desc 2",
                 OptionId = 2
             }
         }
     };
     poll = await pollsService.SavePollAsync(poll);
     poll = await pollsService.VotePollAsync(poll.PollId, poll.ConversationId, poll.ConversationType, new List<byte> { 1 }, chatUser.UserId);
     var votedUsers = await pollsService.GetPollVotedUsersAsync(poll.PollId, poll.ConversationId, poll.ConversationType, 1, chatUser.UserId);
     Assert.True(votedUsers.Count == 1 && votedUsers.Any(opt => opt.FirstValue.Id == chatUser.UserId));           
 }
        public async Task <Response> CreateResponseAsync()
        {
            try
            {
                var pollResults = await pollsService.GetPollVotedUsersAsync(
                    request.PollId,
                    request.ConversationId,
                    request.ConversationType,
                    request.OptionId,
                    clientConnection.UserId.GetValueOrDefault(),
                    30,
                    request.NavigationUserId.GetValueOrDefault()).ConfigureAwait(false);

                var         usersGroups   = pollResults.GroupBy(opt => opt.FirstValue.NodeId);
                List <Task> getUsersTasks = new List <Task>();
                var         resultUsers   = new ConcurrentBag <VoteInfo>();
                foreach (var group in usersGroups)
                {
                    var nodeConnection = connectionsService.GetNodeConnection(group.Key.GetValueOrDefault());
                    if (nodeConnection != null)
                    {
                        await MetricsHelper.Instance.SetCrossNodeApiInvolvedAsync(request.RequestId).ConfigureAwait(false);

                        getUsersTasks.Add(Task.Run(async() =>
                        {
                            var usersInfo = await nodeRequestSender.GetUsersInfoAsync(group.Select(opt => opt.FirstValue.Id).ToList(), clientConnection.UserId, nodeConnection).ConfigureAwait(false);
                            resultUsers.AddRange(usersInfo.Select(user => new VoteInfo(user, pollResults.FirstOrDefault(opt => opt.FirstValue.Id == user.Id).SecondValue)));
                        }));
                    }
                    else if (group.Key == NodeSettings.Configs.Node.Id)
                    {
                        var users = UserConverter.GetUsersVm(group.Select(opt => opt.FirstValue).ToList(), clientConnection.UserId);
                        resultUsers.AddRange(users.Select(user => new VoteInfo(user, pollResults.FirstOrDefault(opt => opt.FirstValue.Id == user.Id).SecondValue)));
                    }
                }
                await Task.WhenAll(getUsersTasks).ConfigureAwait(false);

                return(new PollResultsResponse(request.RequestId, resultUsers.OrderBy(opt => opt.User.Id)));
            }
            catch (PermissionDeniedException ex)
            {
                Logger.WriteLog(ex);
                return(new ResultResponse(request.RequestId, "User does not have access to voted users list.", ErrorCode.PermissionDenied));
            }
        }