public override async Task OnActionExecutionAsync( ActionExecutingContext context, ActionExecutionDelegate next) { if (_serviceParameters.AuthenticationType != AuthenticationType.Employer) { await next(); return; } if (_serviceParameters.AuthenticationType == AuthenticationType.Employer && context.HttpContext.Request.Query.ContainsKey("transferSenderId")) { if (context.RouteData.Values["controller"].ToString().Equals("SelectReservations", StringComparison.CurrentCultureIgnoreCase) && context.RouteData.Values["action"].ToString().Equals("SelectReservation", StringComparison.CurrentCultureIgnoreCase) ) { await next(); return; } context.Result = new RedirectToRouteResult(RouteNames.Error500, null); return; } if (!context.RouteData.Values.TryGetValue("employerAccountId", out var employerAccountId)) { context.Result = new RedirectToRouteResult(RouteNames.Error500, null); return; } var decodedAccountId = _encodingService.Decode(employerAccountId?.ToString(), EncodingType.AccountId); var result = await _mediator.Send(new GetLegalEntitiesQuery { AccountId = decodedAccountId }); if (result.AccountLegalEntities.Any(entity => !entity.IsLevy && entity.AgreementType != AgreementType.NonLevyExpressionOfInterest) || !result.AccountLegalEntities.Any()) { var homeLink = _urlHelper.GenerateDashboardUrl(employerAccountId?.ToString()); var model = new NonEoiHoldingViewModel { HomeLink = homeLink }; var viewResult = new ViewResult { ViewName = "NonEoiHolding", ViewData = new ViewDataDictionary(((Controller)context.Controller).ViewData) { Model = model } }; context.Result = viewResult; return; } await next(); }
public async Task <IActionResult> SelectReservation( ReservationsRouteModel routeModel, SelectReservationViewModel viewModel) { var backUrl = _urlHelper.GenerateCohortDetailsUrl(routeModel.UkPrn, routeModel.EmployerAccountId, viewModel.CohortReference); try { var apprenticeshipTrainingRouteName = RouteNames.EmployerSelectCourse; CacheReservationEmployerCommand cacheReservationEmployerCommand; Guid?userId = null; if (routeModel.UkPrn.HasValue) { var response = await _mediator.Send(new GetProviderCacheReservationCommandQuery { AccountLegalEntityPublicHashedId = routeModel.AccountLegalEntityPublicHashedId, CohortRef = routeModel.CohortReference, CohortId = _encodingService.Decode(routeModel.CohortReference, EncodingType.CohortReference), UkPrn = routeModel.UkPrn.Value }); cacheReservationEmployerCommand = response.Command; apprenticeshipTrainingRouteName = RouteNames.ProviderApprenticeshipTraining; } else { var userAccountIdClaim = HttpContext.User.Claims.First(c => c.Type.Equals(EmployerClaims.IdamsUserIdClaimTypeIdentifier)); userId = Guid.Parse(userAccountIdClaim.Value); cacheReservationEmployerCommand = await BuildEmployerReservationCacheCommand( routeModel.EmployerAccountId, routeModel.AccountLegalEntityPublicHashedId, viewModel.CohortReference, viewModel.ProviderId); } var redirectResult = await CheckCanAutoReserve(cacheReservationEmployerCommand.AccountId, viewModel.TransferSenderId, cacheReservationEmployerCommand.AccountLegalEntityPublicHashedId, routeModel.UkPrn ?? viewModel.ProviderId, viewModel.CohortReference, routeModel.EmployerAccountId, userId); if (!string.IsNullOrEmpty(redirectResult)) { if (redirectResult == RouteNames.Error500) { return(RedirectToRoute(redirectResult)); } return(Redirect(redirectResult)); } var availableReservationsResult = await _mediator.Send( new GetAvailableReservationsQuery { AccountId = cacheReservationEmployerCommand.AccountId }); if (availableReservationsResult.Reservations != null && availableReservationsResult.Reservations.Any()) { viewModel.AvailableReservations = availableReservationsResult.Reservations .Select(reservation => new AvailableReservationViewModel(reservation)); viewModel.AccountId = cacheReservationEmployerCommand.AccountId; viewModel.BackLink = backUrl; return(View(ViewNames.Select, viewModel)); } await _mediator.Send(cacheReservationEmployerCommand); routeModel.Id = cacheReservationEmployerCommand.Id; return(RedirectToRoute(apprenticeshipTrainingRouteName, routeModel)); } catch (ValidationException e) { _logger.LogWarning(e, "Validation error trying to render select reservation."); return(RedirectToRoute(RouteNames.Error500)); } catch (ProviderNotAuthorisedException e) { _logger.LogWarning(e, $"Provider (UKPRN: {e.UkPrn}) does not has access to create a reservation for legal entity for account (Id: {e.AccountId})."); return(View("NoPermissions", backUrl)); } catch (ReservationLimitReachedException) { return(View("ReservationLimitReached", backUrl)); } catch (NonEoiUserAccessDeniedException) { var homeLink = _urlHelper.GenerateDashboardUrl(routeModel.EmployerAccountId); var model = new NonEoiHoldingViewModel { HomeLink = homeLink }; return(View("NonEoiHolding", model)); } catch (GlobalReservationRuleException) { if (routeModel.UkPrn.HasValue) { return(View("ProviderFundingPaused")); } return(View("EmployerFundingPaused")); } catch (AccountLegalEntityNotFoundException e) { _logger.LogWarning($"Account legal entity not found [{e.AccountLegalEntityPublicHashedId}]."); return(RedirectToRoute(RouteNames.Error404)); } catch (AccountLegalEntityInvalidException ex) { _logger.LogWarning(ex.Message); return(RedirectToRoute(RouteNames.Error500)); } catch (TransferSenderNotAllowedException e) { _logger.LogWarning(e, $"AccountId: {e.AccountId} does not have sender id {e.TransferSenderId} allowed)."); return(RedirectToRoute(RouteNames.Error500)); } catch (Exception e) { _logger.LogError(e, "Error trying to render select reservation."); return(RedirectToRoute(RouteNames.Error500)); } }