Example #1
0
        public IActionResult ChangeAddressGet(long id)
        {
            var organisation = SharedBusinessLogic.DataRepository.Get <Organisation>(id);

            if (!string.IsNullOrWhiteSpace(organisation.CompanyNumber))
            {
                try
                {
                    var organisationFromCompaniesHouse =
                        CompaniesHouseApi.GetCompanyAsync(organisation.CompanyNumber).Result;

                    var addressFromCompaniesHouse =
                        UpdateFromCompaniesHouseService.CreateOrganisationAddressFromCompaniesHouseAddress(
                            organisationFromCompaniesHouse.RegisteredOfficeAddress);

                    if (!organisation.GetLatestAddress().AddressMatches(addressFromCompaniesHouse))
                    {
                        return(OfferNewCompaniesHouseAddress(organisation, addressFromCompaniesHouse));
                    }
                }
                catch (Exception ex)
                {
                    // Use Manual Change page instead
                    WebService.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));
        }
Example #2
0
        public IActionResult ChangeNameGet(long id)
        {
            var organisation = SharedBusinessLogic.DataRepository.Get <Organisation>(id);

            if (!string.IsNullOrWhiteSpace(organisation.CompanyNumber))
            {
                try
                {
                    var organisationFromCompaniesHouse =
                        companiesHouseApi.GetCompanyAsync(organisation.CompanyNumber).Result;

                    var nameFromCompaniesHouse = organisationFromCompaniesHouse.CompanyName;

                    if (!string.Equals(organisation.OrganisationName, nameFromCompaniesHouse, StringComparison.Ordinal))
                    {
                        return(OfferNewCompaniesHouseName(organisation, nameFromCompaniesHouse));
                    }
                }
                catch (Exception ex)
                {
                    // Use Manual Change page instead
                    WebService.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 name matches Organisation name
            // ... send to the Manual Change page
            return(SendToManualChangePage(organisation));
        }
Example #3
0
        public IActionResult ChangeSicCodesGet(long id)
        {
            var organisation = SharedBusinessLogic.DataRepository.Get <Organisation>(id);

            if (!string.IsNullOrWhiteSpace(organisation.CompanyNumber))
            {
                try
                {
                    var organisationFromCompaniesHouse =
                        companiesHouseApi.GetCompanyAsync(organisation.CompanyNumber).Result;

                    var sicCodeIdsFromCompaniesHouse = organisationFromCompaniesHouse.SicCodes;
                    var sicCodesFromDatabase         =
                        organisation.GetLatestSicCodeIds().ToList();

                    if (!sicCodesFromDatabase.ToHashSet().SetEquals(sicCodeIdsFromCompaniesHouse.Select(s => s.ToInt32())))
                    {
                        return(OfferNewCompaniesHouseSicCodes(organisation, sicCodeIdsFromCompaniesHouse));
                    }
                }
                catch (Exception ex)
                {
                    // Use Manual Change page instead
                    WebService.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 SIC codes match Organisation SIC codes
            // ... send to the Manual Change page
            return(SendToManualChangePage(organisation));
        }
        public void UpdateOrganisationDetails(long organisationId)
        {
            _CustomLogger.Debug($"Loading organisation - OrganisationId({organisationId})");
            var organisation = _DataRepository.Get <Organisation>(organisationId);

            _CustomLogger.Debug($"Updating LastCheckedAgainstCompaniesHouse - OrganisationId({organisationId})");
            organisation.LastCheckedAgainstCompaniesHouse = VirtualDateTime.Now;
            _DataRepository.SaveChangesAsync().Wait();

            try
            {
                _CustomLogger.Debug($"Calling CoHo API - OrganisationId({organisationId})");
                var organisationFromCompaniesHouse =
                    _CompaniesHouseAPI.GetCompanyAsync(organisation.CompanyNumber).Result;

                _CustomLogger.Debug($"Starting transaction - OrganisationId({organisationId})");
                _DataRepository.BeginTransactionAsync(
                    async() =>
                {
                    try
                    {
                        _CustomLogger.Debug($"Updating SIC codes - OrganisationId({organisationId})");
                        UpdateSicCode(organisation, organisationFromCompaniesHouse);

                        _CustomLogger.Debug($"Updating Address - OrganisationId({organisationId})");
                        UpdateAddress(organisation, organisationFromCompaniesHouse);

                        _CustomLogger.Debug($"Updating Name - OrganisationId({organisationId})");
                        UpdateName(organisation, organisationFromCompaniesHouse);

                        _CustomLogger.Debug($"Saving - OrganisationId({organisationId})");
                        _DataRepository.SaveChangesAsync().Wait();
                        _DataRepository.CommitTransaction();

                        _CustomLogger.Debug($"Saved - OrganisationId({organisationId})");
                    }
                    catch (Exception ex)
                    {
                        var message =
                            $"Update from Companies House: Failed to update database, organisation id = {organisationId}";
                        _CustomLogger.Error(message, ex);
                        _DataRepository.RollbackTransaction();
                    }
                })
                .Wait();
            }
            catch (Exception ex)
            {
                var message =
                    $"Update from Companies House: Failed to get company data from companies house, organisation id = {organisationId}";
                _CustomLogger.Error(message, ex);
            }
        }
        private void PopulateViewModelWithCompanyFromCompaniesHouse(
            AdminChangeCompaniesHouseOptInOutViewModel viewModel, Organisation organisation)
        {
            CompaniesHouseCompany companiesHouseCompany;
            try
            {
                companiesHouseCompany = companiesHouseApi.GetCompanyAsync(organisation.CompanyNumber).Result;
            }
            catch (Exception)
            {
                throw new Exception("This organisation doesn't have a companies house record.");
            }

            viewModel.CompaniesHouseCompany = companiesHouseCompany;
        }