Exemplo n.º 1
0
        protected async Task <IActionResult> ValidateAndUpdatePageAnswer <RAVM>(SubmitAssessorPageAnswerCommand command,
                                                                                Func <Task <RAVM> > viewModelBuilder,
                                                                                string errorView) where RAVM : AssessorReviewAnswersViewModel
        {
            var validationResponse = await _assessorPageValidator.Validate(command);

            if (validationResponse.Errors.Any())
            {
                foreach (var error in validationResponse.Errors)
                {
                    ModelState.AddModelError(error.Field, error.ErrorMessage);
                }
            }

            var submittedPageOutcomeSuccessfully = false;

            if (ModelState.IsValid)
            {
                var userId   = HttpContext.User.UserId();
                var userName = HttpContext.User.UserDisplayName();

                submittedPageOutcomeSuccessfully = await _assessorApiClient.SubmitAssessorPageReviewOutcome(command.ApplicationId,
                                                                                                            command.SequenceNumber,
                                                                                                            command.SectionNumber,
                                                                                                            command.PageId,
                                                                                                            userId,
                                                                                                            userName,
                                                                                                            command.Status,
                                                                                                            command.ReviewComment);

                if (!submittedPageOutcomeSuccessfully)
                {
                    ModelState.AddModelError(string.Empty, "Unable to save outcome as this time");
                }
            }

            if (!submittedPageOutcomeSuccessfully)
            {
                var viewModel = await viewModelBuilder.Invoke();

                viewModel.Status               = command.Status;
                viewModel.OptionFailText       = command.OptionFailText;
                viewModel.OptionInProgressText = command.OptionInProgressText;
                viewModel.OptionPassText       = command.OptionPassText;

                return(View(errorView, viewModel));
            }
            else if (string.IsNullOrEmpty(command.NextPageId))
            {
                return(RedirectToAction("ViewApplication", "AssessorOverview", new { applicationId = command.ApplicationId }, $"sequence-{command.SequenceNumber}"));
            }
            else
            {
                return(RedirectToAction("ReviewPageAnswers", new { applicationId = command.ApplicationId, sequenceNumber = command.SequenceNumber, sectionNumber = command.SectionNumber, pageId = command.NextPageId }));
            }
        }