public async Task <IActionResult> ValidateProviderSearchResultAsync(SaveReferralViewModel viewModel)
        {
            if (viewModel.SelectedProvider.Any(p => p.IsSelected))
            {
                viewModel.SelectedProvider = viewModel.SelectedProvider.Where(p => p.IsSelected).ToArray();

                TempData["SelectedProviders"] = JsonConvert.SerializeObject(viewModel);

                return(RedirectToRoute("SaveReferral"));
            }

            ModelState.AddModelError("SearchResults.Results[0].IsSelected", "You must select at least one provider");

            var searchViewModel = await GetSearchResultsAsync(new SearchParametersViewModel
            {
                Postcode           = viewModel.Postcode,
                SelectedRouteId    = viewModel.SelectedRouteId,
                OpportunityId      = viewModel.OpportunityId,
                OpportunityItemId  = viewModel.OpportunityItemId,
                CompanyNameWithAka = viewModel.CompanyNameWithAka
            });

            return(View("Results", searchViewModel));
        }
        public When_Opportunity_Controller_Save_Referral_Update_Opportunity()
        {
            const int opportunityId     = 1;
            const int opportunityItemId = 2;

            _opportunityService = Substitute.For <IOpportunityService>();
            _opportunityService.IsNewReferralAsync(opportunityItemId).Returns(false);

            var httpContextAccessor = Substitute.For <IHttpContextAccessor>();

            var config = new MapperConfiguration(c =>
            {
                c.AddMaps(typeof(EmployerDtoMapper).Assembly);
                c.ConstructServicesUsing(type =>
                {
                    if (type.FullName != null && type.FullName.Contains("LoggedInUserEmailResolver"))
                    {
                        return(new LoggedInUserEmailResolver <SaveReferralViewModel, OpportunityDto>(httpContextAccessor));
                    }
                    if (type.FullName != null && type.FullName.Contains("LoggedInUserNameResolver") && type.FullName.Contains("SaveReferralViewModel"))
                    {
                        return(new LoggedInUserNameResolver <SaveReferralViewModel, OpportunityDto>(httpContextAccessor));
                    }
                    if (type.FullName != null && type.FullName.Contains("LoggedInUserNameResolver") && type.FullName.Contains("SelectedProviderViewModel"))
                    {
                        return(new LoggedInUserNameResolver <SelectedProviderViewModel, ReferralDto>(httpContextAccessor));
                    }

                    return(null);
                });
            });
            var mapper = new Mapper(config);

            var opportunityController = new OpportunityController(_opportunityService, mapper);
            var controllerWithClaims  = new ClaimsBuilder <OpportunityController>(opportunityController)
                                        .AddUserName(UserName)
                                        .AddEmail(Email)
                                        .Build();

            httpContextAccessor.HttpContext.Returns(controllerWithClaims.HttpContext);

            var viewModel = new SaveReferralViewModel
            {
                SearchResultProviderCount = 2,
                SelectedRouteId           = 1,
                Postcode          = "cv12wt",
                SearchRadius      = 10,
                OpportunityId     = opportunityId,
                OpportunityItemId = opportunityItemId,
                SelectedProvider  = new[]
                {
                    new SelectedProviderViewModel
                    {
                        ProviderVenueId      = 1,
                        DistanceFromEmployer = 1.2m,
                        IsSelected           = false
                    },
                    new SelectedProviderViewModel
                    {
                        ProviderVenueId      = 2,
                        DistanceFromEmployer = 3.4m,
                        IsSelected           = true
                    }
                }
            };

            var serializeObject = JsonConvert.SerializeObject(viewModel);

            var tempDataProvider          = Substitute.For <ITempDataProvider>();
            var tempDataDictionaryFactory = new TempDataDictionaryFactory(tempDataProvider);
            var tempData = tempDataDictionaryFactory.GetTempData(new DefaultHttpContext());

            tempData.Add("SelectedProviders", serializeObject);
            controllerWithClaims.TempData = tempData;

            _result = controllerWithClaims.SaveReferralAsync().GetAwaiter().GetResult();
        }