public ProviderProximityReportDtoBuilder()
 {
     _dto = new ProviderProximityReportDto
     {
         Postcode   = "CV1 2WT",
         SkillAreas = new List <string>(),
         Providers  = new List <ProviderProximityReportItemDto>()
     };
 }
        public When_SqlSearchProvider_Search_Providers_For_Report_Is_Called_With_No_Selected_Routes()
        {
            var logger = Substitute.For <ILogger <Data.SearchProviders.SqlSearchProvider> >();

            _dbContext = new TestConfiguration().GetDbContext();

            _providerVenue = new ValidProviderVenueSearchBuilder().BuildOneVenue();
            _dbContext.Add(_providerVenue);
            _dbContext.SaveChanges();

            var provider = new Data.SearchProviders.SqlSearchProvider(logger, _dbContext);

            _results = provider.SearchProvidersByPostcodeProximityForReportAsync(
                new ProviderProximitySearchParametersDto
            {
                Postcode       = "CV1 2WT",
                SearchRadius   = 5,
                SelectedRoutes = null,
                Latitude       = "52.400997",
                Longitude      = "-1.508122"
            }).GetAwaiter().GetResult();
        }
Beispiel #3
0
        public async Task <ProviderProximityReportDto> SearchProvidersByPostcodeProximityForReportAsync(ProviderProximitySearchParametersDto searchParameters)
        {
            _logger.LogInformation($"Searching for providers within radius {searchParameters.SearchRadius} of postcode '{searchParameters.Postcode}'");

            var employerLocation = GetSearchStartPoint(searchParameters.Latitude, searchParameters.Longitude);

            var searchRadiusInMeters = searchParameters.SearchRadius * MilesToMeters;

            var resultTemp = await(from provider in _matchingDbContext.Provider
                                   join providerVenue in _matchingDbContext.ProviderVenue on provider.Id equals providerVenue.ProviderId
                                   join providerQualification in _matchingDbContext.ProviderQualification on providerVenue.Id equals providerQualification.ProviderVenueId
                                   join routePathMapping in _matchingDbContext.QualificationRouteMapping on providerQualification.QualificationId equals routePathMapping.QualificationId
                                   join qualification in _matchingDbContext.Qualification on routePathMapping.QualificationId equals qualification.Id
                                   join route in _matchingDbContext.Route on routePathMapping.RouteId equals route.Id
                                   orderby providerVenue.Location.Distance(employerLocation)
                                   where providerVenue.Location.Distance(employerLocation) <= searchRadiusInMeters &&
                                   provider.IsCdfProvider &&
                                   provider.IsEnabledForReferral &&
                                   providerVenue.IsEnabledForReferral &&
                                   !providerVenue.IsRemoved
                                   select new
            {
                ProviderVenueId     = providerVenue.Id,
                ProviderName        = provider.Name,
                ProviderDisplayName = provider.DisplayName,
                provider.PrimaryContact,
                provider.PrimaryContactEmail,
                provider.PrimaryContactPhone,
                provider.SecondaryContact,
                provider.SecondaryContactEmail,
                provider.SecondaryContactPhone,
                ProviderVenueName     = providerVenue.Name,
                Distance              = providerVenue.Location.Distance(employerLocation) / MilesToMeters,
                ProviderVenuePostcode = providerVenue.Postcode,
                ProviderVenueTown     = providerVenue.Town,
                Latitude              = providerVenue.Latitude ?? 0,
                Longitude             = providerVenue.Longitude ?? 0,
                provider.IsTLevelProvider,
                RouteName = route.Name,
                RouteId   = route.Id,
                QualificationShortTitle = qualification.ShortTitle
            }).Distinct().ToListAsync();

            var result = resultTemp.GroupBy(g => new
            {
                g.Distance,
                g.IsTLevelProvider,
                g.Latitude,
                g.Longitude,
                g.ProviderDisplayName,
                g.PrimaryContact,
                g.PrimaryContactEmail,
                g.PrimaryContactPhone,
                g.SecondaryContact,
                g.SecondaryContactEmail,
                g.SecondaryContactPhone,
                g.ProviderVenueId,
                g.ProviderVenueName,
                g.ProviderVenuePostcode,
                g.ProviderVenueTown
            })
                         .Select(grp => new ProviderProximityReportItemDto
            {
                Distance              = grp.Key.Distance,
                ProviderVenueTown     = grp.Key.ProviderVenueTown,
                ProviderVenuePostcode = grp.Key.ProviderVenuePostcode,
                ProviderDisplayName   = grp.Key.ProviderDisplayName,
                ProviderVenueName     = grp.Key.ProviderVenueName,
                PrimaryContact        = grp.Key.PrimaryContact,
                PrimaryContactEmail   = grp.Key.PrimaryContactEmail,
                PrimaryContactPhone   = grp.Key.PrimaryContactPhone,
                SecondaryContact      = grp.Key.SecondaryContact,
                SecondaryContactEmail = grp.Key.SecondaryContactEmail,
                SecondaryContactPhone = grp.Key.SecondaryContactPhone,
                Routes = grp.GroupBy(rt => new { rt.RouteName, rt.RouteId })
                         .Select(rt => new RouteAndQualificationsDto
                {
                    RouteName = rt.Key.RouteName,
                    RouteId   = rt.Key.RouteId,
                    QualificationShortTitles = rt.Select(q =>
                                                         q.QualificationShortTitle)
                                               .OrderBy(q => q)
                }).OrderBy(rt => rt.RouteName)
            }).OrderBy(r => r.Distance)
                         .ThenBy(r =>
            {
                if (r.ProviderVenueName == r.ProviderVenuePostcode)
                {
                    return(r.ProviderDisplayName);
                }
                else
                {
                    return(r.ProviderVenueName);
                }
            })
                         .ToList();

            var dto = new ProviderProximityReportDto
            {
                Providers = result,
                Postcode  = searchParameters.Postcode,
                //TODO: Get selected route names
                //SkillAreas = searchParameters.SelectedRoutes
            };

            return(dto);
        }