public async void IndexPost_GivenInvalidModelState_PreviousQuarterSelectionShouldBeRetrieved()
        {
            var model = new SelectYourPcsViewModel()
            {
                ReturnId       = this.fixture.Create <Guid>(),
                OrganisationId = this.fixture.Create <Guid>()
            };

            var previousQuarterReturnResult = this.fixture.Build <PreviousQuarterReturnResult>()
                                              .With(p => p.PreviousQuarter, new Quarter(2019, QuarterType.Q1)).Create();

            A.CallTo(
                () => weeeClient.SendAsync(
                    A <string> ._,
                    A <GetPreviousQuarterSchemes> .That.Matches(p => p.ReturnId == model.ReturnId && p.OrganisationId == model.OrganisationId)))
            .Returns(previousQuarterReturnResult);

            this.controller.ModelState.AddModelError("error", "error");

            var result = await controller.Index(model, true) as ViewResult;

            var returnedModel = result.Model as SelectYourPcsViewModel;

            returnedModel.PreviousQuarterData.Should().Be(previousQuarterReturnResult);
        }
        public virtual async Task <ActionResult> Index(SelectYourPcsViewModel viewModel, bool reselect = false)
        {
            await this.SetBreadcrumb(viewModel.OrganisationId, BreadCrumbConstant.AatfReturn, DisplayHelper.YearQuarterPeriodFormat(this.TempData["currentQuarter"] as Quarter, this.TempData["currentQuarterWindow"] as QuarterWindow));

            if (this.ModelState.IsValid)
            {
                if (reselect)
                {
                    return(await this.Reselect(viewModel));
                }

                using (var client = this.apiClient())
                {
                    var requests = this.requestCreator.ViewModelToRequest(viewModel);
                    await client.SendAsync(this.User.GetAccessToken(), requests);
                }

                return(AatfRedirect.TaskList(viewModel.ReturnId));
            }

            using (var client = this.apiClient())
            {
                viewModel.PreviousQuarterData = await client.SendAsync(
                    this.User.GetAccessToken(),
                    new GetPreviousQuarterSchemes(viewModel.OrganisationId, viewModel.ReturnId));
            }

            return(this.View(viewModel));
        }
        public void SelectYourPCSViewModel_GivenSelectedSchemesListIsNull_ArgumentNullExceptionExpected()
        {
            Action action = () =>
            {
                var selectYourPCSModel = new SelectYourPcsViewModel(A.Fake <List <SchemeData> >(), null);
            };

            action.Should().Throw <ArgumentNullException>();
        }
        public void SelectYourPCSViewModel_GivenSchemeListIsNull_ArgumentNullExceptionExpected()
        {
            Action action = () =>
            {
                var selectYourPCSModel = new SelectYourPcsViewModel(null, A.Fake <List <Guid> >());
            };

            action.Should().Throw <ArgumentNullException>();
        }
        private bool HaveSchemesBeenRemoved(SelectYourPcsViewModel model, List <SchemeData> alreadySelected)
        {
            foreach (var scheme in alreadySelected)
            {
                if (model.SelectedSchemes.Contains(scheme.Id) == false)
                {
                    return(true);
                }
            }

            return(false);
        }
        public void ViewModelToRequested_GivenValidViewModel_RequestShouldNotBeNull()
        {
            var schemeList         = A.Fake <List <SchemeData> >();
            var selectedSchemeList = new List <Guid>();

            selectedSchemeList.Add(Guid.NewGuid());

            var viewModel = new SelectYourPcsViewModel(schemeList, selectedSchemeList);

            var request = requestCreator.ViewModelToRequest(viewModel);

            request.Should().NotBeNull();
        }
        public async void IndexPost_GivenSchemesRemoved_ShouldRedirectToRemovedPcsRoute()
        {
            var existingSchemes   = A.CollectionOfDummy <SchemeData>(3).ToList();
            var reselectedSchemes = new List <Guid>();

            foreach (var scheme in existingSchemes)
            {
                scheme.Id = Guid.NewGuid();
                reselectedSchemes.Add(scheme.Id);
            }

            var usersAlreadySavedSchemeDataList = new SchemeDataList()
            {
                SchemeDataItems = existingSchemes
            };

            var removedItem = reselectedSchemes.ElementAt(reselectedSchemes.Count - 1);

            reselectedSchemes.RemoveAt(reselectedSchemes.Count - 1);

            var model = new SelectYourPcsViewModel()
            {
                ReturnId        = Guid.NewGuid(),
                OrganisationId  = Guid.NewGuid(),
                SchemeList      = existingSchemes,
                SelectedSchemes = reselectedSchemes
            };

            model.SelectedSchemes = reselectedSchemes;

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <GetReturnScheme> .That.Matches(p => p.ReturnId == model.ReturnId))).Returns(usersAlreadySavedSchemeDataList);

            var result = await controller.Index(model, true) as RedirectToRouteResult;

            result.RouteName.Should().Be(AatfRedirect.RemovedPcsRouteName);
            result.RouteValues["returnId"].Should().Be(model.ReturnId);
            result.RouteValues["organisationId"].Should().Be(model.OrganisationId);

            var removedSchemeList = controller.TempData["RemovedSchemeList"] as List <SchemeData>;

            removedSchemeList.Count.Should().Be(1);
            removedSchemeList.ElementAt(0).Id.Should().Be(removedItem);

            controller.TempData["SelectedSchemes"].Should().Be(model.SelectedSchemes);

            var removedSchemes = controller.TempData["RemovedSchemes"] as List <Guid>;

            removedSchemes.Count().Should().Be(1);
            removedSchemes.ElementAt(0).Should().Be(removedItem);
        }
        public async void IndexPost_GivenModel_RedirectShouldBeCorrect()
        {
            var returnId = Guid.NewGuid();

            var viewModel = new SelectYourPcsViewModel(A.Fake <List <SchemeData> >(), A.Fake <List <Guid> >())
            {
                ReturnId = returnId
            };

            var redirect = await controller.Index(viewModel) as RedirectToRouteResult;

            redirect.RouteValues["action"].Should().Be("Index");
            redirect.RouteValues["controller"].Should().Be("AatfTaskList");
            redirect.RouteValues["returnId"].Should().Be(returnId);
        }
        public void ViewModelToRequested_GivenValidViewModel_AddReturnSchemeRequestShouldBeMapped()
        {
            var schemeList         = A.Fake <List <SchemeData> >();
            var selectedSchemeList = new List <Guid>();
            var selectedSchemeId   = Guid.NewGuid();
            var returnId           = Guid.NewGuid();

            selectedSchemeList.Add(selectedSchemeId);

            var viewModel = new SelectYourPcsViewModel(schemeList, selectedSchemeList);

            viewModel.ReturnId = returnId;

            var requests = requestCreator.ViewModelToRequest(viewModel);

            requests.ReturnId.Should().Be(returnId);
            requests.SchemeIds.Count.Should().Be(1);
            requests.SchemeIds.First().Should().Be(selectedSchemeId);
        }
        public virtual async Task <ActionResult> Index(Guid organisationId, Guid returnId, bool reselect = false, bool copyPrevious = false, bool clearSelections = false)
        {
            using (var client = this.apiClient())
            {
                var viewModel = new SelectYourPcsViewModel
                {
                    OrganisationId      = organisationId,
                    ReturnId            = returnId,
                    SchemeList          = await client.SendAsync(this.User.GetAccessToken(), new GetSchemesExternal()),
                    PreviousQuarterData = await client.SendAsync(this.User.GetAccessToken(), new GetPreviousQuarterSchemes(organisationId, returnId))
                };

                if (reselect)
                {
                    var request  = new GetReturnScheme(returnId);
                    var existing = await client.SendAsync(this.User.GetAccessToken(), request);

                    viewModel.SelectedSchemes = existing.SchemeDataItems.Select(p => p.Id).ToList();
                    viewModel.Reselect        = true;
                }

                if (copyPrevious)
                {
                    viewModel.SelectedSchemes = viewModel.PreviousQuarterData.PreviousSchemes;
                    viewModel.CopyPrevious    = true;
                }

                if (clearSelections)
                {
                    viewModel.SelectedSchemes = new List <Guid>();
                }

                var @return = await client.SendAsync(this.User.GetAccessToken(), new GetReturn(returnId, false));

                await this.SetBreadcrumb(organisationId, BreadCrumbConstant.AatfReturn, DisplayHelper.YearQuarterPeriodFormat(@return.Quarter, @return.QuarterWindow));

                this.TempData["currentQuarter"]       = @return.Quarter;
                this.TempData["currentQuarterWindow"] = @return.QuarterWindow;

                return(this.View("Index", viewModel));
            }
        }
        private async Task <ActionResult> Reselect(SelectYourPcsViewModel viewModel)
        {
            using (var client = this.apiClient())
            {
                var existing = await client.SendAsync(this.User.GetAccessToken(), new GetReturnScheme(viewModel.ReturnId));

                if (this.HaveSchemesBeenRemoved(viewModel, existing.SchemeDataItems.ToList()))
                {
                    this.TempData[RemovedSchemeList] = viewModel.SchemeList
                                                       .Where(q => !viewModel.SelectedSchemes.Contains(q.Id) && existing.SchemeDataItems.Any(e => e.Id == q.Id)).ToList();
                    this.TempData[SelectedSchemes] = viewModel.SelectedSchemes;
                    this.TempData[RemovedSchemes]  = viewModel.SchemeList.Select(p => p.Id)
                                                     .Where(q => !viewModel.SelectedSchemes.Contains(q) && existing.SchemeDataItems.Any(e => e.Id == q)).ToList();

                    return(RedirectToRoute(AatfRedirect.RemovedPcsRouteName, new { returnId = viewModel.ReturnId, organisationId = viewModel.OrganisationId }));
                }

                return(await this.SaveAndContinue(existing, viewModel.SelectedSchemes, viewModel.ReturnId));
            }
        }
        public async void IndexPost_NoSchemeRemoved_RedirectToTaskListAndAddSchemeRequestSentForEachScheme()
        {
            var returnId = Guid.NewGuid();

            var reselectedSchemes = PrepareSaveSchemes(returnId);

            var model = new SelectYourPcsViewModel()
            {
                ReturnId        = returnId,
                SelectedSchemes = reselectedSchemes
            };

            var result = await controller.Index(model, true) as RedirectToRouteResult;

            result.RouteValues["returnId"].Should().Be(returnId);
            result.RouteValues["action"].Should().Be("Index");
            result.RouteValues["controller"].Should().Be("AatfTaskList");
            result.RouteName.Should().Be(AatfRedirect.Default);

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <AddReturnScheme> .That.Matches(p => p.ReturnId == returnId && p.SchemeIds == reselectedSchemes))).MustHaveHappened(Repeated.Exactly.Once);
        }
 public SelectYourPCSViewModelTests()
 {
     model = new SelectYourPcsViewModel();
 }