Beispiel #1
0
        private async Task <IActionResult> LoadViewModel(string view)
        {
            var username = GetUsernameFromClaim();

            Logger.LogDebug($"Load View Model for CertificateVersionViewModel for {username}");

            var viewModel = new CertificateVersionViewModel();

            CheckAndSetRedirectToCheck(viewModel);

            if (!TryGetCertificateSession("CertificateVersionViewModel", username, out CertificateSession certSession))
            {
                return(RedirectToAction("Index", "Search"));
            }

            var certificate = await CertificateApiClient.GetCertificate(certSession.CertificateId, includeLogs : true);

            if (certSession.Versions == null || certSession.Versions.Count == 0)
            {
                return(RedirectToAction("Index", "Search"));
            }

            if (certSession.Versions.Count == 1)
            {
                // Only 1 version no need for a selection
                var singularStandard = certSession.Versions.First();
                var options          = await _standardVersionClient.GetStandardOptions(singularStandard.StandardUId);

                if (options != null & options.HasOptions())
                {
                    certSession.StandardUId = singularStandard.StandardUId;
                    certSession.Options     = options.CourseOption.ToList();
                    SessionService.Set(nameof(CertificateSession), certSession);
                    return(RedirectToAction("Option", "CertificateOption"));
                }

                return(RedirectToAction("Declare", "CertificateDeclaration"));
            }

            Logger.LogDebug($"Got Certificate for CertificateVersionViewModel requested by {username} with Id {certificate.Id}");

            viewModel.FromCertificate(certificate, certSession.Versions);

            var attemptedStandardVersion = SessionService.Get("AttemptedStandardVersion");

            if (attemptedStandardVersion != null)
            {
                viewModel.StandardUId = attemptedStandardVersion;
                SessionService.Remove("AttemptedStandardVersion");
            }

            Logger.LogDebug($"Got View Model of type CertificateVersionViewModel requested by {username}");

            return(View(view, viewModel));
        }
        public async Task <IActionResult> Start(CertificateStartViewModel vm)
        {
            _sessionService.Remove(nameof(CertificateSession));

            var ukprn    = _contextAccessor.HttpContext.User.FindFirst("http://schemas.portal.com/ukprn")?.Value;
            var username = _contextAccessor.HttpContext.User.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn")?.Value;

            List <string>          options      = new List <string>();
            var                    singleOption = string.Empty;
            List <StandardVersion> versions     = new List <StandardVersion>();

            var startCertificateRequest = new StartCertificateRequest()
            {
                UkPrn        = int.Parse(ukprn),
                StandardCode = vm.StdCode,
                Uln          = vm.Uln,
                Username     = username
            };

            async Task RetrieveAndPopulateStandardOptions(string standardUId)
            {
                var optionsResult = await _standardVersionClient.GetStandardOptions(standardUId);

                if (optionsResult != null && optionsResult.HasOptions())
                {
                    if (optionsResult.OnlyOneOption())
                    {
                        singleOption = optionsResult.CourseOption.Single();
                        startCertificateRequest.CourseOption = singleOption;
                    }

                    options = optionsResult.CourseOption.ToList();
                }
            }

            var learner = await _learnerApiClient.GetLearnerRecord(vm.StdCode, vm.Uln);

            var versionsResult = await _standardVersionClient.GetStandardVersionsByLarsCode(vm.StdCode);

            if (learner.VersionConfirmed && !string.IsNullOrEmpty(learner.Version))
            {
                var version = versionsResult.First(v => v.Version == learner.Version);

                startCertificateRequest.StandardUId = version.StandardUId;
                versions.Add(version);

                if (!string.IsNullOrEmpty(learner.CourseOption))
                {
                    options.Add(learner.CourseOption);
                    startCertificateRequest.CourseOption = learner.CourseOption;
                }
                else
                {
                    await RetrieveAndPopulateStandardOptions(version.StandardUId);
                }
            }
            else
            {
                versions = versionsResult.ToList();

                if (versionsResult.Count() == 1)
                {
                    startCertificateRequest.StandardUId = versionsResult.First().StandardUId;

                    await RetrieveAndPopulateStandardOptions(versionsResult.First().StandardUId);
                }
            }

            _logger.LogInformation($"Start of Certificate Start for ULN {vm.Uln} and Standard Code: {vm.StdCode} by user {username}");

            var cert = await _certificateApiClient.Start(startCertificateRequest);

            var certificateSession = new CertificateSession()
            {
                CertificateId = cert.Id,
                Uln           = vm.Uln,
                StandardCode  = vm.StdCode,
                StandardUId   = startCertificateRequest.StandardUId,
                Versions      = Mapper.Map <List <StandardVersionViewModel> >(versions),
                Options       = options
            };

            _sessionService.Set(nameof(CertificateSession), certificateSession);
            _logger.LogInformation($"New Certificate received for ULN {vm.Uln} and Standard Code: {vm.StdCode} with ID {cert.Id}");

            if (versions.Count() > 1)
            {
                return(RedirectToAction("Version", "CertificateVersion"));
            }
            else if (!string.IsNullOrWhiteSpace(singleOption))
            {
                return(RedirectToAction("Declare", "CertificateDeclaration"));
            }
            else if (options.Count > 1)
            {
                return(RedirectToAction("Option", "CertificateOption"));
            }

            return(RedirectToAction("Declare", "CertificateDeclaration"));
        }