private IActionResult FoundGetWithCompanyNumber(AddOrganisationFoundViewModel viewModel) { Organisation existingOrganisation = dataRepository.GetAll <Organisation>() .Where(o => o.CompanyNumber == viewModel.CompanyNumber) .Where(o => o.Status == OrganisationStatuses.Active) // Only redirect the user to an Active organisation (i.e. don't do this for Retired or Deleted organisations) .FirstOrDefault(); // We expect an ID for organisations that are in our database // We expect a Company Number for organisations that are not in our database (but are in the Companies House API) // If we've been given a Company Number, but the organisation IS in our database, // then redirect to this same page, but using the correct ID if (existingOrganisation != null) { return(RedirectToAction( "FoundGet", "AddOrganisationFound", new { id = existingOrganisation.GetEncryptedId(), query = viewModel.Query, sector = viewModel.Sector })); } PopulateViewModelBasedOnCompanyNumber(viewModel); return(View("Found", viewModel)); }
private static void PopulateViewModelBasedOnOrganisation(AddOrganisationFoundViewModel viewModel, Organisation organisation) { // Name viewModel.Name = organisation.OrganisationName; // Address OrganisationAddress organisationAddress = organisation.GetLatestAddress(); string addressString = organisationAddress?.GetAddressString() ?? ""; viewModel.AddressLines = addressString.Split(",").ToList(); // IsUkAddress bool?isUkAddress = organisationAddress?.IsUkAddress; if (isUkAddress.HasValue) { viewModel.IsUkAddress = isUkAddress.Value ? AddOrganisationIsUkAddress.Yes : AddOrganisationIsUkAddress.No; } else { viewModel.IsUkAddress = PostcodesIoApi.IsValidPostcode(organisationAddress?.GetPostCodeInAllCaps()) ? AddOrganisationIsUkAddress.Yes : (AddOrganisationIsUkAddress?)null; } // Company number if (!string.IsNullOrWhiteSpace(organisation.CompanyNumber)) { viewModel.CompanyNumber = organisation.CompanyNumber; } }
private static void ThrowIfBothIdAndCompanyNumberAreSpecified(AddOrganisationFoundViewModel viewModel) { if (!string.IsNullOrWhiteSpace(viewModel.Id) && !string.IsNullOrWhiteSpace(viewModel.CompanyNumber)) { // It is only valid to specify ONE of `id` or `companyNumber` throw new PageNotFoundException(); } }
private static void ThrowIfNeitherIdNorCompanyNumberIsSpecified(AddOrganisationFoundViewModel viewModel) { if (string.IsNullOrWhiteSpace(viewModel.Id) && string.IsNullOrWhiteSpace(viewModel.CompanyNumber)) { // One of `id` or `companyNumber` must be specified, otherwise we can't show a valid page // The user possibly typed in the link incorrectly throw new PageNotFoundException(); } }
private IActionResult FoundPostWithCompanyNumber(AddOrganisationFoundViewModel viewModel) { // IsUkAddress can be set by a hidden input (in which case it will be bound automatically) // Or it can be set by a GovUk_Radio button (in which case we need to use ParseAndValidate to get the value) // So, if the value hasn't already been bound, ParseAndValidate it if (!viewModel.IsUkAddress.HasValue) { viewModel.ParseAndValidateParameters(Request, m => m.IsUkAddress); } // If IsUkAddress still doesn't has a value on, then show an error if (!viewModel.IsUkAddress.HasValue) { PopulateViewModelBasedOnCompanyNumber(viewModel); return(View("Found", viewModel)); } Organisation existingOrganisation = dataRepository.GetAll <Organisation>() .Where(o => o.CompanyNumber == viewModel.CompanyNumber) .Where(o => o.Status == OrganisationStatuses.Active) // Only redirect the user to an Active organisation (i.e. don't do this for Retired or Deleted organisations) .FirstOrDefault(); // We expect an ID for organisations that are in our database // We expect a Company Number for organisations that are not in our database (but are in the Companies House API) // If we've been given a Company Number, but the organisation IS in our database, // then redirect to this same page, but using the correct ID if (existingOrganisation != null) { return(RedirectToAction( "FoundGet", "AddOrganisationFound", new { id = existingOrganisation.GetEncryptedId(), query = viewModel.Query, sector = viewModel.Sector })); } User user = ControllerHelper.GetGpgUserFromAspNetUser(User, dataRepository); Organisation organisation = organisationService.ImportOrganisationFromCompaniesHouse(viewModel.CompanyNumber, user); organisationService.UpdateIsUkAddressIfItIsNotAlreadySet(organisation.OrganisationId, viewModel.GetIsUkAddressAsBoolean()); UserOrganisation userOrganisation = registrationRepository.CreateRegistration(organisation, user, Url); return(RedirectToConfirmationPage(userOrganisation)); }
private AddOrganisationAlreadyRegisteringViewModel CreateAlreadyRegisteringViewModel( AddOrganisationFoundViewModel foundViewModel, UserOrganisation existingUserOrganisation) { var alreadyRegisteringViewModel = new AddOrganisationAlreadyRegisteringViewModel { Id = foundViewModel.Id, Query = foundViewModel.Query }; // UserOrganisation alreadyRegisteringViewModel.ExistingUserOrganisation = existingUserOrganisation; return(alreadyRegisteringViewModel); }
public IActionResult FoundGet(AddOrganisationFoundViewModel viewModel) { ControllerHelper.ThrowIfUserAccountRetiredOrEmailNotVerified(User, dataRepository); ThrowIfNeitherIdNorCompanyNumberIsSpecified(viewModel); ThrowIfBothIdAndCompanyNumberAreSpecified(viewModel); if (!string.IsNullOrWhiteSpace(viewModel.Id)) { return(FoundGetWithId(viewModel)); } else { return(FoundGetWithCompanyNumber(viewModel)); } }
private IActionResult FoundGetWithId(AddOrganisationFoundViewModel viewModel) { Organisation organisation = dataRepository.Get <Organisation>(viewModel.DeObfuscatedId); User user = ControllerHelper.GetGpgUserFromAspNetUser(User, dataRepository); UserOrganisation existingUserOrganisation = organisation.UserOrganisations .Where(uo => uo.UserId == user.UserId) .FirstOrDefault(); if (existingUserOrganisation != null) { AddOrganisationAlreadyRegisteringViewModel alreadyRegisteringViewModel = CreateAlreadyRegisteringViewModel(viewModel, existingUserOrganisation); return(View("AlreadyRegistering", alreadyRegisteringViewModel)); } PopulateViewModelBasedOnOrganisation(viewModel, organisation); return(View("Found", viewModel)); }
private void PopulateViewModelBasedOnCompanyNumber(AddOrganisationFoundViewModel viewModel) { CompaniesHouseCompany organisationFromCompaniesHouse = companiesHouseApi.GetCompany(viewModel.CompanyNumber); // Name viewModel.Name = organisationFromCompaniesHouse.CompanyName; // Address CompaniesHouseAddress coHoAddress = organisationFromCompaniesHouse.RegisteredOfficeAddress; OrganisationAddress organisationAddress = UpdateFromCompaniesHouseService.CreateOrganisationAddressFromCompaniesHouseAddress(coHoAddress); string addressString = organisationAddress?.GetAddressString() ?? ""; viewModel.AddressLines = addressString.Split(",").ToList(); // IsUkAddress string postCode = organisationFromCompaniesHouse.RegisteredOfficeAddress.PostalCode; viewModel.IsUkAddress = PostcodesIoApi.IsValidPostcode(postCode) ? AddOrganisationIsUkAddress.Yes : (AddOrganisationIsUkAddress?)null; }
private IActionResult FoundPostWithId(AddOrganisationFoundViewModel viewModel) { Organisation organisation = dataRepository.Get <Organisation>(viewModel.DeObfuscatedId); // IsUkAddress can be set by a hidden input (in which case it will be bound automatically) // Or it can be set by a GovUk_Radio button (in which case we need to use ParseAndValidate to get the value) // So, if the value hasn't already been bound, ParseAndValidate it if (!viewModel.IsUkAddress.HasValue) { viewModel.ParseAndValidateParameters(Request, m => m.IsUkAddress); } // If IsUkAddress still doesn't has a value on, then show an error if (!viewModel.IsUkAddress.HasValue) { PopulateViewModelBasedOnOrganisation(viewModel, organisation); return(View("Found", viewModel)); } organisationService.UpdateIsUkAddressIfItIsNotAlreadySet(organisation.OrganisationId, viewModel.GetIsUkAddressAsBoolean()); User user = ControllerHelper.GetGpgUserFromAspNetUser(User, dataRepository); UserOrganisation existingUserOrganisation = organisation.UserOrganisations .Where(uo => uo.UserId == user.UserId) .FirstOrDefault(); if (existingUserOrganisation != null) { AddOrganisationAlreadyRegisteringViewModel alreadyRegisteringViewModel = CreateAlreadyRegisteringViewModel(viewModel, existingUserOrganisation); return(View("AlreadyRegistering", alreadyRegisteringViewModel)); } UserOrganisation userOrganisation = registrationRepository.CreateRegistration(organisation, user, Url); return(RedirectToConfirmationPage(userOrganisation)); }
public IActionResult FoundPost(AddOrganisationFoundViewModel viewModel) { return(RedirectToActionPermanent("FoundPost", "AddOrganisationFound")); }