private async Task <IActionResult> SaveViewModel(CertificateOptionViewModel vm, string returnToIfModelNotValid, RedirectToActionResult nextAction, string action)
        {
            var username = GetUsernameFromClaim();

            Logger.LogDebug($"Save View Model for CertificateOptionViewModel for {username} with values: {GetModelValues(vm)}");

            var certificate = await CertificateApiClient.GetCertificate(vm.Id);

            var certData = JsonConvert.DeserializeObject <CertificateData>(certificate.CertificateData);

            SessionService.RemoveRedirectedFromVersion();

            var sessionString = SessionService.Get(nameof(CertificateSession));

            if (sessionString == null)
            {
                Logger.LogDebug($"Session for CertificateOptionViewModel requested by {username} has been lost. Redirecting to Search Index");
                return(RedirectToAction("Index", "Search"));
            }
            var certSession = JsonConvert.DeserializeObject <CertificateSession>(sessionString);

            if (!ModelState.IsValid)
            {
                vm.Options = certSession.Options;
                Logger.LogDebug($"Model State not valid for CertificateOptionViewModel requested by {username} with Id {certificate.Id}. Errors: {ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage)}");
                return(View(returnToIfModelNotValid, vm));
            }

            var updatedCertificate = vm.GetCertificateFromViewModel(certificate, certData);

            try
            {
                await CertificateApiClient.UpdateCertificate(new UpdateCertificateRequest(updatedCertificate) { Username = username, Action = action });
            }
            catch
            {
                Logger.LogError($"Unable to update certificate with Id {certificate.Id}.");
                return(RedirectToAction("Error", "Home"));
            }

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

            if (SessionService.GetRedirectToCheck())
            {
                Logger.LogDebug($"Certificate for CertificateOptionViewModel requested by {username} with Id {certificate.Id} redirecting back to Certificate Check.");
                return(new RedirectToActionResult("Check", "CertificateCheck", null));
            }

            Logger.LogDebug($"Certificate for CertificateOptionViewModel requested by {username} with Id {certificate.Id} redirecting to {nextAction.ControllerName} {nextAction.ActionName}");
            return(nextAction);
        }
        private async Task <IActionResult> SaveViewModel(CertificateOptionViewModel vm, string returnToIfModelNotValid, RedirectToActionResult nextAction, string action)
        {
            var username = ContextAccessor.HttpContext.User.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn")?.Value;

            Logger.LogInformation($"Save View Model for CertificateOptionViewModel for {username} with values: {GetModelValues(vm)}");

            var certificate = await CertificateApiClient.GetCertificate(vm.Id);

            var certData = JsonConvert.DeserializeObject <CertificateData>(certificate.CertificateData);

            var sessionString = SessionService.Get("CertificateSession");

            if (sessionString == null)
            {
                Logger.LogInformation($"Session for CertificateOptionViewModel requested by {username} has been lost. Redirecting to Search Index");
                return(RedirectToAction("Index", "Search"));
            }
            var certSession = JsonConvert.DeserializeObject <CertificateSession>(sessionString);

            if (!ModelState.IsValid)
            {
                vm.FamilyName = certData.LearnerFamilyName;
                vm.GivenNames = certData.LearnerGivenNames;
                vm.Options    = certSession.Options;
                Logger.LogInformation($"Model State not valid for CertificateOptionViewModel requested by {username} with Id {certificate.Id}. Errors: {ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage)}");
                return(View(returnToIfModelNotValid, vm));
            }

            var updatedCertificate = vm.GetCertificateFromViewModel(certificate, certData);

            await CertificateApiClient.UpdateCertificate(new UpdateCertificateRequest(updatedCertificate) { Username = username, Action = action });

            Logger.LogInformation($"Certificate for CertificateOptionViewModel requested by {username} with Id {certificate.Id} updated.");

            if (SessionService.Exists("redirecttocheck") && bool.Parse(SessionService.Get("redirecttocheck")))
            {
                Logger.LogInformation($"Certificate for CertificateOptionViewModel requested by {username} with Id {certificate.Id} redirecting back to Certificate Check.");
                return(new RedirectToActionResult("Check", "CertificateCheck", null));
            }

            Logger.LogInformation($"Certificate for CertificateOptionViewModel requested by {username} with Id {certificate.Id} redirecting to {nextAction.ControllerName} {nextAction.ActionName}");
            return(nextAction);
        }
Exemplo n.º 3
0
        private async Task <IActionResult> SaveViewModel(CertificateVersionViewModel vm, RedirectToActionResult nextAction, string action)
        {
            var username        = GetUsernameFromClaim();
            var epaoid          = GetEpaOrgIdFromClaim();
            var redirectToCheck = SessionService.GetRedirectToCheck();

            Logger.LogDebug($"Save View Model for CertificateVersionViewModel for {username} with values: {GetModelValues(vm)}");

            var certificate = await CertificateApiClient.GetCertificate(vm.Id);

            var certData = JsonConvert.DeserializeObject <CertificateData>(certificate.CertificateData);

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

            if (!ModelState.IsValid)
            {
                Logger.LogDebug($"Model State not valid for CertificateVersionViewModel requested by {username} with Id {certificate.Id}. Errors: {ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage)}");
                return(View("~/Views/Certificate/Version.cshtml", vm));
            }

            var standardVersion = await _standardVersionClient.GetStandardVersionById(vm.StandardUId);

            var approvedStandardVersions = await _standardVersionClient.GetEpaoRegisteredStandardVersions(epaoid, certSession.StandardCode);

            var options = await _standardVersionClient.GetStandardOptions(vm.StandardUId);

            if (!approvedStandardVersions.Any(v => v.StandardUId == vm.StandardUId))
            {
                SessionService.Set("AttemptedStandardVersion", vm.StandardUId);

                return(RedirectToAction("NotApprovedToAssess", "CertificateVersionNotApproved", redirectToCheck ? new { redirectToCheck } : null));
            }

            var versionChanged = certificate.StandardUId != vm.StandardUId;
            // Edge case to cater to back buttons where user can change version without setting an option
            var optionNotSet = string.IsNullOrEmpty(certData.CourseOption) && options != null && options.HasOptions();

            if (!versionChanged && !optionNotSet && redirectToCheck)
            {
                // if version hasn't changed, and option set if required, don't need to update options.
                return(new RedirectToActionResult("Check", "CertificateCheck", null));
            }

            certSession.StandardUId = vm.StandardUId;

            // To pass in to inherited method.
            vm.SelectedStandardVersion = standardVersion;
            vm.SelectedStandardOptions = options;
            var updatedCertificate = vm.GetCertificateFromViewModel(certificate, certData);

            try
            {
                await CertificateApiClient.UpdateCertificate(new UpdateCertificateRequest(updatedCertificate) { Username = username, Action = action });
            }
            catch
            {
                Logger.LogError($"Unable to update certificate with Id {certificate.Id}.");
                return(RedirectToAction("Error", "Home"));
            }

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

            if (options != null && options.HasOptions())
            {
                certSession.Options = options.CourseOption.ToList();
                SessionService.Set(nameof(CertificateSession), certSession);

                if (options.OnlyOneOption())
                {
                    if (redirectToCheck)
                    {
                        return(new RedirectToActionResult("Check", "CertificateCheck", null));
                    }

                    return(new RedirectToActionResult("Declare", "CertificateDeclaration", null));
                }

                object routeValues = null;
                if (redirectToCheck)
                {
                    routeValues = new { redirecttocheck = true };
                }

                SessionService.SetRedirectedFromVersion(true);
                return(new RedirectToActionResult("Option", "CertificateOption", routeValues));
            }
            else
            {
                certSession.Options = null;
                SessionService.Set(nameof(CertificateSession), certSession);
            }

            if (redirectToCheck)
            {
                Logger.LogDebug($"Certificate for CertificateVersionViewModel requested by {username} with Id {certificate.Id} redirecting back to Certificate Check.");
                return(new RedirectToActionResult("Check", "CertificateCheck", null));
            }

            Logger.LogDebug($"Certificate for CertificateVersionViewModel requested by {username} with Id {certificate.Id} redirecting to {nextAction.ControllerName} {nextAction.ActionName}");
            return(nextAction);
        }