public bool CanUserEditRole(string userId, int personnelRoleId)
        {
            var department = _departmentsService.GetDepartmentByUserId(userId);
            var role       = _personnelRolesService.GetRoleById(personnelRoleId);

            if (department == null || role == null)
            {
                return(false);
            }

            if (role.DepartmentId != department.DepartmentId)
            {
                return(false);
            }

            if (department.IsUserAnAdmin(userId))
            {
                return(true);
            }

            return(false);
        }
        public IActionResult Index()
        {
            var model = new NotificationIndexView();

            model.Notifications = _notificationService.GetNotificationsByDepartment(DepartmentId);
            var unitTypes = _unitsService.GetUnitTypesForDepartment(DepartmentId);
            var allRoles  = _personnelRolesService.GetRolesForDepartment(DepartmentId);

            model.CustomStates = _customStateService.GetAllActiveCustomStatesForDepartment(DepartmentId);

            foreach (var notification in model.Notifications)
            {
                if (notification.Everyone)
                {
                    model.NotifyUsers.Add(notification.DepartmentNotificationId, "Everyone");
                }
                else if (notification.DepartmentAdmins)
                {
                    model.NotifyUsers.Add(notification.DepartmentNotificationId, "Department Admins");
                }
                else if (notification.SelectedGroupsAdminsOnly)
                {
                    model.NotifyUsers.Add(notification.DepartmentNotificationId, "Selected Group(s) Admins");
                }
                else
                {
                    var sb = new StringBuilder();

                    if (!String.IsNullOrWhiteSpace(notification.RolesToNotify))
                    {
                        var roles = notification.RolesToNotify.Split(char.Parse(","));
                        foreach (var roleId in roles)
                        {
                            var role = _personnelRolesService.GetRoleById(int.Parse(roleId));

                            if (role != null)
                            {
                                if (sb.Length > 0)
                                {
                                    sb.Append("," + role.Name);
                                }
                                else
                                {
                                    sb.Append(role.Name);
                                }
                            }
                        }
                    }

                    if (!String.IsNullOrWhiteSpace(notification.GroupsToNotify))
                    {
                        var groups = notification.GroupsToNotify.Split(char.Parse(","));
                        foreach (var groupId in groups)
                        {
                            var group = _departmentGroupsService.GetGroupById(int.Parse(groupId), false);

                            if (group != null)
                            {
                                if (sb.Length > 0)
                                {
                                    sb.Append("," + group.Name);
                                }
                                else
                                {
                                    sb.Append(group.Name);
                                }
                            }
                        }
                    }

                    if (!String.IsNullOrWhiteSpace(notification.UsersToNotify))
                    {
                        var users = notification.UsersToNotify.Split(char.Parse(","));
                        foreach (var userId in users)
                        {
                            var user = _usersService.GetUserById(userId);

                            if (sb.Length > 0)
                            {
                                sb.Append("," + UserHelper.GetFullNameForUser(user.UserId));
                            }
                            else
                            {
                                sb.Append(UserHelper.GetFullNameForUser(user.UserId));
                            }
                        }
                    }

                    model.NotifyUsers.Add(notification.DepartmentNotificationId, sb.ToString());
                }

                if (notification.EventType == (int)EventTypes.RolesInGroupAvailabilityAlert || notification.EventType == (int)EventTypes.RolesInDepartmentAvailabilityAlert)
                {
                    if (!String.IsNullOrWhiteSpace(notification.Data) && allRoles.Any(x => x.PersonnelRoleId == int.Parse(notification.Data)))
                    {
                        model.NotifyData.Add(notification.DepartmentNotificationId, allRoles.First(x => x.PersonnelRoleId == int.Parse(notification.Data)).Name);
                    }
                }
                else if (notification.EventType == (int)EventTypes.UnitTypesInGroupAvailabilityAlert || notification.EventType == (int)EventTypes.UnitTypesInDepartmentAvailabilityAlert)
                {
                    if (!String.IsNullOrWhiteSpace(notification.Data) && unitTypes.Any(x => x.UnitTypeId == int.Parse(notification.Data)))
                    {
                        model.NotifyData.Add(notification.DepartmentNotificationId, unitTypes.First(x => x.UnitTypeId == int.Parse(notification.Data)).Type);
                    }
                }
            }

            return(View(model));
        }
        public string GetMessageForType(ProcessedNotification notification)
        {
            try
            {
                NotificationItem data = ObjectSerialization.Deserialize <NotificationItem>(notification.Data);

                switch (notification.Type)
                {
                case EventTypes.UnitStatusChanged:
                    var unitEvent  = _unitsService.GetUnitStateById((int)data.StateId);
                    var unitStatus = _customStateService.GetCustomUnitState(unitEvent);

                    if (unitEvent != null && unitEvent.Unit != null)
                    {
                        return(String.Format("Unit {0} is now {1}", unitEvent.Unit.Name, unitStatus.ButtonText));
                    }
                    else if (unitEvent != null)
                    {
                        return(String.Format("A Unit's status is now {0}", unitStatus.ButtonText));
                    }
                    else
                    {
                        return("A unit's status changed");
                    }

                case EventTypes.PersonnelStaffingChanged:
                    var userStaffing     = _userStateService.GetUserStateById((int)data.StateId);
                    var userProfile      = _userProfileService.GetProfileByUserId(userStaffing.UserId);
                    var userStaffingText = _customStateService.GetCustomPersonnelStaffing(data.DepartmentId, userStaffing);

                    return(String.Format("{0} staffing is now {1}", userProfile.FullName.AsFirstNameLastName, userStaffingText.ButtonText));

                case EventTypes.PersonnelStatusChanged:
                    var actionLog = _actionLogsService.GetActionlogById(data.StateId);

                    UserProfile profile = null;
                    if (actionLog != null)
                    {
                        profile = _userProfileService.GetProfileByUserId(actionLog.UserId);
                    }
                    else if (data.UserId != String.Empty)
                    {
                        profile = _userProfileService.GetProfileByUserId(data.UserId);
                    }

                    var userStatusText = _customStateService.GetCustomPersonnelStatus(data.DepartmentId, actionLog);

                    if (profile != null && userStatusText != null)
                    {
                        return(String.Format("{0} status is now {1}", profile.FullName.AsFirstNameLastName, userStatusText.ButtonText));
                    }
                    else if (profile != null)
                    {
                        return(String.Format("{0} status has changed", profile.FullName.AsFirstNameLastName));
                    }

                    return(String.Empty);

                case EventTypes.UserCreated:
                    var newUserprofile = _userProfileService.GetProfileByUserId(data.UserId);

                    if (newUserprofile != null)
                    {
                        return(String.Format("{0} has been added to your department", newUserprofile.FullName.AsFirstNameLastName));
                    }
                    else
                    {
                        return("A new user has been added to your department");
                    }

                case EventTypes.UserAssignedToGroup:

                    UserProfile groupUserprofile = null;
                    try
                    {
                        if (data.UserId != String.Empty)
                        {
                            groupUserprofile = _userProfileService.GetProfileByUserId(data.UserId);
                        }
                    }
                    catch { }

                    DepartmentGroup newGroup = null;
                    try
                    {
                        if (data.GroupId != 0)
                        {
                            newGroup = _departmentGroupsService.GetGroupById((int)data.GroupId, false);
                        }
                    }
                    catch { }

                    if (groupUserprofile != null && newGroup != null)
                    {
                        return(String.Format("{0} has been assigned to group {1}", groupUserprofile.FullName.AsFirstNameLastName, newGroup.Name));
                    }
                    else if (groupUserprofile != null && newGroup == null)
                    {
                        return(String.Format("{0} has been assigned to group", groupUserprofile.FullName.AsFirstNameLastName));
                    }
                    else if (newGroup != null && groupUserprofile == null)
                    {
                        return(String.Format("A user has been assigned to group {0}", newGroup.Name));
                    }
                    else
                    {
                        return(String.Format("A has been assigned to a group"));
                    }

                case EventTypes.CalendarEventUpcoming:
                    var calandarItem = _calendarService.GetCalendarItemById((int)data.ItemId);
                    return(String.Format("Event {0} is upcoming", calandarItem.Title));

                case EventTypes.DocumentAdded:
                    var document = _documentsService.GetDocumentById((int)data.ItemId);
                    return(String.Format("Document {0} has been added", document.Name));

                case EventTypes.NoteAdded:
                    var note = _notesService.GetNoteById((int)data.ItemId);

                    if (note != null)
                    {
                        return(String.Format("Message {0} has been added", note.Title));
                    }

                    break;

                case EventTypes.UnitAdded:
                    var unit = _unitsService.GetUnitById((int)data.UnitId);
                    return(String.Format("Unit {0} has been added", unit.Name));

                case EventTypes.LogAdded:
                    var log = _workLogsService.GetWorkLogById((int)data.ItemId);

                    if (log != null)
                    {
                        var logUserProfile = _userProfileService.GetProfileByUserId(log.LoggedByUserId);
                        return(String.Format("{0} created log {1}", logUserProfile.FullName.AsFirstNameLastName, log.LogId));
                    }
                    else
                    {
                        return(String.Format("A new log was created"));
                    }

                case EventTypes.DepartmentSettingsChanged:
                    return(String.Format("Settings have been updated for your department"));

                case EventTypes.RolesInGroupAvailabilityAlert:

                    var userStateChanged = _userStateService.GetUserStateById(int.Parse(notification.Value));
                    var roleForGroup     = _personnelRolesService.GetRoleById(notification.PersonnelRoleTargeted);
                    var groupForRole     = _departmentGroupsService.GetGroupForUser(userStateChanged.UserId, notification.DepartmentId);

                    return(String.Format("Availability for role {0} in group {1} is at or below the lower limit", roleForGroup.Name, groupForRole.Name));

                case EventTypes.RolesInDepartmentAvailabilityAlert:
                    if (notification != null)
                    {
                        var roleForDep = _personnelRolesService.GetRoleById(notification.PersonnelRoleTargeted);

                        if (roleForDep != null)
                        {
                            return(String.Format("Availability for role {0} for the department is at or below the lower limit", roleForDep.Name));
                        }
                    }
                    break;

                case EventTypes.UnitTypesInGroupAvailabilityAlert:
                    if (data.UnitId != 0)
                    {
                        var unitForGroup = _unitsService.GetUnitById(data.UnitId);

                        if (unitForGroup != null && unitForGroup.StationGroupId.HasValue)
                        {
                            var groupForUnit = _departmentGroupsService.GetGroupById(unitForGroup.StationGroupId.Value);

                            return(String.Format("Availability for unit type {0} in group {1} is at or below the lower limit",
                                                 unitForGroup.Type, groupForUnit.Name));
                        }
                    }
                    return(String.Empty);

                case EventTypes.UnitTypesInDepartmentAvailabilityAlert:
                    return(String.Format("Availability for unit type {0} for the department is at or below the lower limit", notification.UnitTypeTargeted));

                case EventTypes.CalendarEventAdded:
                    var calEvent   = _calendarService.GetCalendarItemById(notification.ItemId);
                    var department = _departmentsService.GetDepartmentById(calEvent.DepartmentId);

                    if (calEvent != null)
                    {
                        if (calEvent.ItemType == 0)
                        {
                            if (calEvent.IsAllDay)
                            {
                                return($"New Calendar Event {calEvent.Title} on {calEvent.Start.TimeConverter(department).ToShortDateString()}");
                            }
                            else
                            {
                                return($"New Calendar Event {calEvent.Title} on {calEvent.Start.TimeConverter(department).ToShortDateString()} at {calEvent.Start.TimeConverter(department).ToShortTimeString()}");
                            }
                        }
                        else
                        if (calEvent.IsAllDay)
                        {
                            return($"New Calendar RSVP Event {calEvent.Title} on {calEvent.Start.TimeConverter(department).ToShortDateString()}");
                        }
                        else
                        {
                            return($"New Calendar RSVP Event {calEvent.Title} on {calEvent.Start.TimeConverter(department).ToShortDateString()} at {calEvent.Start.TimeConverter(department).ToShortTimeString()}");
                        }
                    }
                    else
                    {
                        return(String.Empty);
                    }

                case EventTypes.CalendarEventUpdated:
                    var calUpdatedEvent           = _calendarService.GetCalendarItemById(notification.ItemId);
                    var calUpdatedEventDepartment = _departmentsService.GetDepartmentById(calUpdatedEvent.DepartmentId);

                    if (calUpdatedEvent != null)
                    {
                        return($"Calendar Event {calUpdatedEvent.Title} on {calUpdatedEvent.Start.TimeConverter(calUpdatedEventDepartment).ToShortDateString()} has changed");
                    }
                    else
                    {
                        return(String.Empty);
                    }

                default:
                    throw new ArgumentOutOfRangeException("type");
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(ex, extraMessage: notification.Data);
                return(String.Empty);
            }

            return(String.Empty);
        }