Exemplo n.º 1
0
        public async Task <ProcessPageEntity> ProcessPage(string form, string path, string subPath, IQueryCollection queryParamters)
        {
            if (string.IsNullOrEmpty(path))
            {
                _sessionHelper.RemoveSessionGuid();
            }

            var sessionGuid = _sessionHelper.GetSessionGuid();

            if (string.IsNullOrEmpty(sessionGuid))
            {
                sessionGuid = Guid.NewGuid().ToString();
                _sessionHelper.SetSessionGuid(sessionGuid);
            }

            var baseForm = await _schemaFactory.Build(form);

            if (baseForm == null)
            {
                return(null);
            }

            if (!baseForm.IsAvailable(_environment.EnvironmentName))
            {
                throw new ApplicationException($"Form: {form} is not available in this Environment: {_environment.EnvironmentName.ToS3EnvPrefix()}");
            }

            var formData = _distributedCache.GetString(sessionGuid);

            if (formData == null && path != baseForm.FirstPageSlug && (!baseForm.HasDocumentUpload || path != FileUploadConstants.DOCUMENT_UPLOAD_URL_PATH))
            {
                return new ProcessPageEntity
                       {
                           ShouldRedirect = true,
                           TargetPage     = baseForm.FirstPageSlug
                       }
            }
            ;

            if (string.IsNullOrEmpty(path))
            {
                return new ProcessPageEntity
                       {
                           ShouldRedirect = true,
                           TargetPage     = baseForm.FirstPageSlug
                       }
            }
            ;

            if (formData != null && path == baseForm.FirstPageSlug)
            {
                var convertedFormData = JsonConvert.DeserializeObject <FormAnswers>(formData);

                if (form.ToLower() != convertedFormData.FormName.ToLower())
                {
                    _distributedCache.Remove(sessionGuid);
                }
            }

            var page = baseForm.GetPage(_pageHelper, path);

            if (page == null)
            {
                throw new ApplicationException($"Requested path '{path}' object could not be found for form '{form}'");
            }

            await baseForm.ValidateFormSchema(_pageHelper, form, path);

            List <object> searchResults = null;

            if (subPath.Equals(LookUpConstants.Automatic) || subPath.Equals(LookUpConstants.Manual))
            {
                var convertedAnswers = new FormAnswers {
                    Pages = new List <PageAnswers>()
                };

                if (!string.IsNullOrEmpty(formData))
                {
                    convertedAnswers = JsonConvert.DeserializeObject <FormAnswers>(formData);
                }

                if (convertedAnswers.FormData.ContainsKey($"{path}{LookUpConstants.SearchResultsKeyPostFix}"))
                {
                    searchResults = ((IEnumerable <object>)convertedAnswers.FormData[$"{path}{LookUpConstants.SearchResultsKeyPostFix}"])?.ToList();
                }
            }

            if (page.Elements.Any(_ => _.Type == EElementType.PaymentSummary))
            {
                var data = await _mappingService.Map(sessionGuid, form);

                var paymentAmount = await _payService.GetFormPaymentInformation(data, form, page);

                page.Elements.First(_ => _.Type == EElementType.PaymentSummary).Properties.Value = paymentAmount.Settings.Amount;
            }

            if (page.HasIncomingGetValues)
            {
                var convertedAnswers = new FormAnswers {
                    Pages = new List <PageAnswers>()
                };
                if (!string.IsNullOrEmpty(formData))
                {
                    convertedAnswers = JsonConvert.DeserializeObject <FormAnswers>(formData);
                }

                var result = _incomingDataHelper.AddIncomingFormDataValues(page, queryParamters, convertedAnswers);
                _pageHelper.SaveNonQuestionAnswers(result, form, path, sessionGuid);
            }

            if (page.HasPageActionsGetValues)
            {
                await _actionsWorkflow.Process(page.PageActions, null, form);
            }

            if (page.Elements.Any(_ => _.Type.Equals(EElementType.Booking)))
            {
                var bookingProcessEntity = await _bookingService.Get(baseForm.BaseURL, page, sessionGuid);

                if (bookingProcessEntity.BookingHasNoAvailableAppointments)
                {
                    return(new ProcessPageEntity
                    {
                        ShouldRedirect = true,
                        TargetPage = BookingConstants.NO_APPOINTMENT_AVAILABLE
                    });
                }

                searchResults = bookingProcessEntity.BookingInfo;
            }

            var viewModel = await GetViewModel(page, baseForm, path, sessionGuid, subPath, searchResults);

            return(new ProcessPageEntity
            {
                ViewModel = viewModel
            });
        }
Exemplo n.º 2
0
        public async Task <ProcessPageEntity> ProcessPage(string form, string path, string subPath, IQueryCollection queryParameters)
        {
            if (string.IsNullOrEmpty(path))
            {
                _sessionHelper.RemoveSessionGuid();
            }

            var sessionGuid = _sessionHelper.GetSessionGuid();

            if (string.IsNullOrEmpty(sessionGuid))
            {
                sessionGuid = Guid.NewGuid().ToString();
                _sessionHelper.SetSessionGuid(sessionGuid);
            }

            var baseForm = await _schemaFactory.Build(form);

            if (baseForm is null)
            {
                return(null);
            }

            if (!_formAvailabilityService.IsAvailable(baseForm.EnvironmentAvailabilities, _environment.EnvironmentName))
            {
                _logger.LogWarning($"Form: {form} is not available in this Environment: {_environment.EnvironmentName.ToS3EnvPrefix()}");
                return(null);
            }

            if (string.IsNullOrEmpty(path))
            {
                return new ProcessPageEntity {
                           ShouldRedirect = true, TargetPage = baseForm.FirstPageSlug
                }
            }
            ;

            var formData = _distributedCache.GetString(sessionGuid);

            if (string.IsNullOrEmpty(formData) && !path.Equals(baseForm.FirstPageSlug) && (!baseForm.HasDocumentUpload || !path.Equals(FileUploadConstants.DOCUMENT_UPLOAD_URL_PATH)))
            {
                return new ProcessPageEntity {
                           ShouldRedirect = true, TargetPage = baseForm.FirstPageSlug
                }
            }
            ;

            if (!string.IsNullOrEmpty(formData) && path.Equals(baseForm.FirstPageSlug))
            {
                var convertedFormData = JsonConvert.DeserializeObject <FormAnswers>(formData);

                if (!form.Equals(convertedFormData.FormName, StringComparison.OrdinalIgnoreCase))
                {
                    _distributedCache.Remove(sessionGuid);
                }
            }

            var page = baseForm.GetPage(_pageHelper, path);

            if (page is null)
            {
                throw new ApplicationException($"Requested path '{path}' object could not be found for form '{form}'");
            }

            List <object> searchResults    = null;
            var           convertedAnswers = new FormAnswers {
                Pages = new List <PageAnswers>()
            };

            if (!string.IsNullOrEmpty(formData))
            {
                convertedAnswers = JsonConvert.DeserializeObject <FormAnswers>(formData);
            }

            if (page.Elements.Any(_ => _.Type.Equals(EElementType.Summary)))
            {
                var journeyPages = baseForm.GetReducedPages(convertedAnswers);
                foreach (var schemaPage in journeyPages)
                {
                    await _schemaFactory.TransformPage(schemaPage, convertedAnswers);
                }
            }
            else
            {
                await _schemaFactory.TransformPage(page, convertedAnswers);
            }

            if (subPath.Equals(LookUpConstants.Automatic) || subPath.Equals(LookUpConstants.Manual))
            {
                if (convertedAnswers.FormData.ContainsKey($"{path}{LookUpConstants.SearchResultsKeyPostFix}"))
                {
                    searchResults = ((IEnumerable <object>)convertedAnswers.FormData[$"{path}{LookUpConstants.SearchResultsKeyPostFix}"])?.ToList();
                }
            }

            if (page.HasIncomingGetValues)
            {
                var result = _incomingDataHelper.AddIncomingFormDataValues(page, queryParameters, convertedAnswers);
                _pageHelper.SaveNonQuestionAnswers(result, form, path, sessionGuid);
            }

            if (page.HasPageActionsGetValues)
            {
                await _actionsWorkflow.Process(page.PageActions, null, form);
            }

            if (page.Elements.Any(_ => _.Type.Equals(EElementType.Booking)))
            {
                var bookingProcessEntity = await _bookingService.Get(baseForm.BaseURL, page, sessionGuid);

                if (bookingProcessEntity.BookingHasNoAvailableAppointments)
                {
                    return(new ProcessPageEntity
                    {
                        ShouldRedirect = true,
                        TargetPage = BookingConstants.NO_APPOINTMENT_AVAILABLE
                    });
                }

                searchResults = bookingProcessEntity.BookingInfo;
            }

            var viewModel = await GetViewModel(page, baseForm, path, sessionGuid, subPath, searchResults);

            return(new ProcessPageEntity {
                ViewModel = viewModel
            });
        }