public async Task <IActionResult> NotApprovedToAssess(bool?redirectToCheck = false) { var standardUId = _sessionService.Get("AttemptedStandardVersion"); if (standardUId == null) { return(RedirectToAction("Index", "Search")); } var standardVersion = await _standardVersionClient.GetStandardVersionById(standardUId); var viewModel = new CertificateVersionNotApprovedViewModel() { AttemptedVersion = standardVersion.Version, BackToCheckPage = redirectToCheck.Value }; return(View("~/Views/Certificate/VersionNotApproved.cshtml", viewModel)); }
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); }