public async Task SearchApprenticeships_POST_EmptyModel_ReturnsError(CommitmentsDataController sut)
        {
            //Given
            var model = new SearchApprenticeshipsViewModel();

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

            //Then
            result.Should().BeOfType <JsonResult>().Which
            .Value.Should().BeEquivalentTo(new
            {
                ErrorTitle   = "Invalid Search",
                ErrorMessage = "At least one parameter must be populated"
            });
        }
        public async Task <IActionResult> SearchApprenticeships(SearchApprenticeshipsViewModel model)
        {
            if (model.IsModelEmpty)
            {
                return(Json(new { ErrorTitle = "Invalid Search", ErrorMessage = "At least one parameter must be populated" }));
            }

            var result = await _employerCommitmentsService.SearchApprenticeships(
                new SearchApprenticeshipsRequest
            {
                CourseName           = model.CourseName,
                EmployerName         = model.EmployerName,
                ProviderName         = model.ProviderName,
                Ukprn                = model.Ukprn,
                SearchTerm           = model.ApprenticeNameOrUln,
                StartDate            = model.StartDate,
                EndDate              = model.EndDate,
                ApprenticeshipStatus = model.SelectedStatus
            }, new CancellationToken());

            if (result.HasError)
            {
                return(Json(new { ErrorTitle = "Call to Commitments Api Failed", ErrorMessage = result.ErrorMessage }));
            }

            return(Json(result.Apprenticeships.Select(a => new StopApprenticeshipRow
            {
                Id = a.Id,
                FirstName = a.FirstName,
                LastName = a.LastName,
                EmployerName = a.EmployerName,
                ProviderName = a.ProviderName,
                Ukprn = a.Ukprn,
                CourseName = a.CourseName,
                StartDate = a.StartDate,
                EndDate = a.EndDate,
                Uln = a.Uln,
                Status = a.ApprenticeshipStatus.ToString(),
                PaymentStatus = a.PaymentStatus.ToString(),
                CohortReference = a.CohortReference,
            })));
        }
        public IActionResult SearchApprenticeships(string employerName, string courseName, string providerName, string apprenticeNameOrUln, DateTime?startDate, DateTime?endDate, string selectedStatus, long?ukprn, string act)
        {
            var model = new SearchApprenticeshipsViewModel
            {
                EmployerName        = employerName,
                CourseName          = courseName,
                ProviderName        = providerName,
                Ukprn               = ukprn,
                StartDate           = startDate.HasValue && startDate.Value != DateTime.MinValue ? startDate : null,
                EndDate             = endDate.HasValue && endDate.Value != DateTime.MinValue ? endDate : null,
                SelectedStatus      = string.IsNullOrWhiteSpace(selectedStatus) ? "" : selectedStatus,
                ApprenticeNameOrUln = apprenticeNameOrUln
            };

            switch (act)
            {
            case ActionNames.Resume:
                ViewData.Add("FormActionRoute", RouteNames.Approval_ResumeApprenticeship);
                ViewData.Add("FormActionText", "Resume apprenticeship(s)");
                break;

            case ActionNames.Pause:
                ViewData.Add("FormActionRoute", RouteNames.Approval_PauseApprenticeship);
                ViewData.Add("FormActionText", "Pause apprenticeship(s)");
                break;

            case ActionNames.Stop:
                ViewData.Add("FormActionRoute", RouteNames.Approval_StopApprenticeship);
                ViewData.Add("FormActionText", "Stop apprenticeship(s)");
                break;

            default:
                return(BadRequest());
            }

            return(View(model));
        }
        public async Task SearchApprenticeships_POST_ApiCallSucceeds_ReturnsResult([Frozen] Mock <IEmployerCommitmentsService> api, SearchApprenticeshipsViewModel model, SearchApprenticeshipsResult apiResult, CommitmentsDataController sut)
        {
            //Given
            apiResult.ErrorMessage = "";
            api.Setup(s => s.SearchApprenticeships(
                          It.Is <SearchApprenticeshipsRequest>(s => s.CourseName == model.CourseName && s.EmployerName == model.EmployerName &&
                                                               s.ProviderName == model.ProviderName && s.StartDate == model.StartDate && s.EndDate == model.EndDate && s.SearchTerm == model.ApprenticeNameOrUln &&
                                                               s.ApprenticeshipStatus == model.SelectedStatus),
                          It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(apiResult));

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

            //Then
            result.Should().BeOfType <JsonResult>().Which
            .Value.Should().BeEquivalentTo(apiResult.Apprenticeships.Select(s => new
            {
                s.Id,
                s.FirstName,
                s.LastName,
                s.EmployerName,
                s.ProviderName,
                s.CourseName,
                s.StartDate,
                s.EndDate,
                Status        = s.ApprenticeshipStatus.ToString(),
                PaymentStatus = s.PaymentStatus.ToString()
            }));
        }
        public async Task SearchApprenticeships_POST_ApiCallErrors_ReturnsError([Frozen] Mock <IEmployerCommitmentsService> api, SearchApprenticeshipsViewModel model, CommitmentsDataController sut)
        {
            //Given
            var apiResult = new SearchApprenticeshipsResult
            {
                ErrorMessage = "Api Error Message"
            };

            api.Setup(s => s.SearchApprenticeships(
                          It.Is <SearchApprenticeshipsRequest>(s => s.CourseName == model.CourseName && s.EmployerName == model.EmployerName &&
                                                               s.ProviderName == model.ProviderName && s.Ukprn == model.Ukprn && s.StartDate == model.StartDate && s.EndDate == model.EndDate && s.SearchTerm == model.ApprenticeNameOrUln &&
                                                               s.ApprenticeshipStatus == model.SelectedStatus),
                          It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(apiResult));

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

            //Then
            result.Should().BeOfType <JsonResult>().Which
            .Value.Should().BeEquivalentTo(new
            {
                ErrorTitle   = "Call to Commitments Api Failed",
                ErrorMessage = apiResult.ErrorMessage
            });
        }