public bool IsValid(StopApprenticeshipViewModel model, IEnumerable <string> claims, out List <StopApprenticeshipRow> 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 stop apprenticeship");

                return(false);
            }

            if (apprenticeshipsData.Any(s => s.GetStopDate == null && s.ApiSubmissionStatus != SubmissionStatus.Successful))
            {
                model.Apprenticeships = apprenticeshipsData;
                ModelState.AddModelError(string.Empty, "Not all Apprenticeship rows have been supplied with a stop date.");

                return(false);
            }

            return(true);
        }
        public async Task <IActionResult> StopApprenticeshipConfirmation(StopApprenticeshipViewModel model)
        {
            var claims = GetClaims();

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

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

            foreach (var apprenticeship in apprenticeshipsData.Where(a => a.ApiSubmissionStatus != SubmissionStatus.Successful))
            {
                tasks.Add(_employerCommitmentsService.StopApprenticeship(new Core.Models.StopApprenticeshipRequest
                {
                    AccountId        = apprenticeship.AccountId,
                    ApprenticeshipId = apprenticeship.Id,
                    StopDate         = apprenticeship.GetStopDate.Value,
                    MadeRedundant    = false,
                    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("StopApprenticeship", model));
        }
 public IActionResult CancelStopApprenticeship(StopApprenticeshipViewModel 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.Stop
     }));
 }
Example #4
0
        public static bool TryDeserialise(this StopApprenticeshipViewModel model, out List <StopApprenticeshipRow> result, ILogger logger = null)
        {
            result = null;

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

                return(true);
            }

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

                return(false);
            }
        }
        public async Task <IActionResult> StopApprenticeship(ApprenticeshipSearchResultsViewModel model)
        {
            var ids = model.SelectedIds?.Split(',');

            if (ids == null || ids.Count() == 0)
            {
                return(RedirectToAction(RouteNames.Approval_SearchApprenticeships, "SearchApprovals", CreateSearchModel(model, ActionNames.Stop)));
            }

            var results = await Task.WhenAll(GetApprenticeshipsFromApprovals(ids));

            if (results.Any(a => a.HasError))
            {
                var stopModelError = new StopApprenticeshipViewModel
                {
                    HasError = true
                };
                return(View(stopModelError));
            }

            // Reconstruct Search Params for return to search page.
            return(View(new StopApprenticeshipViewModel
            {
                Apprenticeships = _mapper.Map <List <StopApprenticeshipRow> >(results.Select(s => s.Apprenticeship)),
                SearchParams = new SearchParameters
                {
                    ApprenticeNameOrUln = model.ApprenticeNameOrUln,
                    CourseName = model.CourseName,
                    EmployerName = model.EmployerName,
                    ProviderName = model.ProviderName,
                    Ukprn = model.Ukprn,
                    SelectedStatus = model.Status,
                    StartDate = model.StartDate,
                    EndDate = model.EndDate
                }
            }));
        }
Example #6
0
        public async Task StopApprenticeshipConfirmation_POST_DataEnteredCorrectly_SubmitsStopToApiAndSucceeds([Frozen] Mock <IEmployerCommitmentsService> api, StopApprovalsController sut, StopApprenticeshipViewModel model, List <StopApprenticeshipRow> 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.StopApprenticeship(
                          It.Is <StopApprenticeshipRequest>(r => apprenticeshipIds.Contains(r.ApprenticeshipId)), It.IsAny <CancellationToken>()))
            .Returns((StopApprenticeshipRequest request, CancellationToken token) =>
            {
                return(Task.FromResult(new StopApprenticeshipResult
                {
                    ApprenticeshipId = request.ApprenticeshipId
                }));
            });

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

            //Then

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

            resultModel.Apprenticeships.All(s => s.ApiSubmissionStatus == SubmissionStatus.Successful);
            resultModel.HasError.Should().BeFalse();
        }
Example #7
0
        public async Task StopApprenticeshipConfirmation_POST_NotAllStopDatesEntered_ReturnsErrorViewModel(StopApprovalsController sut, StopApprenticeshipViewModel model, List <StopApprenticeshipRow> apprenticeshipData)
        {
            //Given
            var jsonData = JsonSerializer.Serialize(apprenticeshipData, new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            }).ToString();

            model.ApprenticeshipsData = jsonData;

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

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

            resultModel.ApprenticeshipsData.Should().BeSameAs(model.ApprenticeshipsData);
            sut.ModelState.IsValid.Should().BeFalse();
            sut.ModelState.Values.First().Errors.First().ErrorMessage.Should().Be("Not all Apprenticeship rows have been supplied with a stop date.");
        }
Example #8
0
        public async Task StopApprenticeshipConfirmation_POST_IdentityError_ReturnsErrorViewModel(StopApprovalsController sut, StopApprenticeshipViewModel model, List <StopApprenticeshipRow> 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.StopApprenticeshipConfirmation(model);

            //Then
            var resultModel = result.Should().BeOfType <ViewResult>().Which
                              .Model.Should().BeOfType <StopApprenticeshipViewModel>().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 stop apprenticeship");
        }
Example #9
0
        public async Task StopApprenticeshipConfirmation_POST_JsonDataError_ReturnsErrorViewModel(StopApprovalsController sut, StopApprenticeshipViewModel model)
        {
            //Given
            model.ApprenticeshipsData = "RandomData";

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

            //Then
            var resultModel = result.Should().BeOfType <ViewResult>().Which
                              .Model.Should().BeOfType <StopApprenticeshipViewModel>().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");
        }
Example #10
0
        public void CancelStopApprenticeship_POST_RedirectsToSearch(StopApprovalsController sut, StopApprenticeshipViewModel model)
        {
            //Given

            //When
            var result = sut.CancelStopApprenticeship(model, StopAction);

            //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"),
                StopAction
            });
        }