public static CreateEmployeeAsUserRequest Map(EmployeeViewModel viewModel, Guid actioningUserId) { var siteId = default(long); if (viewModel.UserSiteGroupId.HasValue) siteId = viewModel.UserSiteGroupId.Value; if (viewModel.UserSiteId.HasValue) siteId = viewModel.UserSiteId.Value; if (!viewModel.EmployeeId.HasValue) throw new Exception("Invalid Field - EmployeeId undefined"); if (!viewModel.UserRoleId.HasValue) throw new Exception("Invalid Field - UserRoleId undefined"); return new CreateEmployeeAsUserRequest { EmployeeId = viewModel.EmployeeId.Value, NewUserId = viewModel.UserId, CompanyId = viewModel.CompanyId, RoleId = viewModel.UserRoleId.Value, SiteId = siteId, ActioningUserId = actioningUserId, MainSiteId = siteId, // ?? Is this right?? EmployeeContact = new EmployeeContactDetail() { Telephone1 = viewModel.Telephone, Email = viewModel.Email }, PermissionsForAllSites = viewModel.UserPermissionsApplyToAllSites }; }
protected IEnumerable<CreateEmergencyContactRequest> MapEmergencyContactDetails(EmployeeViewModel viewModel) { var result = new List<CreateEmergencyContactRequest>(); foreach (var request in viewModel.EmergencyContactDetails) { var addEmergencyContactRequest = new CreateEmergencyContactRequest() { Title = request.Title, Forename = request.Forename, Surname = request.Surname, Relationship = request.Relationship, WorkTelephone = request.WorkTelephone, HomeTelephone = request.HomeTelephone, PreferredContactNumber = request.PreferredContactNumber, MobileTelephone = request.MobileTelephone, SameAddressAsEmployee = request.SameAddressAsEmployee, Address1 = request.Address1, Address2 = request.Address2, Address3 = request.Address3, Town = request.Town, County = request.County, CountryId = request.EmergencyContactCountryId.GetValueOrDefault(), PostCode = request.PostCode }; result.Add(addEmergencyContactRequest); } return result; }
public UpdateEmployeeRequest Map(EmployeeViewModel viewModel, Guid userId) { var result = new UpdateEmployeeRequest() { EmployeeId = viewModel.EmployeeId.GetValueOrDefault(), EmployeeReference = viewModel.EmployeeReference, Title = (string.IsNullOrEmpty(viewModel.NameTitle) || viewModel.NameTitle.ToLower().Contains("select") ? string.Empty : viewModel.NameTitle), Forename = viewModel.Forename, Surname = viewModel.Surname, MiddleName = viewModel.MiddleName, PreviousSurname = viewModel.PreviousSurname, Sex = viewModel.Sex, DateOfBirth = viewModel.DateOfBirth.GetValueOrDefault(), NationalityId = viewModel.NationalityId, HasDisability = viewModel.HasDisability, DisabilityDescription = viewModel.DisabilityDescription, SiteId = viewModel.SiteId, JobTitle = viewModel.JobTitle, EmploymentStatusId = viewModel.EmploymentStatusId, NINumber = viewModel.NINumber, PassportNumber = viewModel.PassportNumber, PPSNumber = viewModel.PPSNumber, WorkVisaNumber = viewModel.WorkVisaNumber, WorkVisaExpirationDate = viewModel.WorkVisaExpirationDate.GetValueOrDefault(), DrivingLicenseNumber = viewModel.DrivingLicenseNumber, DrivingLicenseExpirationDate = viewModel.DrivingLicenseExpirationDate.GetValueOrDefault(), HasCompanyVehicle = viewModel.HasCompanyVehicle, CompanyVehicleTypeId = viewModel.CompanyVehicleTypeId, CompanyVehicleRegistration = viewModel.CompanyVehicleRegistration, Address1 = viewModel.Address1, Address2 = viewModel.Address2, Address3 = viewModel.Address3, Town = viewModel.Town, County = viewModel.County, CountryId = viewModel.CountryId.GetValueOrDefault(), Postcode = viewModel.Postcode, Telephone = viewModel.Telephone, Mobile = viewModel.Mobile, Email = viewModel.Email, UserId = userId, CompanyId = viewModel.CompanyId, ContactDetailId = viewModel.ContactDetailId, IsRiskAssessor = viewModel.IsRiskAssessor, DoNotSendTaskOverdueNotifications = viewModel.DoNotSendTaskOverdueNotifications, DoNotSendTaskCompletedNotifications = viewModel.DoNotSendTaskCompletedNotifications, DoNotSendReviewDueNotification = viewModel.DoNotSendReviewDueNotification, RiskAssessorHasAccessToAllSites = viewModel.RiskAssessorHasAccessToAllSites, RiskAssessorSiteId = viewModel.RiskAssessorSiteId.HasValue ? viewModel.RiskAssessorSiteId.Value : 0, NotificationType = MapNotificationType(viewModel.NotificationType), NotificiationFrequency = viewModel.NotificationFrequency }; result.UserId = userId; return result; }
public void given_employee_model_has_no_site_or_role_values_then_no_validation_errors() { var employeeViewModel = new EmployeeViewModel() { UserRoleId = null, UserSiteGroupId = null, UserSiteGroups = null, UserPermissionsApplyToAllSites = false }; var validationContext = new ValidationContext(employeeViewModel, null, null); var validationResults = new List<ValidationResult>(); Validator.TryValidateObject(employeeViewModel, validationContext, validationResults); }
public static EmergencyContactDetailViewModel CreateFrom(EmergencyContactViewModel emergencyContact, EmployeeViewModel employeeViewModel) { var contactDetail = emergencyContact ?? new EmergencyContactViewModel(); return new EmergencyContactDetailViewModel() { ContactDetail = contactDetail, Titles = employeeViewModel.Titles, Countries =employeeViewModel.Countries, SameAddressAsEmployee = emergencyContact != null ? emergencyContact.SameAddressAsEmployee : false, EmployeeAddress1 = employeeViewModel.Address1, EmployeeAddress2 = employeeViewModel.Address2, EmployeeAddress3 = employeeViewModel.Address3, EmployeeTown = employeeViewModel.Town, EmployeeCounty = employeeViewModel.County, EmployeeCountryId = employeeViewModel.CountryId, EmployeePostCode = employeeViewModel.Postcode }; }
public ActionResult Create(EmployeeViewModel employeeViewModel) { if (!ModelState.IsValid) { return ReturnInvalidSaveEmployeeViewResult(employeeViewModel); } var addEmployeeRequest = new AddEmployeeRequestMapper().Map(employeeViewModel, CurrentUser.UserId); var response =_employeeService.Add(addEmployeeRequest); if(response.Success) { return RedirectToEmployeeIndexViewResult(response.EmployeeId, employeeViewModel.CompanyId); } { response.Errors.ToList() .ForEach( err => ModelState.AddModelError(err.PropertyName, err.ErrorMessage)); return ReturnInvalidSaveEmployeeViewResult(employeeViewModel); } }
public void given_employee_model_has_no_user_role_but_site_group_then_validation_error() { var employeeViewModel = new EmployeeViewModel() { UserRoleId = null, UserSiteGroupId = 1, UserSiteGroups = null, UserPermissionsApplyToAllSites = false, Forename = "name", Surname = "name", Sex = "M", SiteId = 1 }; var validationContext = new ValidationContext(employeeViewModel, null, null); var validationResults = new List<ValidationResult>(); Validator.TryValidateObject(employeeViewModel, validationContext, validationResults); Assert.That(validationResults.Count > 0); Assert.That(validationResults[0].ErrorMessage, Is.EqualTo("The User Role must be selected for the user")); }
private static AddEmployeeRequest MatchesEmployeeViewModelProperties(EmployeeViewModel employeeViewModel) { return It.Is<AddEmployeeRequest>(y => y.EmployeeReference == employeeViewModel.EmployeeReference && y.Title == employeeViewModel.NameTitle && y.Forename == employeeViewModel.Forename && y.Surname == employeeViewModel.Surname && y.Sex == employeeViewModel.Sex && y.NationalityId == employeeViewModel.NationalityId ); }
public static EmployeeViewModelBuilder Create() { _viewModel = new EmployeeViewModel(); return new EmployeeViewModelBuilder(); }
public EmployeeViewModel GetViewModel() { var employeeDto = GetEmployee(); var countries = GetCountries(employeeDto); var nationalities = GetNationalities(employeeDto); var sites = GetSites(); var sexes = GetSexes(); var titles = GetTitles(); var employmentStatuses = GetEmploymentStatuses(); var companyVehicleTypes = GetCompanyVehicleTypes(); AddUsersViewModel addUsersViewModel = _addUsersViewModelFactory .WithCompanyId(_companyId) .GetViewModel(); var viewModel = new EmployeeViewModel { EmployeeReference = employeeDto.EmployeeReference, NameTitle = employeeDto.Title, Forename = employeeDto.Forename, Surname = employeeDto.Surname, MiddleName = employeeDto.MiddleName, PreviousSurname = employeeDto.PreviousSurname, Sex = employeeDto.Sex, DateOfBirth = employeeDto.DateOfBirth, NationalityId = employeeDto.Nationality != null ? employeeDto.Nationality.Id : default(int), HasDisability = employeeDto.HasDisability, DisabilityDescription = employeeDto.DisabilityDescription, NINumber = employeeDto.NINumber, PassportNumber = employeeDto.PassportNumber, PPSNumber = employeeDto.PPSNumber, WorkVisaNumber = employeeDto.WorkVisaNumber, WorkVisaExpirationDate = employeeDto.WorkVisaExpirationDate, DrivingLicenseNumber = employeeDto.DrivingLicenseNumber, DrivingLicenseExpirationDate = employeeDto.DrivingLicenseExpirationDate, HasCompanyVehicle = employeeDto.HasCompanyVehicle, CompanyVehicleTypeId = employeeDto.CompanyVehicleTypeId, CompanyVehicleRegistration = employeeDto.CompanyVehicleRegistration, SiteId = employeeDto.SiteId, JobTitle = employeeDto.JobTitle, EmploymentStatusId = employeeDto.EmploymentStatusId, CompanyId = _companyId, EmployeeId = _employeeId, Countries = countries, Nationalities = nationalities, Sites = sites, Sexes = sexes, Titles = titles, EmploymentStatuses = employmentStatuses, CompanyVehicleTypes = companyVehicleTypes, Address1 = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.Address1 : null, Address2 = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.Address2 : null, Address3 = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.Address3 : null, Town = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.Town : null, County = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.County : null, CountryId = employeeDto.MainContactDetails != null && employeeDto.MainContactDetails.Country != null ? (int?)employeeDto.MainContactDetails.Country.Id : null, Postcode = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.PostCode : null, Telephone = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.Telephone1 : null, Mobile = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.Telephone2 : null, Email = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.Email : null, IsExistingUser = employeeDto.User != null, CanChangeEmail = employeeDto.CanChangeEmail, ContactDetailId = employeeDto.MainContactDetails != null ? employeeDto.MainContactDetails.Id : 0, // UserId = employeeDto.User != null ? employeeDto.User.Id : Guid.Empty, IsPendingRegistration = employeeDto.User != null && employeeDto.User.Deleted == false && employeeDto.User.IsRegistered == false, EmergencyContactDetails = MapEmergencyContactDetails(employeeDto.EmergencyContactDtos), UserSites = addUsersViewModel.Sites, UserSiteGroups = addUsersViewModel.SiteGroups, UserRoles = addUsersViewModel.Roles, NotificationType = MapNotificationType(employeeDto.NotificationType), NotificationFrequency = employeeDto.NotificationFrequency }; if (employeeDto.User != null && employeeDto.User.Deleted == false) { viewModel.UserId = employeeDto.User.Id; viewModel.UserRoleId = employeeDto.User.Role != null ? employeeDto.User.Role.Id : (Guid?)null; viewModel.UserRoleDescription = employeeDto.User.Role != null ? employeeDto.User.Role.Description : null; if (employeeDto.User.SiteStructureElement != null) { viewModel.UserPermissionsApplyToAllSites = employeeDto.User.SiteStructureElement.IsMainSite; if (viewModel.UserPermissionsApplyToAllSites == false) { if (employeeDto.User.SiteStructureElement.GetType() == typeof (SiteGroupDto)) { viewModel.UserSiteGroupId = employeeDto.User.SiteStructureElement.Id; } else if (employeeDto.User.SiteStructureElement.GetType() == typeof (SiteDto)) { viewModel.UserSiteId = employeeDto.User.SiteStructureElement.Id; } } } if (_currentUser != null && employeeDto.User.Role.Description == "User Admin" && _currentUser.UserId == employeeDto.User.Id) { viewModel.CanChangeRoleDdl = false; } } if (employeeDto.RiskAssessor == null) return viewModel; viewModel.IsRiskAssessor = true; viewModel.DoNotSendTaskOverdueNotifications = employeeDto.RiskAssessor.DoNotSendTaskOverdueNotifications; viewModel.DoNotSendTaskCompletedNotifications = employeeDto.RiskAssessor.DoNotSendTaskCompletedNotifications; viewModel.DoNotSendReviewDueNotification = employeeDto.RiskAssessor.DoNotSendReviewDueNotification; viewModel.IsRiskAssessorAssignedToRiskAssessments = _riskAssessorService.HasRiskAssessorGotOutstandingRiskAssessments(employeeDto.RiskAssessor.Id, viewModel.CompanyId); viewModel.RiskAssessorHasAccessToAllSites = employeeDto.RiskAssessor.HasAccessToAllSites; if (employeeDto.RiskAssessor.Site == null) return viewModel; viewModel.RiskAssessorSite = employeeDto.RiskAssessor.Site.Name; viewModel.RiskAssessorSiteId = employeeDto.RiskAssessor.Site.Id; return viewModel; }
public ActionResult Update(EmployeeViewModel employeeViewModel) { if (!ModelState.IsValid) { var errors = ModelState.GetErrorMessagesWithPropertyName().Distinct().ToArray(); return ReturnInvalidSaveEmployeeViewResult(employeeViewModel); } var updateEmployeeRequest = new UpdateEmployeeRequestMapper().Map(employeeViewModel, CurrentUser.UserId); var response = _employeeService.Update(updateEmployeeRequest); if (response.Success) { if (employeeViewModel.UserId == Guid.Empty) { // attempt to create user if (!CreateUpdateReinstateUser(employeeViewModel)) return ReturnInvalidSaveEmployeeViewResult(employeeViewModel); } return RedirectToEmployeeIndexViewResult(employeeViewModel.EmployeeId.GetValueOrDefault(), employeeViewModel.CompanyId); } { response.Errors.ToList() .ForEach( err => ModelState.AddModelError(err.PropertyName, err.ErrorMessage)); return ReturnInvalidSaveEmployeeViewResult(employeeViewModel); } }
private ActionResult ReturnInvalidSaveEmployeeViewResult(EmployeeViewModel employeeViewModel) { var viewModel = _employeeViewModelFactory .WithEmployeeId(employeeViewModel.EmployeeId) .WithCompanyId(employeeViewModel.CompanyId) .GetViewModel(); return View("Index", viewModel); }
private bool CreateUpdateReinstateUser(EmployeeViewModel employeeViewModel) { if ( (!employeeViewModel.UserRoleId.HasValue || employeeViewModel.UserRoleId.Value == Guid.Empty ) || (!employeeViewModel.UserSiteId.HasValue || employeeViewModel.UserSiteId.Value == 0 ) && (!employeeViewModel.UserSiteGroupId.HasValue || employeeViewModel.UserSiteGroupId.Value == 0) && (!employeeViewModel.UserPermissionsApplyToAllSites) ) { // assume we aren't trying to create a userCreateUser return true; } EmployeeDto employee = _employeeService.GetEmployee(employeeViewModel.EmployeeId.Value, employeeViewModel.CompanyId); if (employee.User == null) { //CREATING NEW USER var registerEmployeeAsUserRequest = AddUserForEmployeeMapper.Map(employeeViewModel, CurrentUser.UserId); if (!RegistrationAttemptIsValid(employee, registerEmployeeAsUserRequest)) return false; RegisterEmployeeAsUser(employee, registerEmployeeAsUserRequest); } else if (employee.User.Deleted) { // REINSTATE USER var user = _userService.GetByIdAndCompanyIdIncludeDeleted(employee.User.Id, employee.User.CompanyId); if (_newRegistrationRequestService.HasEmailBeenRegistered(user.Employee.MainContactDetails.Email)) { ModelState.AddModelError("User", "Sorry you are unable to reinstate this user: the email address has been registered to another user"); return true; } _userService.ReinstateUser(employee.User.Id, CurrentUser.CompanyId, CurrentUser.UserId); } else { //UPDATING USER //set role and site var roleSiteRequest = new SetUserRoleAndSiteRequest(); roleSiteRequest.ActioningUserId = CurrentUser.UserId; roleSiteRequest.CompanyId = CurrentUser.CompanyId; roleSiteRequest.RoleId = employeeViewModel.UserRoleId.Value; roleSiteRequest.SiteId = employeeViewModel.UserSiteId ?? employeeViewModel.UserSiteGroupId ?? 0; roleSiteRequest.UserToUpdateId = employee.User != null ? employee.User.Id : new Guid(); roleSiteRequest.PermissionsApplyToAllSites = employeeViewModel.UserPermissionsApplyToAllSites; _userService.SetRoleAndSite(roleSiteRequest); } return true; }