Ejemplo n.º 1
0
        public async Task <ActionResult> AgentSummary()
        {
            try
            {
                var onlineEmails = NotificationHub.OnlineAgents();
                var KeyValues    = NotificationHub.GetConnections();
                var Campaigns    = await db.Campaigns.Where(x => !x.IsDeleted).ToListAsync();

                var entities = await db.Users.Where(x => !x.IsDeleted && x.Roles.Any(y => y.RoleId.Equals("2") || y.RoleId.Equals("3"))).ToListAsync();

                var onlineUsers = onlineEmails.Select(c => entities.Where(x => x.Email.Equals(c)).FirstOrDefault()).Where(z => z != null).ToList();


                var onlineModel = KeyValues.Select(c => new OnlineUser
                {
                    User           = entities.FirstOrDefault(x => x.Email.Equals(c.UserName)),
                    Campaign       = Campaigns.FirstOrDefault(x => x.Id == c.CampaignId),
                    ConnectionTime = (DateTime.Now - db.UsersHistory.FirstOrDefault(x => x.Id == c.UserHistoryId)?.LoggedIn) ?? TimeSpan.FromSeconds(0)
                }
                                                   ).Where(z => z != null).ToList();


                var offlineUsers = entities.Except(onlineUsers).ToList();

                var model = new UserHistoryModel
                {
                    OnlineUsers  = onlineModel.Where(x => x.User != null).ToList(),
                    OfflineUsers = offlineUsers
                };

                return(View(model));
            }
            catch (Exception e)
            {
                AddAlert($"Oops! something went wrong. Error code: {e.HResult}", "Index", this.GetType().ToString(), AlertType.error, e);
                return(View());
            }
        }