Exemplo n.º 1
0
        private ComposeMessageModel FillComposeMessageModel(ComposeMessageModel model)
        {
            model.Department = _departmentsService.GetDepartmentByUserId(UserId);
            model.User       = _usersService.GetUserById(UserId);

            //model.Users = _departmentsService.GetAllUsersForDepartment(model.Department.DepartmentId);

            return(model);
        }
Exemplo n.º 2
0
        public IActionResult Compose()
        {
            ComposeMessageModel model = new ComposeMessageModel();

            model.Department = _departmentsService.GetDepartmentByUserId(UserId);
            model.User       = _usersService.GetUserById(UserId);
            model.Types      = model.MessageType.ToSelectList();

            var shifts = _shiftsService.GetAllShiftsByDepartment(DepartmentId);

            model.Shifts = new SelectList(shifts, "ShiftId", "Name");

            model.Message = new Message();

            return(View(FillComposeMessageModel(model)));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Compose(ComposeMessageModel model, IFormCollection collection, CancellationToken cancellationToken)
        {
            var roles  = new List <string>();
            var groups = new List <string>();
            var users  = new List <string>();
            var shifts = new List <string>();

            if (collection.ContainsKey("roles"))
            {
                roles.AddRange(collection["roles"].ToString().Split(char.Parse(",")));
            }

            if (collection.ContainsKey("groups"))
            {
                groups.AddRange(collection["groups"].ToString().Split(char.Parse(",")));
            }

            if (collection.ContainsKey("users"))
            {
                users.AddRange(collection["users"].ToString().Split(char.Parse(",")));
            }

            if (collection.ContainsKey("exludedShifts"))
            {
                shifts.AddRange(collection["exludedShifts"].ToString().Split(char.Parse(",")));
            }

            if (!model.SendToAll && (roles.Count + groups.Count + users.Count) == 0)
            {
                ModelState.AddModelError("", "You must specify at least one Recipient.");
            }

            if (model.SendToMatchOnly && (roles.Count + groups.Count) == 0)
            {
                ModelState.AddModelError("", "You must specify at least one Group or Role to send to.");
            }

            if (ModelState.IsValid)
            {
                var excludedUsers = new List <string>();

                if (shifts.Any())
                {
                    foreach (var shiftId in shifts)
                    {
                        var shift = await _shiftsService.GetShiftByIdAsync(int.Parse(shiftId));

                        excludedUsers.AddRange(shift.Personnel.Select(x => x.UserId));
                    }
                }

                model.Message.Type = (int)model.MessageType;
                if (model.SendToAll)
                {
                    var allUsers = await _departmentsService.GetAllUsersForDepartmentAsync(DepartmentId);

                    foreach (var user in allUsers)
                    {
                        if (user.UserId != UserId && (!excludedUsers.Any() || !excludedUsers.Contains(user.UserId)))
                        {
                            model.Message.AddRecipient(user.UserId);
                        }
                    }
                }
                else if (model.SendToMatchOnly)
                {
                    var usersInRoles = new Dictionary <int, List <string> >();

                    foreach (var role in roles)
                    {
                        var roleMembers = await _personnelRolesService.GetAllMembersOfRoleAsync(int.Parse(role));

                        usersInRoles.Add(int.Parse(role), roleMembers.Select(x => x.UserId).ToList());
                    }

                    foreach (var group in groups)
                    {
                        var members = await _departmentGroupsService.GetAllMembersForGroupAsync(int.Parse(group));

                        foreach (var member in members)
                        {
                            bool isInRoles = false;
                            if (model.Message.GetRecipients().All(x => x != member.UserId) && member.UserId != UserId &&
                                (!excludedUsers.Any() || !excludedUsers.Contains(member.UserId)))
                            {
                                foreach (var key in usersInRoles)
                                {
                                    if (key.Value.Any(x => x == member.UserId))
                                    {
                                        isInRoles = true;
                                        break;
                                    }
                                }

                                if (isInRoles)
                                {
                                    model.Message.AddRecipient(member.UserId);
                                }
                            }
                        }
                    }
                }
                else
                {
                    foreach (var user in users)
                    {
                        model.Message.AddRecipient(user);
                    }

                    // Add all members of the group
                    foreach (var group in groups)
                    {
                        var members = await _departmentGroupsService.GetAllMembersForGroupAsync(int.Parse(group));

                        foreach (var member in members)
                        {
                            if (model.Message.GetRecipients().All(x => x != member.UserId) && member.UserId != UserId && (!excludedUsers.Any() || !excludedUsers.Contains(member.UserId)))
                            {
                                model.Message.AddRecipient(member.UserId);
                            }
                        }
                    }

                    // Add all the users of a specific role
                    foreach (var role in roles)
                    {
                        var roleMembers = await _personnelRolesService.GetAllMembersOfRoleAsync(int.Parse(role));

                        foreach (var member in roleMembers)
                        {
                            if (model.Message.GetRecipients().All(x => x != member.UserId) && member.UserId != UserId && (!excludedUsers.Any() || !excludedUsers.Contains(member.UserId)))
                            {
                                model.Message.AddRecipient(member.UserId);
                            }
                        }
                    }
                }

                model.Message.SentOn        = DateTime.UtcNow;
                model.Message.SendingUserId = UserId;
                model.Message.Body          = System.Net.WebUtility.HtmlDecode(model.Message.Body);
                model.Message.IsBroadcast   = true;

                var savedMessage = await _messageService.SaveMessageAsync(model.Message, cancellationToken);

                await _messageService.SendMessageAsync(savedMessage, "", DepartmentId, false, cancellationToken);

                return(RedirectToAction("Inbox"));
            }

            model.Department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId);

            model.User  = _usersService.GetUserById(UserId);
            model.Types = model.MessageType.ToSelectList();

            var savedShifts = await _shiftsService.GetAllShiftsByDepartmentAsync(DepartmentId);

            model.Shifts       = new SelectList(savedShifts, "ShiftId", "Name");
            model.Message.Body = System.Net.WebUtility.HtmlDecode(model.Message.Body);

            return(View(await FillComposeMessageModel(model)));
        }