private async Task <CreateApplicationRequest> BuildRequest(StartApplicationRequest startApplicationRequest, string applicationType, Guid contactId, Guid organisationId, string referenceFormat) { var qnaResponse = await _qnaApiClient.StartApplications(startApplicationRequest); var sequences = await _qnaApiClient.GetAllApplicationSequences(qnaResponse.ApplicationId); var sections = sequences.Select(async sequence => await _qnaApiClient.GetSections(qnaResponse.ApplicationId, sequence.Id)).Select(t => t.Result).ToList(); return(new CreateApplicationRequest { ApplicationType = applicationType, QnaApplicationId = qnaResponse.ApplicationId, OrganisationId = organisationId, ApplicationReferenceFormat = referenceFormat, CreatingContactId = contactId, ApplySequences = sequences.Select(sequence => new ApplySequence { SequenceId = sequence.Id, Sections = sections.SelectMany(y => y.Where(x => x.SequenceNo == sequence.SequenceNo).Select(x => new ApplySection { SectionId = x.Id, SectionNo = x.SectionNo, Status = ApplicationSectionStatus.Draft, RequestedFeedbackAnswered = x.QnAData.RequestedFeedbackAnswered })).ToList(), Status = ApplicationSequenceStatus.Draft, IsActive = sequence.IsActive, SequenceNo = sequence.SequenceNo, NotRequired = sequence.NotRequired }).ToList() }); }
public async Task <IActionResult> StartApplication(PreviewViewModel previewViewModel) { if (!ModelState.IsValid) { return(View(previewViewModel)); } var applicationStartRequest = new StartApplicationRequest { UserReference = UserReference, WorkflowType = previewViewModel.ProjectType, ApplicationData = previewViewModel.ApplicationData }; var qnaResponse = new StartApplicationResponse(); try { qnaResponse = await _qnaApiClient.StartApplications(applicationStartRequest); } catch (HttpRequestException ex) { dynamic obj = JToken.Parse(ex.Message); if (obj.statusCode == "400") { ModelState.AddModelError(nameof(previewViewModel.ApplicationData), (string)obj.message); return(View(previewViewModel)); } } if (_sessionService.Exists("viewPreviewData")) { _sessionService.Remove("viewPreviewData"); } _sessionService.Set("viewPreviewData", JsonConvert.SerializeObject(previewViewModel)); var allApplicationSequences = await _qnaApiClient.GetAllApplicationSequences(qnaResponse.ApplicationId); var sequenceNos = string.Join(',', allApplicationSequences.Select(x => x.SequenceNo)); foreach (var _ in allApplicationSequences.Where(seq => seq.SequenceNo == previewViewModel.SequenceNo).Select(seq => new { })) { var sections = allApplicationSequences.Select(async sequence => await _qnaApiClient.GetSections(qnaResponse.ApplicationId, sequence.Id)).Select(t => t.Result).ToList(); return(RedirectToAction("Sequence", new { Id = qnaResponse.ApplicationId, sequenceNo = previewViewModel.SequenceNo })); } ModelState.AddModelError(nameof(previewViewModel.SequenceNo), $"Sequence number not found. Valid sequences are: {sequenceNos}"); return(View(previewViewModel)); }