public IActionResult ChangeAddressGet(long id) { Organisation organisation = dataRepository.Get <Organisation>(id); if (!string.IsNullOrWhiteSpace(organisation.CompanyNumber)) { try { CompaniesHouseCompany organisationFromCompaniesHouse = companiesHouseApi.GetCompany(organisation.CompanyNumber); OrganisationAddress addressFromCompaniesHouse = UpdateFromCompaniesHouseService.CreateOrganisationAddressFromCompaniesHouseAddress( organisationFromCompaniesHouse.RegisteredOfficeAddress); OrganisationAddress latestAddress = organisation.GetLatestAddress(); if (latestAddress != null && !latestAddress.AddressMatches(addressFromCompaniesHouse)) { return(OfferNewCompaniesHouseAddress(organisation, addressFromCompaniesHouse)); } } catch (Exception ex) { // Use Manual Change page instead CustomLogger.Warning("Error from Companies House API", ex); } } // In all other cases... // * Organisation doesn't have a Companies House number // * CoHo API returns an error // * CoHo address matches Organisation address // ... send to the Manual Change page return(SendToManualChangePage(organisation)); }
public void UpdateAddress(Organisation organisation, CompaniesHouseCompany organisationFromCompaniesHouse) { CompaniesHouseAddress companiesHouseAddress = organisationFromCompaniesHouse.RegisteredOfficeAddress; OrganisationAddress newOrganisationAddressFromCompaniesHouse = CreateOrganisationAddressFromCompaniesHouseAddress(companiesHouseAddress); OrganisationAddress oldOrganisationAddress = organisation.GetLatestAddress(); if (oldOrganisationAddress != null) { if (oldOrganisationAddress.AddressMatches(newOrganisationAddressFromCompaniesHouse) || IsNewOrganisationAddressNullOrEmpty(newOrganisationAddressFromCompaniesHouse)) { return; } oldOrganisationAddress.Status = AddressStatuses.Retired; oldOrganisationAddress.StatusDate = VirtualDateTime.Now; } newOrganisationAddressFromCompaniesHouse.OrganisationId = organisation.OrganisationId; organisation.OrganisationAddresses.Add(newOrganisationAddressFromCompaniesHouse); dataRepository.Insert(newOrganisationAddressFromCompaniesHouse); }