Beispiel #1
0
        public async Task <IActionResult> PauseApprenticeshipConfirmation(PauseApprenticeshipViewModel model)
        {
            var claims = GetClaims();

            if (!IsValid(model, new string[] { claims.UserId, claims.DisplayName }, out List <PauseApprenticeshipRow> apprenticeshipsData))
            {
                return(View("PauseApprenticeship", model));
            }

            var tasks = new List <Task <PauseApprenticeshipResult> >();

            // is this where unecessary as its captured in the validation?
            foreach (var apprenticeship in apprenticeshipsData.Where(a => a.ApiSubmissionStatus != SubmissionStatus.Successful))
            {
                tasks.Add(_employerCommitmentsService.PauseApprenticeship(new PauseApprenticeshipRequest
                {
                    ApprenticeshipId = apprenticeship.Id,
                    DisplayName      = claims.DisplayName,
                    EmailAddress     = claims.UserEmail,
                    UserId           = claims.UserId
                }, new CancellationToken()));
            }

            var results = await Task.WhenAll(tasks);

            foreach (var apprenticeship in apprenticeshipsData)
            {
                var result = results.Where(s => s.ApprenticeshipId == apprenticeship.Id).FirstOrDefault();
                if (result == null)
                {
                    continue;
                }

                if (!result.HasError)
                {
                    apprenticeship.ApiSubmissionStatus = SubmissionStatus.Successful;
                    apprenticeship.ApiErrorMessage     = string.Empty;
                }
                else
                {
                    apprenticeship.ApiSubmissionStatus = SubmissionStatus.Errored;
                    apprenticeship.ApiErrorMessage     = result.ErrorMessage;
                }
            }

            model.Apprenticeships = apprenticeshipsData;

            return(View("PauseApprenticeship", model));
        }
Beispiel #2
0
 public IActionResult CancelPauseApprenticeship(PauseApprenticeshipViewModel model, string act)
 {
     return(RedirectToAction(RouteNames.Approval_SearchApprenticeships, "SearchApprovals", new
     {
         model.SearchParams.ApprenticeNameOrUln,
         model.SearchParams.CourseName,
         model.SearchParams.ProviderName,
         model.SearchParams.Ukprn,
         model.SearchParams.EmployerName,
         model.SearchParams.SelectedStatus,
         StartDate = model.SearchParams.StartDate.GetUIFormattedDate(),
         EndDate = model.SearchParams.EndDate.GetUIFormattedDate(),
         act = ActionNames.Pause
     }));
 }
Beispiel #3
0
        public static bool TryDeserialise(this PauseApprenticeshipViewModel model, out List <PauseApprenticeshipRow> result, ILogger logger = null)
        {
            result = null;

            try
            {
                result = JsonSerializer.Deserialize <List <PauseApprenticeshipRow> >(model.ApprenticeshipsData, new JsonSerializerOptions {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                });

                return(true);
            }

            catch (Exception e)
            {
                logger?.LogError("Unable to deserialize apprenticeship data", e);

                return(false);
            }
        }
Beispiel #4
0
        public bool IsValid(PauseApprenticeshipViewModel model, IEnumerable <string> claims, out List <PauseApprenticeshipRow> apprenticeshipsData)
        {
            if (!model.TryDeserialise(out apprenticeshipsData, _logger))
            {
                ModelState.AddModelError(string.Empty, "Unable to Read apprenticeship information, please return to the search and try again");
                model.ApprenticeshipsData = null;

                return(false);
            }

            if (claims.Any(c => string.IsNullOrWhiteSpace(c)))
            {
                model.Apprenticeships = apprenticeshipsData;
                ModelState.AddModelError(string.Empty, "Unable to retrieve userId or name from claim for request to Pause Apprenticeship");

                return(false);
            }

            return(true);
        }
        public async Task PauseApprenticeshipConfirmation_POST_DataEnteredCorrectly_SubmitsPauseToApiAndSucceeds([Frozen] Mock <IEmployerCommitmentsService> api, PauseApprovalsController sut, PauseApprenticeshipViewModel model, List <PauseApprenticeshipRow> apprenticeshipData)
        {
            //Given
            //apprenticeshipData.ForEach(s => s.EnteredDate = DateTime.Today.ToString("yyyy-MM-dd"));
            var apprenticeshipIds = apprenticeshipData.Select(s => s.Id);
            var jsonData          = JsonSerializer.Serialize(apprenticeshipData, new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            }).ToString();

            model.ApprenticeshipsData = jsonData;

            api.Setup(s => s.PauseApprenticeship(
                          It.Is <PauseApprenticeshipRequest>(r => apprenticeshipIds.Contains(r.ApprenticeshipId)), It.IsAny <CancellationToken>()))
            .Returns((PauseApprenticeshipRequest request, CancellationToken token) =>
            {
                return(Task.FromResult(new PauseApprenticeshipResult
                {
                    ApprenticeshipId = request.ApprenticeshipId
                }));
            });

            //When
            var result = await sut.PauseApprenticeshipConfirmation(model);

            //Then

            var resultModel = result.Should().BeOfType <ViewResult>().Which
                              .Model.Should().BeOfType <PauseApprenticeshipViewModel>().Which;

            resultModel.Apprenticeships.All(s => s.ApiSubmissionStatus == SubmissionStatus.Successful);
            resultModel.HasError.Should().BeFalse();
        }
        public async Task PauseApprenticeshipConfirmation_POST_IdentityError_ReturnsErrorViewModel(PauseApprovalsController sut, PauseApprenticeshipViewModel model, List <PauseApprenticeshipRow> apprenticeshipData)
        {
            //Given
            var jsonData = JsonSerializer.Serialize(apprenticeshipData, new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            }).ToString();

            model.ApprenticeshipsData         = jsonData;
            sut.ControllerContext.HttpContext = new DefaultHttpContext();

            //When
            var result = await sut.PauseApprenticeshipConfirmation(model);

            //Then
            var resultModel = result.Should().BeOfType <ViewResult>().Which
                              .Model.Should().BeOfType <PauseApprenticeshipViewModel>().Which;

            resultModel.ApprenticeshipsData.Should().BeSameAs(model.ApprenticeshipsData);
            sut.ModelState.IsValid.Should().BeFalse();
            sut.ModelState.Values.First().Errors.First().ErrorMessage.Should().Be("Unable to retrieve userId or name from claim for request to Pause Apprenticeship");
        }
        public async Task PauseApprenticeshipConfirmation_POST_JsonDataError_ReturnsErrorViewModel(PauseApprovalsController sut, PauseApprenticeshipViewModel model)
        {
            //Given
            model.ApprenticeshipsData = "RandomData";

            //When
            var result = await sut.PauseApprenticeshipConfirmation(model);

            //Then
            var resultModel = result.Should().BeOfType <ViewResult>().Which
                              .Model.Should().BeOfType <PauseApprenticeshipViewModel>().Which;

            resultModel.ApprenticeshipsData.Should().BeSameAs(null);
            sut.ModelState.IsValid.Should().BeFalse();
            sut.ModelState.Values.First().Errors.First().ErrorMessage.Should().Be("Unable to Read apprenticeship information, please return to the search and try again");
        }
        public void CancelPauseApprenticeship_POST_RedirectsToSearch(PauseApprovalsController sut, PauseApprenticeshipViewModel model)
        {
            //Given

            //When
            var result = sut.CancelPauseApprenticeship(model, PauseAction);

            //Then
            var action = result.Should().BeOfType <RedirectToActionResult>().Which;

            action.ActionName.Should().Be(RouteNames.Approval_SearchApprenticeships);
            action.RouteValues.Values.Should().BeEquivalentTo(new object []
            {
                model.SearchParams.ApprenticeNameOrUln,
                model.SearchParams.CourseName,
                model.SearchParams.ProviderName,
                model.SearchParams.Ukprn,
                model.SearchParams.EmployerName,
                model.SearchParams.SelectedStatus,
                model.SearchParams.EndDate.Value.ToString("yyyy-MM-dd"),
                model.SearchParams.StartDate.Value.ToString("yyyy-MM-dd"),
                PauseAction
            });
        }