Exemplo n.º 1
0
        public void returns_a_formatted_postcode(string postcode, string expected)
        {
            var postcodeFormatter = new PostcodeFormatter();
            var formattedPostcode = postcodeFormatter.FormatPostcode(postcode);

            Assert.Equal(formattedPostcode, expected);
        }
Exemplo n.º 2
0
        public async Task <GetNearbyPostcodesResponse> Handle(GetNearbyPostcodesRequest request, CancellationToken cancellationToken)
        {
            string postcode = PostcodeFormatter.FormatPostcode(request.Postcode);

            // get nearest postcodes
            IReadOnlyList <NearestPostcodeDto> nearestPostcodeDtos = await _nearestPostcodeGetter.GetNearestPostcodesAsync(postcode, request.RadiusInMetres, request.MaxNumberOfResults);

            IEnumerable <string> nearestPostcodes = nearestPostcodeDtos.Select(x => x.Postcode).ToList();

            // get postcodes
            IEnumerable <PostcodeDto> postcodeDtos = await _postcodeAndAddressGetter.GetPostcodesAsync(nearestPostcodes, cancellationToken);

            // create response
            GetNearbyPostcodesResponse getNearbyPostcodesResponse = new GetNearbyPostcodesResponse();

            IEnumerable <GetNearbyPostCodeResponse> getNearbyPostCodeResponses = _mapper.Map <IEnumerable <PostcodeDto>, IEnumerable <GetNearbyPostCodeResponse> >(postcodeDtos);

            getNearbyPostcodesResponse.Postcodes =
                (from getNearbyPostCodeResponse in getNearbyPostCodeResponses
                 join nearestPostcodeDto in nearestPostcodeDtos
                 on getNearbyPostCodeResponse.Postcode equals nearestPostcodeDto.Postcode
                 select new GetNearbyPostCodeResponse
            {
                Postcode = getNearbyPostCodeResponse.Postcode,
                AddressDetails = _addressDetailsSorter.OrderAddressDetailsResponse(getNearbyPostCodeResponse.AddressDetails),
                DistanceInMetres = nearestPostcodeDto.DistanceInMetres,
                FriendlyName = getNearbyPostCodeResponse.FriendlyName
            })
                .OrderBy(x => x.DistanceInMetres)
                .ToList();


            return(getNearbyPostcodesResponse);
        }
        public async Task <IEnumerable <PostcodeDto> > GetPostcodesAsync(IEnumerable <string> postcodes, CancellationToken cancellationToken)
        {
            // format postcodes
            postcodes = postcodes.Select(x => PostcodeFormatter.FormatPostcode(x)).ToList();

            // get postcodes from database
            IEnumerable <PostcodeDto> postcodesFromDb = await _repository.GetPostcodesAsync(postcodes);

            // find missing postcodes
            ImmutableHashSet <string> postcodesFromDbHashSet = postcodesFromDb.Select(x => x.Postcode).ToImmutableHashSet();
            IEnumerable <string>      missingPostcodes       = postcodes.Where(x => !postcodesFromDbHashSet.Contains(x)).ToList();

            if (!missingPostcodes.Any())
            {
                return(postcodesFromDb);
            }

            // filter out postcodes that have no addresses so needless QAS calls aren't made
            missingPostcodes = missingPostcodes.Where(x => !_postcodesWithoutAddressesCache.PostcodesWithoutAddresses.Contains(x));

            IEnumerable <PostcodeDto> missingPostcodeDtos = await _qasAddressGetter.GetPostCodesAndAddressesFromQasAsync(missingPostcodes, cancellationToken);

            // add postcodes that QAS says have no addresses to cache to postcodes without addresses cache
            HashSet <string>     postcodesFromQas = missingPostcodeDtos.Select(x => x.Postcode).ToHashSet();
            IEnumerable <string> postcodesThatHaveNoAddressess = missingPostcodes.Where(x => !postcodesFromQas.Contains(x));

            _postcodesWithoutAddressesCache.AddRange(postcodesThatHaveNoAddressess);

            // add missing postcodes to those originally taken from the DB
            IEnumerable <PostcodeDto> allPostcodeDtos = postcodesFromDb.Concat(missingPostcodeDtos);

            return(allPostcodeDtos);
        }
Exemplo n.º 4
0
        public async Task <GetPostCodeResponse> CheckPostCode(string postcode)
        {
            postcode = PostcodeFormatter.FormatPostcode(postcode);
            var response = await Client.GetAsync($"/api/getpostcode?postcode={postcode}");

            var str = await response.Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <GetPostCodeResponse>(str));
        }
Exemplo n.º 5
0
        public void FormatPostcode(string postcodeToTest, string expected)
        {
            string postcodeInput = new String(postcodeToTest.ToArray());
            string result        = PostcodeFormatter.FormatPostcode(postcodeInput);

            Assert.AreEqual(expected, result);

            // check input was not modified
            Assert.AreEqual(postcodeToTest, postcodeInput);
        }
Exemplo n.º 6
0
        public async Task <GetPostcodeResponse> Handle(GetPostcodeRequest request, CancellationToken cancellationToken)
        {
            request.Postcode = PostcodeFormatter.FormatPostcode(request.Postcode);

            PostcodeDto postcodeDto = await _postcodeAndAddressGetter.GetPostcodeAsync(request.Postcode, cancellationToken);

            GetPostcodeResponse getNearbyGetPostcodesResponse = _mapper.Map <PostcodeDto, GetPostcodeResponse>(postcodeDto);

            getNearbyGetPostcodesResponse.AddressDetails = _addressDetailsSorter.OrderAddressDetailsResponse(getNearbyGetPostcodesResponse.AddressDetails);
            return(getNearbyGetPostcodesResponse);
        }
        public async Task <GetHelpersByPostcodeResponse> Handle(GetHelpersByPostcodeRequest request, CancellationToken cancellationToken)
        {
            request.Postcode = PostcodeFormatter.FormatPostcode(request.Postcode);

            var helpers = await _helperService.GetHelpersWithinRadius(request.Postcode, null, cancellationToken);

            GetHelpersByPostcodeResponse response = new GetHelpersByPostcodeResponse()
            {
                Users = helpers.Select(x => x.User).ToList()
            };

            return(response);
        }
        public async Task <GetChampionsByPostcodeResponse> Handle(GetChampionsByPostcodeRequest request, CancellationToken cancellationToken)
        {
            request.PostCode = PostcodeFormatter.FormatPostcode(request.PostCode);

            IReadOnlyList <HelpMyStreet.Utils.Models.User> result = await _repository.GetChampionsByPostCodeAsync(request.PostCode);

            GetChampionsByPostcodeResponse response = new GetChampionsByPostcodeResponse()
            {
                Users = result
            };

            return(response);
        }
Exemplo n.º 9
0
        public async Task <GetPostCodeCoverageResponse> GetPostcodeCoverage(string postcode)
        {
            postcode = PostcodeFormatter.FormatPostcode(postcode);
            GetPostCodeCoverageResponse response = new GetPostCodeCoverageResponse();

            response.PostCodeResponse = await CheckPostCode(postcode);

            if (response.PostCodeResponse.HasContent && response.PostCodeResponse.IsSuccessful)
            {
                response.VolunteerCount = await _userRepository.GetVolunteerCountByPostcode(postcode);
            }

            return(response);
        }
Exemplo n.º 10
0
        public async Task <List <JobHeader> > AttachedDistanceToJobHeaders(string volunteerPostCode, List <JobHeader> jobHeaders, CancellationToken cancellationToken)
        {
            if (jobHeaders.Count == 0)
            {
                return(null);
            }

            volunteerPostCode = PostcodeFormatter.FormatPostcode(volunteerPostCode);

            foreach (JobHeader jobHeader in jobHeaders)
            {
                jobHeader.PostCode = PostcodeFormatter.FormatPostcode(jobHeader.PostCode);
            }

            List <string> distinctPostCodes = jobHeaders.Select(d => d.PostCode).Distinct().ToList();

            if (!distinctPostCodes.Contains(volunteerPostCode))
            {
                distinctPostCodes.Add(volunteerPostCode);
            }

            var postcodeCoordinatesResponse = await _repository.GetLatitudeAndLongitudes(distinctPostCodes, cancellationToken);

            if (postcodeCoordinatesResponse == null)
            {
                return(null);
            }

            var volunteerPostcodeCoordinates = postcodeCoordinatesResponse.Where(w => w.Postcode == volunteerPostCode).FirstOrDefault();

            if (volunteerPostcodeCoordinates == null)
            {
                return(null);
            }

            foreach (JobHeader jobHeader in jobHeaders)
            {
                var jobPostcodeCoordinates = postcodeCoordinatesResponse.Where(w => w.Postcode == jobHeader.PostCode).FirstOrDefault();
                if (jobPostcodeCoordinates != null)
                {
                    jobHeader.DistanceInMiles = _distanceCalculator.GetDistanceInMiles(volunteerPostcodeCoordinates.Latitude, volunteerPostcodeCoordinates.Longitude, jobPostcodeCoordinates.Latitude, jobPostcodeCoordinates.Longitude);
                }
                else
                {
                    jobHeader.DistanceInMiles = double.MaxValue;
                }
            }
            return(jobHeaders);
        }
        public async Task <GetNearbyPostcodesWithoutAddressesResponse> Handle(GetNearbyPostcodesWithoutAddressesRequest request, CancellationToken cancellationToken)
        {
            request.Postcode = PostcodeFormatter.FormatPostcode(request.Postcode);

            IEnumerable <NearestPostcodeDto> nearestPostcodeDtos = await _nearestPostcodeGetter.GetNearestPostcodesAsync(request.Postcode, request.RadiusInMetres, request.MaxNumberOfResults);

            IEnumerable <NearestPostcodeWithoutAddress> nearestPostcodes = _mapper.Map <IEnumerable <NearestPostcodeDto>, IEnumerable <NearestPostcodeWithoutAddress> >(nearestPostcodeDtos);

            GetNearbyPostcodesWithoutAddressesResponse getNearbyPostcodesWithoutAddressesResponse = new GetNearbyPostcodesWithoutAddressesResponse()
            {
                NearestPostcodes = nearestPostcodes.ToList()
            };

            return(getNearbyPostcodesWithoutAddressesResponse);
        }
Exemplo n.º 12
0
        public async Task <List <PostcodeCoordinate> > GetPostcodeCoordinates(string postcode)
        {
            postcode = PostcodeFormatter.FormatPostcode(postcode);

            GetPostcodeCoordinatesRequest getPostcodeCoordinatesRequest = new GetPostcodeCoordinatesRequest()
            {
                Postcodes = new List <string>()
                {
                    postcode
                }
            };

            var response = await _addressRepository.GetPostcodeCoordinates(getPostcodeCoordinatesRequest);

            return(response.PostcodeCoordinates.ToList());
        }
        /// <summary>
        /// Validates postcode using Regex and aims to produce no false negatives.
        /// </summary>
        /// <param name="postcode">Postcode to validate</param>\
        /// <returns>Whether postcode is valid using regex</returns>
        public bool IsPostcodeValid(string postcode)
        {
            if (String.IsNullOrWhiteSpace(postcode))
            {
                return(false);
            }

            postcode = PostcodeFormatter.FormatPostcode(postcode);

            if (!_postCodeRegex.IsMatch(postcode))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 14
0
        public PostcodeDto MapToPostcodeDto(string postcode, IEnumerable <QasFormatRootResponse> qasFormatRootResponses)
        {
            DateTime timeNow = DateTime.UtcNow;

            postcode = PostcodeFormatter.FormatPostcode(postcode);
            PostcodeDto postcodeDto = new PostcodeDto();

            postcodeDto.Postcode    = postcode;
            postcodeDto.LastUpdated = timeNow;

            foreach (var qasFormatRootResponse in qasFormatRootResponses)
            {
                AddressDetailsDto addressDetailsDto = new AddressDetailsDto();

                foreach (QasFormatAddressReponse address in qasFormatRootResponse.Address) // to deal with strange way results are returned ...
                {
                    if (!String.IsNullOrWhiteSpace(address.AddressLine1))
                    {
                        addressDetailsDto.AddressLine1 = address.AddressLine1;
                    }
                    else if (!String.IsNullOrWhiteSpace(address.AddressLine2))
                    {
                        addressDetailsDto.AddressLine2 = address.AddressLine2;
                    }
                    else if (!String.IsNullOrWhiteSpace(address.AddressLine3))
                    {
                        addressDetailsDto.AddressLine3 = address.AddressLine3;
                    }
                    else if (!String.IsNullOrWhiteSpace(address.Locality))
                    {
                        addressDetailsDto.Locality = address.Locality;
                    }
                    else if (!String.IsNullOrWhiteSpace(address.PostalCode))
                    {
                        addressDetailsDto.Postcode = PostcodeFormatter.FormatPostcode(address.PostalCode);
                    }
                }
                // filter out postcodes that weren't returned or don't have the expected postcode
                if (!String.IsNullOrWhiteSpace(addressDetailsDto.Postcode) && addressDetailsDto.Postcode == postcode)
                {
                    addressDetailsDto.LastUpdated = timeNow;
                    postcodeDto.AddressDetails.Add(addressDetailsDto);
                }
            }

            return(postcodeDto);
        }
Exemplo n.º 15
0
        public async Task <GetVolunteersByPostcodeAndActivityResponse> Handle(GetVolunteersByPostcodeAndActivityRequest request, CancellationToken cancellationToken)
        {
            request.VolunteerFilter.Postcode = PostcodeFormatter.FormatPostcode(request.VolunteerFilter.Postcode);

            var users = await _helperService.GetHelpersWithinRadius(request.VolunteerFilter.Postcode, request.VolunteerFilter.OverrideVolunteerRadius, cancellationToken);

            GetVolunteersByPostcodeAndActivityResponse response = new GetVolunteersByPostcodeAndActivityResponse
            {
                Volunteers = users.Where(x => x.User.SupportActivities.Any(sa => request.VolunteerFilter.Activities.Any(ra => sa == ra)))
                             .Select(x => new VolunteerSummary
                {
                    UserID          = x.User.ID,
                    DistanceInMiles = x.Distance
                }).ToList()
            };

            return(response);
        }
        public async Task <IEnumerable <PostcodeWithNumberOfAddressesDto> > GetNumberOfAddressesPerPostcodeAsync(IEnumerable <string> postcodes, CancellationToken cancellationToken)
        {
            // format postcodes
            postcodes = postcodes.Select(x => PostcodeFormatter.FormatPostcode(x)).ToList();

            // filter out postcodes that have no addresses
            postcodes = postcodes.Where(x => !_postcodesWithoutAddressesCache.PostcodesWithoutAddresses.Contains(x));

            // get postcodes with number of addresses from database
            IEnumerable <PostcodeWithNumberOfAddressesDto> postCodesWithNumberOfAddresses = await _repository.GetNumberOfAddressesPerPostcodeAsync(postcodes);

            // find missing postcodes
            ImmutableHashSet <string> postcodesFromDbHashSet    = postCodesWithNumberOfAddresses.Select(x => x.Postcode).ToImmutableHashSet();
            List <string>             postcodesWithoutAddresses = postcodes.Where(x => !postcodesFromDbHashSet.Contains(x)).ToList();

            if (!postcodesWithoutAddresses.Any())
            {
                return(postCodesWithNumberOfAddresses);
            }

            // get and save addresses for postcodes without addresses
            IEnumerable <PostcodeDto> missingPostcodeDtos = await _qasAddressGetter.GetPostCodesAndAddressesFromQasAsync(postcodesWithoutAddresses, cancellationToken);

            // add postcodes that QAS says have no addresses to cache
            HashSet <string>     postcodesFromQas = missingPostcodeDtos.Select(x => x.Postcode).ToHashSet();
            IEnumerable <string> postcodesThatHaveNoAddressess = postcodesWithoutAddresses.Where(x => !postcodesFromQas.Contains(x));

            _postcodesWithoutAddressesCache.AddRange(postcodesThatHaveNoAddressess);

            IEnumerable <PostcodeWithNumberOfAddressesDto> missingPostCodesWithNumberOfAddresses = missingPostcodeDtos.GroupBy(x => x.Postcode)

                                                                                                   .Select(x => new PostcodeWithNumberOfAddressesDto()
            {
                Postcode          = x.Key,
                NumberOfAddresses = x.Sum(y => y.AddressDetails.Count)
            });

            IEnumerable <PostcodeWithNumberOfAddressesDto> postCodesWithNumberOfAddresses2 = postCodesWithNumberOfAddresses.Where(x => x.NumberOfAddresses >= 0).Concat(missingPostCodesWithNumberOfAddresses);

            return(postCodesWithNumberOfAddresses2);
        }
Exemplo n.º 17
0
        public async Task <QasSearchRootResponse> GetGlobalIntuitiveSearchResponseAsync(string postcode, CancellationToken cancellationToken)
        {
            postcode = postcode.Replace(" ", "");

            string path         = $"capture/address/v2/search";
            string query        = $"query={postcode}&country=GBR&take=100";
            string absolutePath = $"{path}?{query}";

            QasSearchRootResponse qasSearchRootResponse;

            using (HttpResponseMessage response = await _httpClientWrapper.GetAsync(HttpClientConfigName.Qas, absolutePath, cancellationToken).ConfigureAwait(false))
            {
                response.EnsureSuccessStatusCode();
                Stream stream = await response.Content.ReadAsStreamAsync();

                qasSearchRootResponse = stream.ReadAndDeserializeFromJson <QasSearchRootResponse>();
            }
            qasSearchRootResponse.Postcode = PostcodeFormatter.FormatPostcode(postcode);

            return(qasSearchRootResponse);
        }
        /// <summary>
        /// Validates postcode by checking using Regex.  If this says it's valid, it checks the DB to see if the postcode is active.
        /// </summary>
        /// <param name="postcode"></param>
        /// <returns></returns>
        public async Task <bool> IsPostcodeValidAsync(string postcode)
        {
            if (!_regexPostcodeValidator.IsPostcodeValid(postcode))
            {
                return(false);
            }

            postcode = PostcodeFormatter.FormatPostcode(postcode);

            try
            {
                bool isPostcodeInDbAndActive = await _repository.IsPostcodeInDbAndActive(postcode);

                return(isPostcodeInDbAndActive);
            }
            catch (Exception ex)
            {
                _logger.LogWarning("Error calling PostcodeIO to validate postcode. Returning that postcode is valid since it passes Regex", ex);
                return(true);
            }
        }
        public async Task <GetDistanceBetweenPostcodesResponse> Handle(GetDistanceBetweenPostcodesRequest request, CancellationToken cancellationToken)
        {
            request.Postcode1 = PostcodeFormatter.FormatPostcode(request.Postcode1);
            request.Postcode2 = PostcodeFormatter.FormatPostcode(request.Postcode2);

            var postcodeDetails = await _repository.GetPostcodeCoordinatesAsync(new List <string>() { request.Postcode1, request.Postcode2 });

            if (postcodeDetails == null)
            {
                throw new Exception($"Unable to retrieve post code details for {request.Postcode1} or {request.Postcode2}");
            }

            if (postcodeDetails.Count() != 2)
            {
                throw new Exception($"Only expecting 2 row in collection for {request.Postcode1} and {request.Postcode2}. {postcodeDetails.Count()} rows returned");
            }

            var postcodeDetails1 = postcodeDetails.FirstOrDefault(x => x.Postcode == request.Postcode1);
            var postcodeDetails2 = postcodeDetails.FirstOrDefault(x => x.Postcode == request.Postcode2);

            if (postcodeDetails1 == null || postcodeDetails2 == null)
            {
                throw new Exception($"Either postcode details not returned for  {request.Postcode1} and {request.Postcode2}.");
            }

            var distanceInMiles = _distanceCalculator.GetDistanceInMiles(
                postcodeDetails1.Latitude,
                postcodeDetails1.Longitude,
                postcodeDetails2.Latitude,
                postcodeDetails2.Longitude
                );

            return(new GetDistanceBetweenPostcodesResponse()
            {
                DistanceInMiles = distanceInMiles
            });
        }
Exemplo n.º 20
0
 public void ThrowExceptionOnNullInput()
 {
     Assert.Throws <ArgumentNullException>(() => PostcodeFormatter.FormatPostcode(null));
 }
Exemplo n.º 21
0
        private void AddDataRowToDataTable(DataTable dataTable, string input)
        {
            string[] split = input.Split(',');

            bool rowHasEnoughColumns = split.Length > 43;

            if (!rowHasEnoughColumns)
            {
                Console.WriteLine("CS row doesn't have enough columns");
                return;
            }

            string postcode = split[0];

            if (postcode != null)
            {
                postcode = postcode.Replace("\"", "");
                postcode = PostcodeFormatter.FormatPostcode(postcode);
            }

            bool postcodeIsValid = _regexPostcodeValidator.IsPostcodeValid(postcode);

            bool latitudeIsValid = decimal.TryParse(split[42], out decimal latitude);

            if (latitudeIsValid)
            {
                latitudeIsValid = latitude >= -90 && latitude <= 90;
            }

            bool longitudeIsValid = decimal.TryParse(split[43], out decimal longitude);

            if (longitudeIsValid)
            {
                longitudeIsValid = longitude >= -180 && longitude <= 180;
            }


            var introductionDateString = split[3];
            var terminationDateString  = split[4];

            var isPostCodeActive = OnsActivePostcodeDeterminer.IsPostcodeActive(introductionDateString, terminationDateString, DateTime.UtcNow);


            if (!postcodeIsValid)
            {
                _invalidPostcodes.Add(postcode);
                _numberOfInvalidPostcodes++;
            }

            if (!latitudeIsValid)
            {
                _numberOfInvalidLatitudes++;
            }

            if (!longitudeIsValid)
            {
                _numberOfInvalidLongitudes++;
            }

            if (!isPostCodeActive)
            {
                _numberOfTerminatedPostcodes++;
            }

            if (!postcodeIsValid || !latitudeIsValid || !longitudeIsValid)
            {
                _numberOfInvalidRows++;
            }
            else
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow["Postcode"]  = postcode;
                dataRow["Latitude"]  = latitude;
                dataRow["Longitude"] = longitude;
                dataRow["IsActive"]  = isPostCodeActive;

                dataTable.Rows.Add(dataRow);
            }
        }
Exemplo n.º 22
0
        public async Task <GetPostcodesResponse> Handle(GetPostcodesRequest request, CancellationToken cancellationToken)
        {
            GetPostcodesResponse getPostcodesResponse = new GetPostcodesResponse()
            {
                PostcodesResponse = new System.Collections.Generic.Dictionary <string, GetPostcodeResponse>()
            };

            request.PostcodeList.Postcodes = request.PostcodeList.Postcodes.Select(x => PostcodeFormatter.FormatPostcode(x)).ToList();
            System.Collections.Generic.IEnumerable <PostcodeDto> postcodesDto = await _repository.GetPostcodesAsync(request.PostcodeList.Postcodes);

            if (!request.IncludeAddressDetails)
            {
                foreach (PostcodeDto postcodeDto in postcodesDto)
                {
                    postcodeDto.AddressDetails = null;
                }
            }

            foreach (PostcodeDto postcodeDto in postcodesDto)
            {
                getPostcodesResponse.PostcodesResponse.Add(postcodeDto.Postcode, _mapper.Map <PostcodeDto, GetPostcodeResponse>(postcodeDto));
            }

            return(getPostcodesResponse);
        }
Exemplo n.º 23
0
        public void MapToPostcodeDto()
        {
            QasMapper qasMapper = new QasMapper();

            var postCode         = "ng1 5fs";
            var expectedPostcode = PostcodeFormatter.FormatPostcode(postCode);

            IEnumerable <QasFormatRootResponse> qasFormatRootResponses = new List <QasFormatRootResponse>()
            {
                new QasFormatRootResponse()
                {
                    Address = new List <QasFormatAddressReponse>()
                    {
                        new QasFormatAddressReponse()
                        {
                            PostalCode = postCode
                        },
                        new QasFormatAddressReponse()
                        {
                            AddressLine1 = "line1"
                        },
                        new QasFormatAddressReponse()
                        {
                            AddressLine2 = "line2"
                        },
                        new QasFormatAddressReponse()
                        {
                            AddressLine3 = "line3"
                        },
                        new QasFormatAddressReponse()
                        {
                            Locality = "loc"
                        },
                    }
                },
                new QasFormatRootResponse()
                {
                    Address = new List <QasFormatAddressReponse>()
                    {
                        new QasFormatAddressReponse()
                        {
                            PostalCode = "FilterMeOut"
                        },
                        new QasFormatAddressReponse()
                        {
                            AddressLine1 = "line1"
                        },
                        new QasFormatAddressReponse()
                        {
                            AddressLine2 = "line2"
                        },
                        new QasFormatAddressReponse()
                        {
                            AddressLine3 = "line3"
                        },
                        new QasFormatAddressReponse()
                        {
                            Locality = "loc"
                        }
                    }
                },
                new QasFormatRootResponse()
                {
                    Address = new List <QasFormatAddressReponse>()
                    {
                        new QasFormatAddressReponse()
                        {
                            PostalCode = null
                        },
                        new QasFormatAddressReponse()
                        {
                            AddressLine1 = "line1"
                        },
                        new QasFormatAddressReponse()
                        {
                            AddressLine2 = "line2"
                        },
                        new QasFormatAddressReponse()
                        {
                            AddressLine3 = "line3"
                        },
                        new QasFormatAddressReponse()
                        {
                            Locality = "loc"
                        }
                    }
                }
            };

            PostcodeDto result = qasMapper.MapToPostcodeDto(postCode, qasFormatRootResponses);

            Assert.AreEqual(1, result.AddressDetails.Count);
            Assert.AreEqual(expectedPostcode, result.Postcode);
            Assert.AreNotEqual(DateTime.MinValue, result.LastUpdated);

            AddressDetailsDto addressResult = result.AddressDetails.FirstOrDefault();

            Assert.AreEqual(expectedPostcode, addressResult.Postcode);
            Assert.AreEqual("line1", addressResult.AddressLine1);
            Assert.AreEqual("line2", addressResult.AddressLine2);
            Assert.AreEqual("line3", addressResult.AddressLine3);
            Assert.AreEqual("loc", addressResult.Locality);
            Assert.AreNotEqual(DateTime.MinValue, addressResult.LastUpdated);
        }
Exemplo n.º 24
0
        public async Task <IEnumerable <PostcodeDto> > GetPostCodesAndAddressesFromQasAsync(IEnumerable <string> missingPostcodes, CancellationToken cancellationToken)
        {
            // call QAS for missing postcodes and addresses
            List <Task <QasSearchRootResponse> > qasSearchResponseTasks = new List <Task <QasSearchRootResponse> >();
            List <QasSearchRootResponse>         qasSearchResponses     = new List <QasSearchRootResponse>();

            foreach (string missingPostcode in missingPostcodes)
            {
                Task <QasSearchRootResponse> qasResponseTask = _qasService.GetGlobalIntuitiveSearchResponseAsync(PostcodeFormatter.FormatPostcode(missingPostcode), cancellationToken);
                qasSearchResponseTasks.Add(qasResponseTask);
            }

            while (qasSearchResponseTasks.Count > 0)
            {
                Task <QasSearchRootResponse> finishedQasResponseTask = await Task.WhenAny(qasSearchResponseTasks);

                qasSearchResponseTasks.Remove(finishedQasResponseTask);
                QasSearchRootResponse qasSearchResponse = await finishedQasResponseTask;
                qasSearchResponses.Add(qasSearchResponse);
            }

            // call QAS for address details (grouped by postcode to avoid sending 1000s of request at once and to map a single PostcodeDto at a time)
            ILookup <string, string> missingQasFormatIdsGroupedByPostCode = _qasMapper.GetFormatIds(qasSearchResponses);
            List <PostcodeDto>       missingPostcodeDtos = new List <PostcodeDto>();

            foreach (IGrouping <string, string> missingQasFormatIds in missingQasFormatIdsGroupedByPostCode)
            {
                List <Task <QasFormatRootResponse> > qasFormatResponseTasks = new List <Task <QasFormatRootResponse> >();
                foreach (string missingQasFormatId in missingQasFormatIds)
                {
                    Task <QasFormatRootResponse> qasFormatResponseTask = _qasService.GetGlobalIntuitiveFormatResponseAsync(missingQasFormatId, cancellationToken);
                    qasFormatResponseTasks.Add(qasFormatResponseTask);
                }

                List <QasFormatRootResponse> qasFormatResponses = new List <QasFormatRootResponse>();

                while (qasFormatResponseTasks.Count > 0)
                {
                    Task <QasFormatRootResponse> finishedQasFormatResponseTask = await Task.WhenAny(qasFormatResponseTasks);

                    qasFormatResponseTasks.Remove(finishedQasFormatResponseTask);
                    QasFormatRootResponse qasFormatResponse = await finishedQasFormatResponseTask;
                    qasFormatResponses.Add(qasFormatResponse);
                }

                PostcodeDto missingPostcodeDtosForThisBatch = _qasMapper.MapToPostcodeDto(missingQasFormatIds.Key, qasFormatResponses);

                try
                {
                    //new addresses need a friendly name
                    _friendlyNameGenerator.GenerateFriendlyName(missingPostcodeDtosForThisBatch);
                }
                catch (Exception ex)
                {
                    _logger.LogWarning("Error generating friendly name", ex);
                }

                missingPostcodeDtos.Add(missingPostcodeDtosForThisBatch);
            }

            if (missingPostcodeDtos.Any())
            {
                await _repository.SaveAddressesAndFriendlyNameAsync(missingPostcodeDtos);
            }

            return(missingPostcodeDtos);
        }
Exemplo n.º 25
0
 public void SetUp()
 {
     formatter = new PostcodeFormatter();
 }
Exemplo n.º 26
0
 public HackneyPropertyServiceRequestBuilder(NameValueCollection configuration, PostcodeFormatter postcodeFormatter)
 {
     _configuration = configuration;
     _postcodeFormatter = postcodeFormatter;
 }