Exemplo n.º 1
0
        public async Task <IActionResult> VerifyOrganisationDetails()
        {
            var applicationDetails = _sessionService.Get <ApplicationDetails>(ApplicationDetailsKey);
            var providerDetails    = applicationDetails.UkrlpLookupDetails;

            var existingApplicationStatuses = await _applicationApiClient.GetExistingApplicationStatus(providerDetails.UKPRN);

            if (existingApplicationStatuses.Any(x => x.Status == ApplicationStatus.InProgress))
            {
                return(RedirectToAction("ApplicationInProgress", "RoatpShutterPages", new ExistingApplicationViewModel {
                    UKPRN = providerDetails.UKPRN
                }));
            }
            else if (existingApplicationStatuses.Any(x => x.Status == ApplicationStatus.Submitted || x.Status == ApplicationStatus.GatewayAssessed))
            {
                return(RedirectToAction("ApplicationPreviouslySubmitted", "RoatpShutterPages", new ExistingApplicationViewModel {
                    UKPRN = providerDetails.UKPRN
                }));
            }

            CompaniesHouseSummary companyDetails = null;
            Charity charityDetails = null;

            if (providerDetails.VerifiedByCompaniesHouse)
            {
                var companiesHouseVerification = providerDetails.VerificationDetails.FirstOrDefault(x =>
                                                                                                    x.VerificationAuthority == VerificationAuthorities.CompaniesHouseAuthority);

                companyDetails = await _companiesHouseApiClient.GetCompanyDetails(companiesHouseVerification.VerificationId);

                if ((companyDetails.Directors == null || companyDetails.Directors.Count == 0) &&
                    (companyDetails.PersonsWithSignificantControl == null ||
                     companyDetails.PersonsWithSignificantControl.Count == 0))
                {
                    companyDetails.ManualEntryRequired = true;
                }

                if (companyDetails.Status == CompaniesHouseSummary.ServiceUnavailable)
                {
                    return(RedirectToAction("CompaniesHouseNotAvailable", "RoatpShutterPages"));
                }
                else if (companyDetails.Status == CompaniesHouseSummary.CompanyStatusNotFound)
                {
                    return(RedirectToAction("CompanyNotFound", "RoatpShutterPages"));
                }
                else if (!CompaniesHouseValidator.CompaniesHouseStatusValid(providerDetails.UKPRN, companyDetails.Status, _logger))
                {
                    return(RedirectToAction("CompanyNotFound", "RoatpShutterPages"));
                }

                applicationDetails.CompanySummary = companyDetails;
            }

            if (applicationDetails.UkrlpLookupDetails.VerifiedbyCharityCommission)
            {
                var charityCommissionVerification = providerDetails.VerificationDetails.FirstOrDefault(x =>
                                                                                                       x.VerificationAuthority == VerificationAuthorities.CharityCommissionAuthority);

                string verificationId = charityCommissionVerification.VerificationId;
                if (verificationId.Contains("-"))
                {
                    verificationId = verificationId.Substring(0, verificationId.IndexOf("-"));
                }

                if (IsEnglandAndWalesCharityCommissionNumber(verificationId))
                {
                    bool isValidCharityNumber = int.TryParse(verificationId, out var charityNumber);
                    if (!isValidCharityNumber)
                    {
                        return(RedirectToAction("CharityNotFound", "RoatpShutterPages"));
                    }

                    try
                    {
                        charityDetails = await _outerApiClient.GetCharityDetails(charityNumber);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error fetching charity details for charity number `{charityNumber}`", charityNumber);
                        return(RedirectToAction("CharityCommissionNotAvailable", "RoatpShutterPages"));
                    }

                    if (!charityDetails.IsActivelyTrading)
                    {
                        return(RedirectToAction("CharityNotFound", "RoatpShutterPages"));
                    }

                    applicationDetails.CharitySummary = Mapper.Map <CharityCommissionSummary>(charityDetails);
                }
                else
                {
                    applicationDetails.CharitySummary = new CharityCommissionSummary
                    {
                        TrusteeManualEntryRequired = true
                    };
                }
            }

            var roatpRegisterStatus = await _roatpApiClient.GetOrganisationRegisterStatus(applicationDetails.UKPRN);

            applicationDetails.RoatpRegisterStatus = roatpRegisterStatus;

            _sessionService.Set(ApplicationDetailsKey, applicationDetails);

            if (ProviderEligibleToChangeRoute(roatpRegisterStatus))
            {
                return(RedirectToAction("ProviderAlreadyOnRegister"));
            }

            return(RedirectToAction("SelectApplicationRoute"));
        }
        public async Task <IActionResult> RefreshDirectorsPscs(Guid applicationId, string ukprn, string companyNumber)
        {
            try
            {
                _logger.LogInformation($"RefreshDirectorsPscs: Retrieving company details applicationId {applicationId} | Company Number : {companyNumber}");
                var timer = new Stopwatch();
                timer.Start();
                var companyDetails = await _companiesHouseApiClient.GetCompanyDetails(companyNumber);

                var timeToCallCompanyDetails = $"{timer.ElapsedMilliseconds} ms";

                switch (companyDetails.Status)
                {
                case CompaniesHouseSummary.ServiceUnavailable:
                    _logger.LogInformation($"Issue refreshing directors/pscs - applicationId {applicationId} | Company Number : {companyNumber} | Status : Service Unavailable");
                    return(RedirectToAction("CompaniesHouseNotAvailable", "RoatpShutterPages"));

                case CompaniesHouseSummary.CompanyStatusNotFound:
                    _logger.LogInformation(
                        $"Issue refreshing directors/pscs - applicationId {applicationId} | Company Number : {companyNumber} | Status : Company Status Not Found");
                    return(RedirectToAction("CompanyNotFoundRefresh", "RoatpShutterPages", new { companyNumber }));
                }

                if (!CompaniesHouseValidator.CompaniesHouseStatusValid(ukprn, companyDetails.Status, _logger))
                {
                    _logger.LogInformation($"Issue refreshing directors/pscs - applicationId {applicationId} | UKPRN : {ukprn} | Status : Companies House status not valid: {companyDetails.Status}");
                    return(RedirectToAction("CompanyNotFoundRefresh", "RoatpShutterPages", new { companyNumber }));
                }

                var applicationDetails = new Domain.Roatp.ApplicationDetails {
                    CompanySummary = companyDetails
                };

                _logger.LogInformation($"RefreshDirectorsPscs: updating organisation directors/pscs applicationId {applicationId}");

                var success = await _organisationApiClient.UpdateDirectorsAndPscs(ukprn, companyDetails.Directors, companyDetails.PersonsWithSignificantControl, User.GetUserId());

                if (!success)
                {
                    _logger.LogInformation($"Organisation director/pscs update failed - applicationId {applicationId}");
                    return(RedirectToAction("CompaniesHouseNotAvailable", "RoatpShutterPages"));
                }

                var directorsAnswers = RoatpPreambleQuestionBuilder.CreateCompaniesHouseWhosInControlQuestions(applicationDetails);

                _logger.LogInformation($"RefreshDirectorsPscs: resetting page answers for companies, applicationId {applicationId}");
                var resetSection1_3 = _qnaApiClient.ResetPageAnswersBySequenceAndSectionNumber(applicationId, RoatpWorkflowSequenceIds.YourOrganisation, RoatpWorkflowSectionIds.YourOrganisation.WhosInControl, RoatpWorkflowPageIds.WhosInControl.CompaniesHouseStartPage);
                var resetSection3_4 = _qnaApiClient.ResetPageAnswersBySection(applicationId, RoatpWorkflowSequenceIds.CriminalComplianceChecks, RoatpWorkflowSectionIds.CriminalComplianceChecks.CheckOnWhosInControl);
                await Task.WhenAll(resetSection1_3, resetSection3_4);

                _logger.LogInformation($"RefreshDirectorsPscs: updating page answers for companies, applicationId {applicationId}");
                await _qnaApiClient.UpdatePageAnswers(applicationId, RoatpWorkflowSequenceIds.YourOrganisation, RoatpWorkflowSectionIds.YourOrganisation.WhosInControl, RoatpWorkflowPageIds.WhosInControl.CompaniesHouseStartPage, directorsAnswers.ToList <Answer>());

                var timeToDoEntireCall = $"{timer.ElapsedMilliseconds} ms";
                timer.Stop();
                _logger.LogInformation($"RefreshDirectorsPscs: all updates completed for {applicationId} - entire call timespan: {timeToDoEntireCall}, Company call timespan: {timeToCallCompanyDetails}");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error when processing directors/pscs - applicationId {applicationId}");
                return(RedirectToAction("CompaniesHouseNotAvailable", "RoatpShutterPages"));
            }

            return(RedirectToAction("StartPage", "RoatpWhosInControlApplication", new { applicationId }));
        }