public When_Proximity_BackFillEmployerPostTown_Function_Fires()
        {
            _opportunityItemRepository = Substitute.For <IRepository <OpportunityItem> >();
            _opportunityItemRepository.GetManyAsync(Arg.Any <Expression <Func <OpportunityItem, bool> > >())
            .Returns(new List <OpportunityItem> {
                new OpportunityItem
                {
                    Postcode = "CV1 2WT",
                    Town     = null
                }
            }.AsQueryable());

            var providerVenueRepository = Substitute.For <IRepository <ProviderVenue> >();

            _functionLogRepository = Substitute.For <IRepository <FunctionLog> >();

            _googleMapsApiClient = Substitute.For <IGoogleMapApiClient>();
            _googleMapsApiClient.GetAddressDetailsAsync("CV1 2WT").Returns("Coventry");

            _locationApiClient = Substitute.For <ILocationApiClient>();
            _locationApiClient.IsValidPostcodeAsync(Arg.Any <string>(), Arg.Any <bool>()).Returns((true, "CV1 2WT"));

            var proximityFunctions = new Functions.Proximity(
                _locationApiClient,
                _googleMapsApiClient,
                _opportunityItemRepository,
                providerVenueRepository,
                _functionLogRepository);

            proximityFunctions.BackFillEmployerPostTownAsync(
                new TimerInfo(new ConstantSchedule(TimeSpan.Zero), null),
                new ExecutionContext(),
                new NullLogger <Functions.Proximity>()
                ).GetAwaiter().GetResult();
        }
Пример #2
0
        public string Resolve(ProviderVenueDetailViewModel source, ProviderVenue destination, string destMember, ResolutionContext context)
        {
            var name = source.Name;

            var(includeTerminated, postcode) = _locationApiClient.IsValidPostcodeAsync(name, true).GetAwaiter().GetResult();

            return(includeTerminated ? postcode : name.ToTitleCase());
        }
        public When_ProviderVenueService_Is_Called_To_Update_Venue()
        {
            var httpContextAccessor = Substitute.For <IHttpContextAccessor>();

            httpContextAccessor.HttpContext.Returns(new DefaultHttpContext
            {
                User = new ClaimsPrincipal(new ClaimsIdentity(new[]
                {
                    new Claim(ClaimTypes.GivenName, "TestUser")
                }))
            });

            var dateTimeProvider = Substitute.For <IDateTimeProvider>();

            dateTimeProvider.UtcNow().Returns(new DateTime(2019, 1, 1));

            _locationApiClient = Substitute.For <ILocationApiClient>();
            _locationApiClient.IsValidPostcodeAsync(Arg.Any <string>(), Arg.Any <bool>())
            .Returns(callinfo => callinfo.Arg <string>() == "CV1 2WT"
                    ? (true, "CV1 2WT")
                    : (false, null));

            var config = new MapperConfiguration(c =>
            {
                c.AddMaps(typeof(ProviderVenueMapper).Assembly);
                c.ConstructServicesUsing(type =>
                                         type.Name.Contains("LoggedInUserEmailResolver") ?
                                         new LoggedInUserEmailResolver <ProviderVenueDetailViewModel, Domain.Models.ProviderVenue>(httpContextAccessor) :
                                         type.Name.Contains("LoggedInUserNameResolver") ?
                                         (object)new LoggedInUserNameResolver <ProviderVenueDetailViewModel, Domain.Models.ProviderVenue>(httpContextAccessor) :
                                         type.Name.Contains("UtcNowResolver") ?
                                         new UtcNowResolver <ProviderVenueDetailViewModel, Domain.Models.ProviderVenue>(dateTimeProvider) :
                                         type.Name.Contains("VenueNameResolver") ?
                                         (object)new VenueNameResolver(_locationApiClient) :
                                         null);
            });
            var mapper = new Mapper(config);

            _providerVenueRepository = Substitute.For <IProviderVenueRepository>();
            _providerVenueRepository.GetSingleOrDefaultAsync(Arg.Any <Expression <Func <Domain.Models.ProviderVenue, bool> > >())
            .Returns(new ValidProviderVenueBuilder().Build());

            var googleMapApiClient = Substitute.For <IGoogleMapApiClient>();
            var locationService    = Substitute.For <ILocationApiClient>();

            var providerVenueService = new ProviderVenueService(mapper, _providerVenueRepository, locationService, googleMapApiClient);

            var viewModel = new ProviderVenueDetailViewModel
            {
                Id                   = 1,
                Postcode             = "CV1 2WT",
                Name                 = "Test provider venue",
                IsEnabledForReferral = true
            };

            providerVenueService.UpdateVenueAsync(viewModel).GetAwaiter().GetResult();
        }
        public async Task <(bool, string)> IsValidPostcodeAsync(string postcode)
        {
            var(valid, postcodeResult) = await _locationApiClient.IsValidPostcodeAsync(postcode);

            if (!valid)
            {
                (valid, postcodeResult) = await _locationApiClient.IsTerminatedPostcodeAsync(postcode);
            }

            return(valid, postcodeResult);
        }
 public async Task <(bool, string)> IsValidPostcodeAsync(string postcode)
 {
     return(await _locationApiClient.IsValidPostcodeAsync(postcode));
 }
Пример #6
0
        public async Task BackFillEmployerPostTownAsync(
#pragma warning disable IDE0060 // Remove unused parameter
            [TimerTrigger("0 0 0 1 1 *", RunOnStartup = true)] TimerInfo timer,
#pragma warning restore IDE0060 // Remove unused parameter
            ExecutionContext context,
            ILogger logger
            )
        {
            try
            {
                var stopwatch = Stopwatch.StartNew();

                logger.LogInformation($"Function {context.FunctionName} triggered");

                var opportunityItems = new List <OpportunityItem>();

                foreach (var opportunityItem in _opportunityItemRepository.GetManyAsync(io => io.Town == null ||
                                                                                        io.Town == "" ||
                                                                                        io.Town == " "))
                {
                    var(isValidPostcode, postcode) = await _locationApiClient.IsValidPostcodeAsync(opportunityItem.Postcode, true);

                    if (!isValidPostcode)
                    {
                        var errorMessage = "Error Back Filling Employer Post Town Data. Invalid Postcode";

                        logger.LogError(errorMessage);

                        await _functionLogRepository.CreateAsync(new FunctionLog
                        {
                            ErrorMessage = errorMessage,
                            FunctionName = context.FunctionName,
                            RowNumber    = opportunityItem.Id
                        });
                    }
                    var googleAddressDetail = await _googleMapApiClient.GetAddressDetailsAsync(postcode);

                    opportunityItem.Town     = googleAddressDetail;
                    opportunityItem.Postcode = postcode;

                    opportunityItems.Add(opportunityItem);
                }

                await _opportunityItemRepository.UpdateManyAsync(opportunityItems);

                stopwatch.Stop();

                logger.LogInformation($"Function {context.FunctionName} finished processing\n" +
                                      $"\tRows saved: {opportunityItems.Count}\n" +
                                      $"\tTime taken: {stopwatch.ElapsedMilliseconds: #,###}ms");
            }
            catch (Exception e)
            {
                var errorMessage = $"Error Back Filling Employer Post Town Data. Internal Error Message {e}";

                logger.LogError(errorMessage);

                await _functionLogRepository.CreateAsync(new FunctionLog
                {
                    ErrorMessage = errorMessage,
                    FunctionName = context.FunctionName,
                    RowNumber    = -1
                });

                throw;
            }
        }