Example #1
0
        public List <ProcessedNotification> ProcessNotifications(List <ProcessedNotification> notifications, List <DepartmentNotification> settings)
        {
            if (notifications == null || notifications.Count < 0)
            {
                return(null);
            }

            //if (settings == null || settings.Count < 0)
            //	return null;

            var processNotifications = new List <ProcessedNotification>();

            foreach (var notification in notifications)
            {
                if (settings != null && settings.Any())
                {
                    var typeSettings = settings.Where(x => x.EventType == (int)notification.Type);

                    if (typeSettings != null && typeSettings.Any())
                    {
                        processNotifications.Add(notification);

                        foreach (var setting in typeSettings)
                        {
                            if (ValidateNotificationForProcessing(notification, setting))
                            {
                                if (setting.Everyone)                                 // Add Everyone
                                {
                                    notification.Users =
                                        _departmentsService.GetAllUsersForDepartment(notification.DepartmentId, true).Select(x => x.UserId).ToList();
                                }
                                else if (setting.DepartmentAdmins)                                 // Add Only Department Admins
                                {
                                    notification.Users =
                                        _departmentsService.GetAllAdminsForDepartment(notification.DepartmentId).Select(x => x.UserId).ToList();
                                }
                                else if (setting.LockToGroup && EventOptions.GroupEvents.Contains(notification.Type))
                                // We are locked to the source group
                                {
                                    var group = GetGroupForEvent(notification);
                                    if (group != null)
                                    {
                                        if (setting.SelectedGroupsAdminsOnly)                                         // Only add source group admins
                                        {
                                            var usersInGroup = _departmentGroupsService.GetAllAdminsForGroup(group.DepartmentGroupId);

                                            if (usersInGroup != null)
                                            {
                                                foreach (var user in usersInGroup)
                                                {
                                                    if (!notification.Users.Contains(user.UserId))
                                                    {
                                                        notification.Users.Add(user.UserId);
                                                    }
                                                }
                                            }
                                        }
                                        else                                         // Add source group users in selected roles
                                        {
                                            var usersInGroup = _departmentGroupsService.GetAllMembersForGroup(group.DepartmentGroupId)
                                                               .Select(x => x.UserId);
                                            var roles = setting.RolesToNotify.Split(char.Parse(","));

                                            foreach (var roleId in roles)
                                            {
                                                var usersInRole = _personnelRolesService.GetAllMembersOfRole(int.Parse(roleId));

                                                if (usersInRole != null)
                                                {
                                                    foreach (var user in usersInRole)
                                                    {
                                                        if (usersInGroup.Contains(user.UserId) && !notification.Users.Contains(user.UserId))
                                                        {
                                                            notification.Users.Add(user.UserId);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else                                 // Run through users, Roles and groups
                                {
                                    if (!String.IsNullOrWhiteSpace(setting.RolesToNotify))
                                    {
                                        var roles = setting.RolesToNotify.Split(char.Parse(","));
                                        foreach (var roleId in roles)                                         // Add all users in Roles
                                        {
                                            var usersInRole = _personnelRolesService.GetAllMembersOfRole(int.Parse(roleId));
                                            foreach (var user in usersInRole)
                                            {
                                                if (!notification.Users.Contains(user.UserId))
                                                {
                                                    notification.Users.Add(user.UserId);
                                                }
                                            }
                                        }
                                    }

                                    if (!String.IsNullOrWhiteSpace(setting.UsersToNotify))
                                    {
                                        var users = setting.UsersToNotify.Split(char.Parse(","));
                                        if (users != null)
                                        {
                                            foreach (var userId in users)                                             // Add all Users
                                            {
                                                if (!notification.Users.Contains(userId))
                                                {
                                                    notification.Users.Add(userId);
                                                }
                                            }
                                        }
                                    }

                                    if (!String.IsNullOrWhiteSpace(setting.GroupsToNotify))
                                    {
                                        var groups = setting.GroupsToNotify.Split(char.Parse(","));
                                        if (groups != null)
                                        {
                                            foreach (var groupId in groups)                                             // Add all users in Groups
                                            {
                                                if (setting.SelectedGroupsAdminsOnly)                                   // Only add group admins
                                                {
                                                    var usersInGroup = _departmentGroupsService.GetAllAdminsForGroup(int.Parse(groupId));

                                                    if (usersInGroup != null)
                                                    {
                                                        foreach (var user in usersInGroup)
                                                        {
                                                            if (!notification.Users.Contains(user.UserId))
                                                            {
                                                                notification.Users.Add(user.UserId);
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    var usersInGroup = _departmentGroupsService.GetAllMembersForGroup(int.Parse(groupId));

                                                    if (usersInGroup != null)
                                                    {
                                                        foreach (var user in usersInGroup)
                                                        {
                                                            if (!notification.Users.Contains(user.UserId))
                                                            {
                                                                notification.Users.Add(user.UserId);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (notification.Type == EventTypes.CalendarEventAdded)
                {
                    var calEvent = _calendarService.GetCalendarItemById(notification.ItemId);

                    if (calEvent != null)
                    {
                        if (calEvent.ItemType == 0 && !String.IsNullOrWhiteSpace(calEvent.Entities))                         // NONE: Notify based on entities
                        {
                            var items = calEvent.Entities.Split(char.Parse(","));

                            if (items.Any(x => x.StartsWith("D:")))
                            {
                                notification.Users =
                                    _departmentsService.GetAllUsersForDepartment(notification.DepartmentId, true).Select(x => x.UserId).ToList();
                            }
                            else
                            {
                                foreach (var val in items)
                                {
                                    int groupId = 0;
                                    if (int.TryParse(val.Replace("G:", ""), out groupId))
                                    {
                                        var usersInGroup = _departmentGroupsService.GetAllMembersForGroup(groupId);

                                        if (usersInGroup != null)
                                        {
                                            foreach (var user in usersInGroup)
                                            {
                                                if (!notification.Users.Contains(user.UserId))
                                                {
                                                    notification.Users.Add(user.UserId);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (calEvent.ItemType == 1)                         // ASSIGNED: Notify based on Required and Optional attendees
                        {
                        }
                        else if (calEvent.ItemType == 1)                         // RSVP: Notify entire department
                        {
                            notification.Users =
                                _departmentsService.GetAllUsersForDepartment(notification.DepartmentId, true).Select(x => x.UserId).ToList();
                        }
                    }
                }
                else if (notification.Type == EventTypes.CalendarEventUpdated)
                {
                    var calEvent = _calendarService.GetCalendarItemById(notification.ItemId);

                    if (calEvent != null)
                    {
                        if (calEvent.ItemType == 0 && !String.IsNullOrWhiteSpace(calEvent.Entities))                         // NONE: Notify based on entities
                        {
                            var items = calEvent.Entities.Split(char.Parse(","));

                            if (items.Any(x => x.StartsWith("D:")))
                            {
                                notification.Users =
                                    _departmentsService.GetAllUsersForDepartment(notification.DepartmentId, true).Select(x => x.UserId).ToList();
                            }
                            else
                            {
                                foreach (var val in items)
                                {
                                    int groupId = 0;
                                    if (int.TryParse(val.Replace("G:", ""), out groupId))
                                    {
                                        var usersInGroup = _departmentGroupsService.GetAllMembersForGroup(groupId);

                                        if (usersInGroup != null)
                                        {
                                            foreach (var user in usersInGroup)
                                            {
                                                if (!notification.Users.Contains(user.UserId))
                                                {
                                                    notification.Users.Add(user.UserId);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (calEvent.ItemType == 1)                         // ASSIGNED: Notify based on Required and Optional attendees
                        {
                        }
                        else if (calEvent.ItemType == 1 && calEvent.Attendees != null && calEvent.Attendees.Any())                         // RSVP: Notify people who've signed up
                        {
                            foreach (var attendee in calEvent.Attendees)
                            {
                                if (!notification.Users.Contains(attendee.UserId))
                                {
                                    notification.Users.Add(attendee.UserId);
                                }
                            }
                        }
                    }
                }
            }

            return(processNotifications);
        }