public static void ClassInit(TestContext context)
        {
            _container = new UnityContainer();
            _container.AddNewExtension<EnterpriseLibraryCoreExtension>();
            using (IConsoleRepository repository = _container.Resolve<ConsoleRepository>())
            {
                var team = repository.Query<Team>(t=>t.RelatedOffices).FirstOrDefault(t => t.Title == "New Test Team");
                if (team != null)
                {
                    repository.Attach<Team>(team);
                    repository.Delete<Team>(team);
                    repository.SaveChanges();
                }
            }

            using (IConsoleRepository repository = _container.Resolve<ConsoleRepository>())
            {
                var team = repository.Query<Team>(t => t.RelatedOffices).FirstOrDefault(t => t.Title == "Test Team");
                if (team != null)
                {
                    repository.Attach<Team>(team);
                    repository.Delete<Team>(team);
                    repository.SaveChanges();
                }
            }

            _logHandler = new LogHandler();
            var consoleRepository = new ConsoleRepository();
            _mockCurrentHttpContext = new Mock<CurrentHttpContext>();
            _mockCurrentHttpContext.Setup(h => h.CurrentUser).Returns(new GenericPrincipal(new GenericIdentity(@"talbotdev\MurrayE"), null));
            _mockCurrentHttpContext.Setup(h => h.Context).Returns(MvcMockHelpers.FakeHttpContextWithSession());

            var adminModuleManager = new AdminModuleManager(consoleRepository, _logHandler, _mockCurrentHttpContext.Object, new WebSiteModuleManager(consoleRepository, _mockCurrentHttpContext.Object));
            _editUserDto = adminModuleManager.GetUser(4);

            var consoleRepository2 = new ConsoleRepository();
            var adminModuleManager2 = new AdminModuleManager(consoleRepository2, _logHandler, _mockCurrentHttpContext.Object, new WebSiteModuleManager(consoleRepository2, _mockCurrentHttpContext.Object));
            var teamDto = new TeamDto { Title = "Test Team", QuoteExpiryDaysDefault = 2, DefaultPolicyType = "MARINE" };
            _testTeam = adminModuleManager2.CreateTeam(teamDto);
        }
        public ActionResult CreateUser(UserDto userDto)
        {
            try
            {
                var newUserId = AdminModuleManager.CreateUser(userDto);

                return new JsonNetResult
                    {
                        Data = newUserId
                    };
            }
            catch (ApplicationException ex)
            {
                LogHandler.WriteLog(ex.ToString(), LogSeverity.Error, LogCategory.BusinessComponent);
                throw new HttpException(409, ex.Message);
            }
			catch (Exception ex) // TODO: Remove
            {
                LogHandler.WriteLog(ex.ToString(), LogSeverity.Error, LogCategory.BusinessComponent);
                throw new HttpException(500, "Server Error");
            }
        }
        public int CreateUser(UserDto userDto)
        {
            using (ConsoleRepository)
            {
                var existingUser =
                    ConsoleRepository.Query<User>().FirstOrDefault(u => u.DomainLogon == userDto.DomainLogon);

                if (existingUser != null)
                    throw new ApplicationException("User already exists");

                var user = new User();
                user.DomainLogon = userDto.DomainLogon;
                user.IsActive = userDto.IsActive;
                

                if (userDto.TeamMemberships != null)
                {
                    var teamMembershipList = new List<TeamMembership>();
                    foreach (var newTeamM in userDto.TeamMemberships)
                    {
                        var team = ConsoleRepository.Query<Team>().FirstOrDefault(t => t.Id == newTeamM.Team.Id);
                        teamMembershipList.Add(new TeamMembership
                            {
                                User = user,
                                Team = team,
                                StartDate = DateTime.Now,
                                EndDate = null,
                                PrimaryTeamMembership = newTeamM.PrimaryTeamMembership
                            });
                    }
                    user.TeamMemberships = teamMembershipList;
                }

               

               

                ConsoleRepository.Add<User>(user);
                ConsoleRepository.SaveChanges();
                return user.Id;
            }
        }
        public UserDto GetUser(int? userId)
        {
            if (userId == null) // TODO: Remove nullable from userId
                throw new Exception("userId is empty"); // TODO: throw new ArgumentNullException(userId)

            var userDto = new UserDto();
            using (ConsoleRepository)
            {
                var user =
                    ConsoleRepository.Query<User>(t => t.Id == userId.Value, 
                                                    t => t.PrimaryOffice, 
                                                    t => t.FilterMembers,
                                                    t => t.FilterCOBs, 
                                                    t => t.AdditionalUsers,
                                                    t => t.TeamMemberships.Select(tm => tm.Team), 
                                                    t => t.DefaultUW).FirstOrDefault();

                if (user != null)
                {
                    SetUserDto(userDto, user, ConsoleRepository);
                }
            }

            return userDto;
        }
        public UserDto GetSelectedUserByName(string userName)
        {
            if (userName == null)
                throw new Exception("userName is empty"); // TODO: Throw new ArgumentNullException(userName)

            var userDto = new UserDto();

            using (ConsoleRepository)
            {
                
                var user = 
                    ConsoleRepository.Query<User>(t => t.DomainLogon == userName, 
                                                t => t.FilterCOBs,
                                                t => t.FilterOffices,
                                                t => t.AdditionalCOBs,
                                                t => t.AdditionalOffices,
                                                t => t.DefaultOrigOffice,
                                                t => t.PrimaryOffice, 
                                                t => t.FilterMembers, 
                                                t => t.AdditionalUsers, 
                                                t => t.DefaultUW, t => t.TeamMemberships.Select(tm => tm.Team.PricingActuary))
                                     .SingleOrDefault();

                if (user != null)
                {
                    user.TeamMemberships.OrderBy(tm => tm.Team.Title).ToList().ForEach((tm) =>
                        { user.TeamMemberships.Remove(tm); user.TeamMemberships.Add(tm); });
                    SetUserDto(userDto, user, ConsoleRepository);
                }
            }

            return userDto;
        }
 public UserDto GetUserPersonalSettings()
 {
     User user = WebSiteModuleManager.EnsureCurrentUser();
     var userDto = new UserDto();
     SetUserDto(userDto, user, ConsoleRepository);
     return userDto;
 }
        public string EditUser(UserDto userDto)
        {
            using (ConsoleRepository)
            {
                var user =
                    ConsoleRepository.Query<User>(u => u.TeamMemberships.Select(tm => tm.Team.RelatedCOBs),
                                                  u => u.TeamMemberships.Select(tm => tm.Team.RelatedOffices),
                                                  u => u.FilterCOBs, u => u.FilterOffices, u => u.FilterMembers,
                                                  u => u.AdditionalCOBs, u => u.AdditionalOffices,
                                                  u => u.AdditionalUsers).FirstOrDefault(u => u.Id == userDto.Id);

                if (user == null)
                    throw new ApplicationException("User to edit not found");
                // TODO: Throw new NullReferenceException(user)

                if (string.IsNullOrEmpty(userDto.UnderwriterId))
                    throw new ApplicationException("Underwriter required");

                if (!ConsoleRepository.Query<Underwriter>().Any(u => u.Code == userDto.UnderwriterId))
                    throw new ApplicationException("Underwriter not recognised");

                ConsoleRepository.Attach<User>(user);

                user.DomainLogon = userDto.DomainLogon;
                user.IsActive = userDto.IsActive;
                user.UnderwriterCode = userDto.UnderwriterId;
                user.NonLondonBroker = userDto.NonLondonBroker;
                user.PrimaryOffice = (userDto.PrimaryOffice != null)
                                         ? ConsoleRepository.Query<Office>()
                                                            .FirstOrDefault(o => o.Id == userDto.PrimaryOffice.Id)
                                         : null;
                user.DefaultOrigOffice = (userDto.DefaultOrigOffice != null)
                                             ? ConsoleRepository.Query<Office>()
                                                                .FirstOrDefault(
                                                                    o => o.Id == userDto.DefaultOrigOffice.Id)
                                             : null;
                user.DefaultUW = (userDto.DefaultUW != null)
                                     ? ConsoleRepository.Query<User>().FirstOrDefault(d => d.Id == userDto.DefaultUW.Id)
                                     : null;


                //  Must do these bits first because they clear e.g. FilterCOBs
                if (userDto.FilterCOBs != null && userDto.FilterCOBs.Count > 0)
                {
                    if (user.FilterCOBs != null)
                        user.FilterCOBs.Clear();

                    var filterCOBIds = userDto.FilterCOBs.Select(fc => fc.Id);
                    var newFilterCOBs = ConsoleRepository.Query<COB>().Where(fc => filterCOBIds.Contains(fc.Id));
                    foreach (var newFilterC in newFilterCOBs)
                        user.FilterCOBs.Add(newFilterC);
                }
                else
                {
                    user.FilterCOBs.Clear();
                }

                if (userDto.FilterOffices != null && userDto.FilterOffices.Count > 0)
                {
                    if (user.FilterOffices != null)
                        user.FilterOffices.Clear();

                    var filterOfficeIds = userDto.FilterOffices.Select(fo => fo.Id);
                    var newFilterOffices = ConsoleRepository.Query<Office>()
                                                            .Where(fo => filterOfficeIds.Contains(fo.Id));
                    foreach (var newFilterO in newFilterOffices)
                        user.FilterOffices.Add(newFilterO);
                }
                else
                {
                    user.FilterOffices.Clear();
                }

                if (userDto.TeamMemberships != null)
                {
                    foreach (var newMem in userDto.TeamMemberships)
                    {
                        var currentMem = user.TeamMemberships.FirstOrDefault(t => t.TeamId == newMem.Team.Id && t.IsCurrent == true);

                        if (currentMem == null)
                        {
                            user.TeamMemberships.Add(new TeamMembership
                                {
                                    PrimaryTeamMembership = newMem.PrimaryTeamMembership,
                                    TeamId = newMem.Team.Id,
                                    UserId = user.Id,
                                    StartDate = DateTime.Now,
                                    EndDate = null,
                                    IsCurrent = true
                                });
                            
                            //  Only add team filters for newly added memberships.
                            AddTeamFilters(new List<Int32>() { newMem.Team.Id }, user, null);
                        }
                        else
                        {
                            currentMem.PrimaryTeamMembership = newMem.PrimaryTeamMembership;
                            currentMem.IsCurrent = true;
                            currentMem.EndDate = null;
                        }
                    }
                }

                //List<COB> memberFilterCobs = ConsoleRepository.Query<Team>(t => t.Contains(), t => t.RelatedCOBs) 

                if (userDto.FilterMembers != null && userDto.FilterMembers.Count > 0)
                {
                    if (user.FilterMembers != null)
                        user.FilterMembers.Clear();

                    var filterMemberIds = userDto.FilterMembers.Select(fm => fm.Id);
                    var newFilterMembers = ConsoleRepository.Query<User>().Where(fm => filterMemberIds.Contains(fm.Id));
                    foreach (var newFilterM in newFilterMembers)
                        user.FilterMembers.Add(newFilterM);
                }
                else
                {
                    user.FilterMembers.Clear();
                }

                if (userDto.AdditionalCOBs != null && userDto.AdditionalCOBs.Count > 0)
                {
                    if (user.AdditionalCOBs != null)
                        user.AdditionalCOBs.Clear();

                    var additionalCOBIds = userDto.AdditionalCOBs.Select(ac => ac.Id);
                    var newAdditionalCOBs = ConsoleRepository.Query<COB>().Where(ac => additionalCOBIds.Contains(ac.Id));
                    foreach (var newAdditionalC in newAdditionalCOBs)
                        user.AdditionalCOBs.Add(newAdditionalC);
                }
                else
                {
                    user.AdditionalCOBs.Clear();
                }

                if (userDto.AdditionalOffices != null && userDto.AdditionalOffices.Count > 0)
                {
                    if (user.AdditionalOffices != null)
                        user.AdditionalOffices.Clear();

                    var additionalOfficeIds = userDto.AdditionalOffices.Select(ao => ao.Id);
                    var newAdditionalOffices =
                        ConsoleRepository.Query<Office>().Where(ao => additionalOfficeIds.Contains(ao.Id));
                    foreach (var newAdditionalO in newAdditionalOffices)
                        user.AdditionalOffices.Add(newAdditionalO);
                }
                else
                {
                    user.AdditionalOffices.Clear();
                }

                if (userDto.AdditionalUsers != null && userDto.AdditionalUsers.Count > 0)
                {
                    if (user.AdditionalUsers != null)
                        user.AdditionalUsers.Clear();

                    var additionalUserIds = userDto.AdditionalUsers.Select(ao => ao.Id);
                    var newAdditionalUsers =
                        ConsoleRepository.Query<User>().Where(ao => additionalUserIds.Contains(ao.Id));
                    foreach (var newAdditionalU in newAdditionalUsers)
                        user.AdditionalUsers.Add(newAdditionalU);
                }
                else
                {
                    user.AdditionalUsers.Clear();
                }

                if (userDto.AllTeamMemberships != null)
                {
                    //  The membership could be in this list because it was removed from the user in the UI
                    //  Or because it was not added in the UI
                    foreach (var membership in userDto.AllTeamMemberships)
                    {
                        //  Does the user have a current membership for this team?
                        var teamMembership = user.TeamMemberships.FirstOrDefault(tm => tm.TeamId == membership.Team.Id && tm.IsCurrent == true);
                        if (teamMembership != null)
                        {
                            
                            //if (!membership.IsCurrent)
                            //{
                                //  Yes, need to  stop it.
                                teamMembership.IsCurrent = false;
                                teamMembership.EndDate = DateTime.Now;

                                // Remove team filters from user
                                var team = teamMembership.Team;// user.TeamMemberships.Where(tm => tm.TeamId == membership.Team.Id && tm.IsCurrent == false).Select(t => t.Team);

                                if (team != null)
                                {
                                    foreach (var cob in team.RelatedCOBs)
                                    {
                                        //  Should only remove if not in another current membership's team related cobs!                                        
                                        user.FilterCOBs.Remove(cob);
                                    }

                                    foreach (var office in team.RelatedOffices)
                                    {
                                        //  Should only remove if not in another current membership's team related offices!
                                        user.FilterOffices.Remove(office);
                                    }
                                }
                            //}
                            //else
                            //{
                            //    teamMembership.IsCurrent = true;
                            //    teamMembership.EndDate = null;
                            //}
                        }
                    }
                }

                // Add Team FilterCOBs and FilterOffices
                //if (userDto.TeamMemberships != null)
                //    AddTeamFilters(userDto.TeamMemberships.Select(tm => tm.Team.Id), user, null);

                ConsoleRepository.SaveChanges();

                if (CurrentHttpContext.CurrentUser.Identity.Name.ToUpper() == user.DomainLogon.ToUpper())
                    CurrentHttpContext.Context.Session["User"] = null;

                return "User successfully updated";
            }
        }
        public int CreateUser(UserDto userDto)
        {
            using (ConsoleRepository)
            {
                var existingUser =
                    ConsoleRepository.Query<User>().FirstOrDefault(u => u.DomainLogon == userDto.DomainLogon);

                if (existingUser != null)
                    throw new ApplicationException("User already exists");

                var user = new User();
                user.DomainLogon = userDto.DomainLogon;
                user.IsActive = userDto.IsActive;
                user.UnderwriterCode = userDto.UnderwriterId;
                user.NonLondonBroker = userDto.NonLondonBroker;
                user.PrimaryOffice =
                    ConsoleRepository.Query<Office>().FirstOrDefault(o => o.Id == userDto.PrimaryOffice.Id);
                user.DefaultOrigOffice =
                    ConsoleRepository.Query<Office>().FirstOrDefault(o => o.Id == userDto.DefaultOrigOffice.Id);
                user.DefaultUW = ConsoleRepository.Query<User>().FirstOrDefault(d => d.Id == userDto.DefaultUW.Id);

                if (userDto.TeamMemberships != null)
                {
                    var teamMembershipList = new List<TeamMembership>();
                    foreach (var newTeamM in userDto.TeamMemberships)
                    {
                        var team = ConsoleRepository.Query<Team>().FirstOrDefault(t => t.Id == newTeamM.Team.Id);
                        teamMembershipList.Add(new TeamMembership
                            {
                                User = user,
                                Team = team,
                                StartDate = DateTime.Now,
                                EndDate = null,
                                PrimaryTeamMembership = newTeamM.PrimaryTeamMembership
                            });
                    }
                    user.TeamMemberships = teamMembershipList;
                }

                if (userDto.FilterCOBs != null)
                {
                    var filterCobList = new List<COB>();
                    foreach (var newFilterC in userDto.FilterCOBs)
                    {
                        var cob = ConsoleRepository.Query<COB>().FirstOrDefault(c => c.Id == newFilterC.Id);
                        filterCobList.Add(cob);
                    }
                    user.FilterCOBs = filterCobList;
                }

                if (userDto.FilterOffices != null)
                {
                    var filterOfficeList = new List<Office>();
                    foreach (var newFilterO in userDto.FilterOffices)
                    {
                        var office = ConsoleRepository.Query<Office>().FirstOrDefault(c => c.Id == newFilterO.Id);
                        filterOfficeList.Add(office);
                    }
                    user.FilterOffices = filterOfficeList;
                }

                if (userDto.FilterMembers != null)
                {
                    var filterMembersList = new List<User>();
                    foreach (var newFilterM in userDto.FilterMembers)
                    {
                        var member = ConsoleRepository.Query<User>().FirstOrDefault(c => c.Id == newFilterM.Id);
                        filterMembersList.Add(member);
                    }
                    user.FilterMembers = filterMembersList;
                }

                if (userDto.AdditionalCOBs != null)
                {
                    var additionalCobList = new List<COB>();
                    foreach (var newAdditionalC in userDto.AdditionalCOBs)
                    {
                        var cob = ConsoleRepository.Query<COB>().FirstOrDefault(c => c.Id == newAdditionalC.Id);
                        additionalCobList.Add(cob);
                    }
                    user.AdditionalCOBs = additionalCobList;
                }

                if (userDto.AdditionalOffices != null)
                {
                    var additionalOffices = new List<Office>();
                    foreach (var newAdditionalO in userDto.AdditionalOffices)
                    {
                        var office = ConsoleRepository.Query<Office>().FirstOrDefault(c => c.Id == newAdditionalO.Id);
                        additionalOffices.Add(office);
                    }
                    user.AdditionalOffices = additionalOffices;
                }

                if (userDto.AdditionalUsers != null)
                {
                    var additionalUserList = new List<User>();
                    foreach (var newAdditionalU in userDto.AdditionalUsers)
                    {
                        var additionalUser =
                            ConsoleRepository.Query<User>().FirstOrDefault(c => c.Id == newAdditionalU.Id);
                        additionalUserList.Add(additionalUser);
                    }
                    user.AdditionalUsers = additionalUserList;
                }

                // Add Team FilterCOBs and FilterOffices
                if (userDto.TeamMemberships != null)
                    AddTeamFilters(userDto.TeamMemberships.Select(tm => tm.Team.Id), user, null);

                ConsoleRepository.Add<User>(user);
                ConsoleRepository.SaveChanges();
                return user.Id;
            }
        }
        private static void SetUserDto(UserDto userDto, User user, IConsoleRepository _consoleRepository)
        {
            userDto.Id = user.Id;
            userDto.DomainLogon = user.DomainLogon;
            userDto.IsActive = user.IsActive;

            // Currently Selected Values
            userDto.DefaultOrigOffice = (user.DefaultOrigOffice != null)
                                            ? new OfficeDto
                                                {
                                                    Id = user.DefaultOrigOffice.Id,
                                                    Title = user.DefaultOrigOffice.Name
                                                }
                                            : null;
            userDto.PrimaryOffice = (user.PrimaryOffice != null)
                                        ? new OfficeDto { Id = user.PrimaryOffice.Id, Title = user.PrimaryOffice.Name }
                                        : null;
            userDto.DefaultUW = (user.DefaultUW != null)
                                    ? new UserDto { Id = user.DefaultUW.Id, DomainLogon = user.DefaultUW.DomainLogon }
                                    : null;
            userDto.UnderwriterId = user.UnderwriterCode;
            userDto.NonLondonBroker = user.NonLondonBroker; 

            OfficeDto officeDto = null;
            var officeList = _consoleRepository.Query<Office>().ToList();
            if (user.DefaultOrigOffice != null)
            {
                var defaultOrigOfficeList = new List<OfficeDto>();
                defaultOrigOfficeList.Add(userDto.DefaultOrigOffice);

                defaultOrigOfficeList.AddRange(
                    officeList.Where(o => o.Id != userDto.DefaultOrigOffice.Id)
                    .Select(o => new OfficeDto {Title = o.Name, Id = o.Id}
                    ));
                userDto.DefaultOrigOfficeList = defaultOrigOfficeList.OrderBy(o=>o.Title).ToList();
            }

            if (user.PrimaryOffice != null)
            {
                var primaryOfficeList = new List<OfficeDto>();
                primaryOfficeList.Add(userDto.PrimaryOffice);

                primaryOfficeList.AddRange(
                    officeList.Where(o => o.Id != userDto.PrimaryOffice.Id)
                              .Select(o => new OfficeDto() { Id = o.Id, Title = o.Name }));

                userDto.PrimaryOfficeList = primaryOfficeList.OrderBy(o => o.Title).ToList();
            }

            if (userDto.DefaultOrigOfficeList == null || userDto.PrimaryOfficeList == null)
            {
                var officeDtoList = officeList.ToList().Select(o =>
                    {
                        return new OfficeDto { Id = o.Id, Title = o.Name };
                    }).OrderBy(o => o.Title).ToList();

                if (userDto.DefaultOrigOfficeList == null)
                    userDto.DefaultOrigOfficeList = officeDtoList;

                if (userDto.PrimaryOfficeList == null)
                    userDto.PrimaryOfficeList = officeDtoList;
            }

            if (userDto.DefaultUW != null)
            {
                var defaultUWDtoList = new List<UserDto>();
                defaultUWDtoList.Add(userDto.DefaultUW);

                UserDto defaultUW = null;
                defaultUWDtoList.AddRange(_consoleRepository.Query<User>().OrderBy(u=>u.DomainLogon).ToList().Select(u =>
                    {
                        if (u.Id != userDto.DefaultUW.Id)
                            defaultUW = new UserDto { Id = u.Id, DomainLogon = u.DomainLogon };
                        return defaultUW;
                    }).ToList());

                userDto.DefaultUWList = defaultUWDtoList;
            }

            if (userDto.DefaultUW == null)
            {
                var defaultUWDtoList = _consoleRepository.Query<User>().OrderBy(u => u.DomainLogon).ToList().Select(u =>
                    {
                        return new UserDto { Id = u.Id, DomainLogon = u.DomainLogon };
                    }).ToList();
                userDto.DefaultUWList = defaultUWDtoList;
            }


            userDto.TeamMemberships = (user.TeamMemberships != null && user.TeamMemberships.Any())
                                          ? user.TeamMemberships.Where(m => m.IsCurrent).OrderBy(tm=>tm.Team.Title).ToList().ConvertAll(tm =>
                new TeamMembershipDto
                {
                    TeamId = tm.TeamId,
                    EndDate = tm.EndDate,
                    Id = tm.Id,
                    PrimaryTeamMembership = tm.PrimaryTeamMembership,
                    IsCurrent = tm.IsCurrent,
                    StartDate = tm.StartDate,
                    Team = new TeamDto
                    {
                        Id = tm.Team.Id,
                        Title = tm.Team.Title,
                        DefaultDomicile = tm.Team.DefaultDomicile,
                        DefaultMOA = tm.Team.DefaultMOA,
                        PricingActuary = tm.Team.PricingActuary,
                        QuoteExpiryDaysDefault = tm.Team.QuoteExpiryDaysDefault,
                        DefaultPolicyType = tm.Team.DefaultPolicyType
                    }
                }) : null;

            userDto.AdditionalUsers = (user.AdditionalUsers != null && user.AdditionalUsers.Any())
                                          ? user.AdditionalUsers.OrderBy(u=>u.DomainLogon).ToList()
                                                .ConvertAll(ad => new UserDto { Id = ad.Id, DomainLogon = ad.DomainLogon })
                                          : null;
            userDto.AdditionalCOBs = (user.AdditionalCOBs != null && user.AdditionalCOBs.Any())
                                         ? user.AdditionalCOBs.OrderBy(cob => cob.Narrative).ToList()
                                               .ConvertAll(cob => new COBDto { Id = cob.Id, Narrative = cob.Narrative })
                                         : null;
            userDto.FilterCOBs = (user.FilterCOBs != null && user.FilterCOBs.Any())
                                     ? user.FilterCOBs.OrderBy(cob => cob.Narrative).ToList()
                                           .ConvertAll(cob => new COBDto { Id = cob.Id, Narrative = cob.Narrative })
                                     : null;
            userDto.AdditionalOffices = (user.AdditionalOffices != null && user.AdditionalOffices.Any())
                                            ? user.AdditionalOffices.OrderBy(o => o.Name).ToList()
                                                  .ConvertAll(o => new OfficeDto { Id = o.Id, Title = o.Name })
                                            : null;
            userDto.FilterMembers = (user.FilterMembers != null && user.FilterMembers.Any())
                                        ? user.FilterMembers.OrderBy(u => u.DomainLogon).ToList()
                                              .ConvertAll(fm => new UserDto { Id = fm.Id, DomainLogon = fm.DomainLogon })
                                        : null;
            userDto.FilterOffices = (user.FilterOffices != null && user.FilterOffices.Any())
                                        ? user.FilterOffices.OrderBy(o => o.Name).ToList()
                                              .ConvertAll(fo => new OfficeDto { Id = fo.Id, Title = fo.Name })
                                        : null;

            var requiredData = GetRequiredDataEditUser(user.DomainLogon, _consoleRepository);

            userDto.AllAdditionalUsers = requiredData.AllAdditionalUsers;
            userDto.AllAdditionalCOBs = requiredData.AllAdditionalCOBs;
            userDto.AllAdditionalOffices = requiredData.AllAdditionalOffices;
            userDto.AllFilterCOBs = requiredData.AllFilterCOBs;
            userDto.AllFilterMembers = requiredData.AllFilterMembers;
            userDto.AllFilterOffices = requiredData.AllFilterOffices;
            userDto.AllTeamMemberships = requiredData.AllTeamMemberships;
        }
        public void EditUser_Exception()
        {
            // Assign
            var userDto = new UserDto { Id = 0 };

            // Act
            var actualResult = _adminModuleManager.EditUser(userDto);

            // Assert
        }
        public ActionResult EditUser(UserDto userDto)
        {
            try
            {
                var updatedUser = _adminModuleManager.EditUser(userDto);

                return new JsonNetResult
                    {
                        Data = updatedUser
                    };
            }
			catch (ApplicationException ex) // TODO: Are all application exceptions due to bad requests ?
            {
                _logHandler.WriteLog(ex.ToString(), LogSeverity.Error, LogCategory.BusinessComponent);
                throw new HttpException(400, "Bad Request: " + ex.ToString());
            }
			catch (Exception ex) // TODO: Remove
            {
                _logHandler.WriteLog(ex.ToString(), LogSeverity.Error, LogCategory.BusinessComponent);
                throw new HttpException(500, "Server Error");
            }
        }