public async Task <IActionResult> Section(Guid applicationId, int sequenceNo, int sectionNo, BackViewModel backViewModel)
        {
            var application = await _applyApiClient.GetApplication(applicationId);

            var organisation = await _apiClient.GetOrganisation(application.OrganisationId);

            var applySequence = application.ApplyData.Sequences.Single(x => x.SequenceNo == sequenceNo);
            var applySection  = applySequence.Sections.Single(x => x.SectionNo == sectionNo);

            var sequence = await _qnaApiClient.GetSequence(application.ApplicationId, applySequence.SequenceId);

            var section = await _qnaApiClient.GetSection(application.ApplicationId, applySection.SectionId);

            var applicationData = await _qnaApiClient.GetApplicationDataDictionary(application.ApplicationId);

            var sectionVm = new SectionViewModel(application, organisation, section, applySection, applicationData, backViewModel.BackAction, backViewModel.BackController, backViewModel.BackOrganisationId);

            var activeApplicationStatuses = new List <string> {
                ApplicationStatus.Submitted, ApplicationStatus.Resubmitted
            };
            var activeSequenceStatuses = new List <string> {
                ApplicationSequenceStatus.Submitted, ApplicationSequenceStatus.Resubmitted
            };

            if (activeApplicationStatuses.Contains(application.ApplicationStatus) && activeSequenceStatuses.Contains(applySequence?.Status))
            {
                if (applySection.Status != ApplicationSectionStatus.Evaluated)
                {
                    await _applyApiClient.StartApplicationSectionReview(applicationId, sequence.SequenceNo, section.SectionNo, _contextAccessor.HttpContext.User.UserDisplayName());
                }

                return(View(nameof(Section), sectionVm));
            }
            else
            {
                return(View($"{nameof(Section)}_ReadOnly", sectionVm));
            }
        }
Example #2
0
        public async Task <IActionResult> Page(Guid Id, int sequenceNo, int sectionNo, string pageId, string __redirectAction, string __summaryLink = "Show")
        {
            var sequence = await _qnaApiClient.GetSequenceBySequenceNo(Id, sequenceNo);

            var section = await _qnaApiClient.GetSectionBySectionNo(Id, sequenceNo, sectionNo);

            PageViewModel viewModel   = null;
            var           returnUrl   = Request.Headers["Referer"].ToString();
            var           pageContext = OrganisationName;

            if (!ModelState.IsValid)
            {
                var page = JsonConvert.DeserializeObject <Page>((string)TempData["InvalidPage"]);
                page = GetDataFedOptions(page);

                var errorMessages = !ModelState.IsValid
                    ? ModelState.SelectMany(k => k.Value.Errors.Select(e => new ValidationErrorDetail()
                {
                    ErrorMessage = e.ErrorMessage,
                    Field        = k.Key
                })).ToList()
                    : null;

                if (page.ShowTitleAsCaption)
                {
                    page.Title = section.Title;
                }

                UpdateValidationDetailsForAddress(page, errorMessages);
                viewModel = new PageViewModel(Id, sequenceNo, sectionNo, pageId, page, pageContext, __redirectAction,
                                              returnUrl, errorMessages, __summaryLink);
            }
            else
            {
                try
                {
                    var page = await _qnaApiClient.GetPageBySectionNo(Id, sequenceNo, sectionNo, pageId);

                    if (page != null && (!page.Active))
                    {
                        var nextPage = page.Next.FirstOrDefault(p => p.Conditions is null || p.Conditions.Count == 0);

                        if (nextPage?.ReturnId != null && nextPage?.Action == "NextPage")
                        {
                            pageId = nextPage.ReturnId;
                            return(RedirectToAction("Page",
                                                    new { Id, sequenceNo, sectionNo, pageId, __redirectAction }));
                        }
                        else
                        {
                            return(RedirectToAction("Section", new { Id, sequenceNo, sectionNo }));
                        }
                    }

                    page = GetDataFedOptions(page);

                    if (page.ShowTitleAsCaption)
                    {
                        page.Title = section.Title;
                    }

                    viewModel = new PageViewModel(Id, sequenceNo, sectionNo, page.PageId, page, pageContext, __redirectAction,
                                                  returnUrl, null, __summaryLink);
                }
                catch (Exception ex)
                {
                    if (ex.Message.Equals("Could not find the page", StringComparison.OrdinalIgnoreCase))
                    {
                        return(RedirectToAction("Applications"));
                    }
                    throw ex;
                }
            }

            var applicationData = await _qnaApiClient.GetApplicationDataDictionary(Id);

            ReplaceApplicationDataPropertyPlaceholdersInQuestions(viewModel.Questions, applicationData);

            if (viewModel.AllowMultipleAnswers)
            {
                return(View("~/Views/Application/Pages/MultipleAnswers.cshtml", viewModel));
            }

            return(View("~/Views/Application/Pages/Index.cshtml", viewModel));
        }
        public async Task <CreateOrganisationContactCommand> GatherAnswersForOrganisationAndContactForApplication(Guid applicationId)
        {
            var application = await _applyApiClient.GetApplication(applicationId);

            var applicationData = await _qnaApiClient.GetApplicationDataDictionary(application?.ApplicationId ?? Guid.Empty);

            var organisation = await _apiApiClient.GetOrganisation(application?.OrganisationId ?? Guid.Empty);

            var organisationContacts = await _apiApiClient.GetOrganisationContacts(organisation?.Id ?? Guid.Empty);

            var applyingContact = organisationContacts?.FirstOrDefault(c => c.Id.ToString().Equals(application?.CreatedBy, StringComparison.InvariantCultureIgnoreCase));

            if (application is null || applicationData is null || organisation is null || applyingContact is null)
            {
                return(new CreateOrganisationContactCommand());
            }

            var tradingName          = GetAnswer(applicationData, "trading_name");
            var useTradingNameString = GetAnswer(applicationData, "use_trading_name");
            var useTradingName       = "yes".Equals(useTradingNameString, StringComparison.InvariantCultureIgnoreCase) || "true".Equals(useTradingNameString, StringComparison.InvariantCultureIgnoreCase) || "1".Equals(useTradingNameString, StringComparison.InvariantCultureIgnoreCase);

            // get a contact by their contact name parts (application started May 2019 to date)
            var contactGivenNames = GetAnswer(applicationData, "contact_given_name");
            var contactFamilyName = GetAnswer(applicationData, "contact_family_name");

            if (string.IsNullOrEmpty(contactGivenNames) && string.IsNullOrEmpty(contactFamilyName))
            {
                // get a contact by their contact name (application started before May 2019)
                var contactName = GetAnswer(applicationData, "contact_name");
                if (!string.IsNullOrEmpty(contactName))
                {
                    var matches = Regex.Matches(contactName, "^*([,'.-a-zA-z]{1,})");
                    if (matches.Count() > 0)
                    {
                        var contactNameParts = matches.Cast <Group>()
                                               .Where(o => o.Value != string.Empty)
                                               .Select(o => o.Value);

                        contactGivenNames = contactNameParts.Count() == 1
                            ? contactNameParts.First()
                            : string.Join(" ", contactNameParts.Take(contactNameParts.Count() - 1));

                        // using the (Unknown) placeholder for the family name in cases where only a single word was entered
                        contactFamilyName = contactNameParts.Count() == 1
                            ? "(Unknown)"
                            : contactNameParts.Last();
                    }
                }
            }

            // get a contact address which is a single question with multiple answers
            var contactAddress = GetJsonAnswer(applicationData, "contact_address");

            // handle both a contact address which is a single question with multiple answers or multiple questions with a single answer
            var contactAddress1 = contactAddress?["AddressLine1"].ToString() ?? GetAnswer(applicationData, "contact_address1");
            var contactAddress2 = contactAddress?["AddressLine2"].ToString() ?? GetAnswer(applicationData, "contact_address2");
            var contactAddress3 = contactAddress?["AddressLine3"].ToString() ?? GetAnswer(applicationData, "contact_address3");
            var contactAddress4 = contactAddress?["AddressLine4"].ToString() ?? GetAnswer(applicationData, "contact_address4");
            var contactPostcode = contactAddress?["Postcode"].ToString() ?? GetAnswer(applicationData, "contact_postcode");

            var contactEmail       = GetAnswer(applicationData, "contact_email");
            var contactPhoneNumber = GetAnswer(applicationData, "contact_phone_number");
            var companyUkprn       = GetAnswer(applicationData, "company_ukprn");

            var companyNumber = GetAnswer(applicationData, "company_number");

            if ("no".Equals(companyNumber, StringComparison.InvariantCultureIgnoreCase))
            {
                companyNumber = null;
            }

            var charityNumber = GetAnswer(applicationData, "charity_number");

            if ("no".Equals(charityNumber, StringComparison.InvariantCultureIgnoreCase))
            {
                charityNumber = null;
            }

            var standardWebsite = GetAnswer(applicationData, "standard_website");

            var command = new CreateOrganisationContactCommand
                              (organisation.Id,
                              organisation.EndPointAssessorName,
                              organisation.OrganisationType.Type,
                              organisation.EndPointAssessorUkprn,
                              organisation.EndPointAssessorOrganisationId,
                              organisation.OrganisationData.RoEPAOApproved,
                              tradingName,
                              useTradingName,
                              contactGivenNames,
                              contactFamilyName,
                              contactAddress1,
                              contactAddress2,
                              contactAddress3,
                              contactAddress4,
                              contactPostcode,
                              contactEmail,
                              contactPhoneNumber,
                              companyUkprn,
                              companyNumber,
                              charityNumber,
                              standardWebsite,
                              applyingContact.Id,
                              applyingContact.GivenNames,
                              applyingContact.FamilyName,
                              applyingContact.Email,
                              organisationContacts.Where(c => c.Email != applyingContact.Email).Select(c => c.Email).ToList(),
                              organisation.OrganisationData?.FHADetails?.FinancialDueDate,
                              organisation.OrganisationData?.FHADetails?.FinancialExempt);

            return(command);
        }