Пример #1
0
        public ActionResult SendMessagePOST(ClientDashboardPageViewModel model)
        {
            //Get user id
            var LoggedInUserId = User.Identity.GetUserId();

            /**************************
            * My Associated Calls
            **************************/
            //Get calls
            var AssociatedClientCalls = _context.Call.Where(n => n.Closed == false && n.ForUserId != null && n.ForUserId.Equals(LoggedInUserId));

            //To View Model
            var AssociatedCallsModel = new List <ClientAssociatedCallViewModel>();

            foreach (var call in AssociatedClientCalls)
            {
                AssociatedCallsModel.Add(new ClientAssociatedCallViewModel
                {
                    Reference = call.Reference,
                    Created   = call.Created,
                    Summary   = call.Summary
                });
            }
            model.AssociatedCalls = AssociatedCallsModel;

            /**************************
            * List of Groups
            **************************/
            var Groups = _context.Group;

            model.GroupList = Groups;

            /**************************
            * My Messages
            **************************/
            //Get dismissed alerts
            IQueryable <Alert> Alerts;

            //Get Non dismissed alerts
            Alerts = _context.Alert.Where(n => (n.ToUserId.Equals(LoggedInUserId)) && (n.DismissedWhen == null) && (n.ToGroupId == null));

            //Convert Alerts into View Model Alerts
            List <ClientAlertsViewModel> AlertsVM = new List <ClientAlertsViewModel>();

            using (ApplicationDbContext dbcontext = new ApplicationDbContext())
            {
                ApplicationUser FromUser, ToUser, DismissedByUser;
                Group           FromGroup;

                foreach (var item in Alerts)
                {
                    FromUser        = dbcontext.Users.SingleOrDefault(n => n.Id.Equals(item.FromUserId));
                    FromGroup       = dbcontext.Group.SingleOrDefault(n => n.Id == item.FromGroupId);
                    ToUser          = dbcontext.Users.SingleOrDefault(n => n.Id.Equals(item.ToUserId));
                    DismissedByUser = dbcontext.Users.SingleOrDefault(n => n.Id.Equals(item.DismissedByUserId));

                    AlertsVM.Add(new ClientAlertsViewModel()
                    {
                        Id                    = item.Id,
                        FromUserId            = item.FromUserId,
                        FromUserName          = FromUser?.UserName,
                        FromGroupId           = item.FromGroupId,
                        FromGroupName         = FromGroup?.Name,
                        ToUserId              = item.ToUserId,
                        ToUserName            = ToUser?.UserName,
                        ToGroupId             = null,
                        ToGroupName           = null,
                        DismissedByUserId     = item.DismissedByUserId,
                        DismissedByUserName   = DismissedByUser?.UserName,
                        DismissedWhen         = item.DismissedWhen,
                        Dismissed             = item.DismissedWhen == null ? false : true,
                        Created               = item.Created,
                        Text                  = item.Text,
                        AssociatedCallRef     = item.AssociatedCallRef,
                        AssociatedKnowledgeId = item.AssociatedKnowledgeId,
                    });
                }
            }

            model.Alerts = AlertsVM;

            /**************************
            * MODEL
            **************************/
            if (ModelState.IsValid)
            {
                //Check group exists
                var ToGroup = _context.Group.SingleOrDefault(n => n.Name.Equals(model.Message.GroupName));
                if (ToGroup == null)
                {
                    ViewBag.ErrorMessage = "An error has occured regarding the selected group - Please contact an admin";
                    return(View("ClientDashboard", model));
                }

                //Create Alert
                var Alert = new Alert
                {
                    FromUserId = LoggedInUserId,
                    ToGroupId  = ToGroup.Id,
                    Text       = model.Message.Message,
                    Created    = DateTime.Now,
                };

                //Send Alert
                _context.Alert.Add(Alert);
                _context.SaveChanges();

                //Return to Dashboard
                TempData["SuccessMessage"] = "Message Sent";
                return(RedirectToAction("Index"));
            }
            return(View("ClientDashboard", model));
        }
Пример #2
0
        public ClientDashboardPageViewModel ClientDashboard(string dismissed = null)
        {
            //Get user id
            var LoggedInUserId = User.Identity.GetUserId();

            /**************************
            * My Associated Calls
            **************************/
            //Get calls
            var AssociatedClientCalls = _context.Call.Where(n => n.Closed == false && n.ForUserId != null && n.ForUserId.Equals(LoggedInUserId));

            //To View Model
            var AssociatedCallsModel = new List <ClientAssociatedCallViewModel>();

            foreach (var call in AssociatedClientCalls)
            {
                AssociatedCallsModel.Add(new ClientAssociatedCallViewModel
                {
                    Reference = call.Reference,
                    Created   = call.Created,
                    Summary   = call.Summary
                });
            }

            /**************************
            * List of Groups
            **************************/
            var Groups = _context.Group;

            /**************************
            * My Messages
            **************************/
            //Get dismissed alerts
            IQueryable <Alert> Alerts;

            if (!String.IsNullOrEmpty(dismissed) && dismissed.Equals("true"))
            {
                Alerts = _context.Alert.Where(n => (n.ToUserId.Equals(LoggedInUserId)) && (n.DismissedWhen != null) && (n.ToGroupId == null));
            }
            //Get Non dismissed alerts
            else
            {
                Alerts = _context.Alert.Where(n => (n.ToUserId.Equals(LoggedInUserId)) && (n.DismissedWhen == null) && (n.ToGroupId == null));
            }

            //Convert Alerts into View Model Alerts
            List <ClientAlertsViewModel> AlertsVM = new List <ClientAlertsViewModel>();

            using (ApplicationDbContext dbcontext = new ApplicationDbContext())
            {
                ApplicationUser FromUser, ToUser, DismissedByUser;
                Group           FromGroup;

                foreach (var item in Alerts)
                {
                    FromUser        = dbcontext.Users.SingleOrDefault(n => n.Id.Equals(item.FromUserId));
                    FromGroup       = dbcontext.Group.SingleOrDefault(n => n.Id == item.FromGroupId);
                    ToUser          = dbcontext.Users.SingleOrDefault(n => n.Id.Equals(item.ToUserId));
                    DismissedByUser = dbcontext.Users.SingleOrDefault(n => n.Id.Equals(item.DismissedByUserId));

                    AlertsVM.Add(new ClientAlertsViewModel()
                    {
                        Id                    = item.Id,
                        FromUserId            = item.FromUserId,
                        FromUserName          = FromUser?.UserName,
                        FromGroupId           = item.FromGroupId,
                        FromGroupName         = FromGroup?.Name,
                        ToUserId              = item.ToUserId,
                        ToUserName            = ToUser?.UserName,
                        ToGroupId             = null,
                        ToGroupName           = null,
                        DismissedByUserId     = item.DismissedByUserId,
                        DismissedByUserName   = DismissedByUser?.UserName,
                        DismissedWhen         = item.DismissedWhen,
                        Dismissed             = item.DismissedWhen == null ? false : true,
                        Created               = item.Created,
                        Text                  = item.Text,
                        AssociatedCallRef     = item.AssociatedCallRef,
                        AssociatedKnowledgeId = item.AssociatedKnowledgeId,
                    });
                }
            }

            /**************************
            * MODELS
            **************************/

            var model = new ClientDashboardPageViewModel
            {
                AssociatedCalls = AssociatedCallsModel,
                GroupList       = Groups,
                Alerts          = AlertsVM
            };

            //Return view
            return(model);
        }