Skip to content

Commit 2a34c22

Browse files
committed
Allow matching any authority-level
1 parent 5135a39 commit 2a34c22

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

app/models/data_set.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def number_of_places
5252
#
5353
# Returns:
5454
# an array of Place objects
55-
def places_near(location, distance = nil, limit = nil, snac = nil)
55+
def places_near(location, distance = nil, limit = nil, snac = [])
5656
query = places
57-
query = query.where(snac: snac) if snac
57+
query = query.where(:snac.in => snac) if snac.any?
5858
query = query.limit(limit) if limit
5959
query = query.geo_near([location.longitude, location.latitude])
6060
query = query.max_distance(distance.in(:degrees)) if distance
@@ -73,23 +73,23 @@ def places_for_postcode(postcode, distance = nil, limit = nil)
7373
# involve some frontend work though. For now, do this to
7474
# match the existing (not totally correct) behaviour
7575
snac = appropriate_snac_for_postcode(postcode)
76-
return [] unless snac
76+
return [] unless snac.any?
7777

7878
places_near(location, distance, limit, snac)
7979
end
8080

8181
def appropriate_snac_for_postcode(postcode)
8282
local_custodian_codes = GdsApi.locations_api.local_custodian_code_for_postcode(postcode)
83-
return nil if local_custodian_codes.compact.empty?
83+
return [] if local_custodian_codes.compact.empty?
8484

8585
local_authorities_response = GdsApi.local_links_manager.local_authority_by_custodian_code(local_custodian_codes.first)
8686
filtered_authorities = local_authorities_response.to_hash["local_authorities"].select do |la|
87-
[service.local_authority_hierarchy_match_type, "unitary"].include?(la["tier"])
87+
service.local_authority_hierarchy_match_type == Service::LOCAL_AUTHORITY_EITHER_MATCH || [service.local_authority_hierarchy_match_type, "unitary"].include?(la["tier"])
8888
end
8989

90-
filtered_authorities.first&.dig("snac") # there should be 0-1, return nil or first snac
90+
filtered_authorities.map { |a| a["snac"] }
9191
rescue GdsApi::HTTPNotFound
92-
nil
92+
[]
9393
end
9494

9595
def duplicating?

app/models/service.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ class Service
44
LOCATION_MATCH_TYPES = %w[nearest local_authority].freeze
55
LOCAL_AUTHORITY_DISTRICT_MATCH = "district".freeze
66
LOCAL_AUTHORITY_COUNTY_MATCH = "county".freeze
7-
LOCAL_AUTHORITY_HIERARCHY_MATCH_TYPES = [LOCAL_AUTHORITY_DISTRICT_MATCH, LOCAL_AUTHORITY_COUNTY_MATCH].freeze
7+
LOCAL_AUTHORITY_EITHER_MATCH = "either".freeze
8+
LOCAL_AUTHORITY_HIERARCHY_MATCH_TYPES = [LOCAL_AUTHORITY_DISTRICT_MATCH, LOCAL_AUTHORITY_COUNTY_MATCH, LOCAL_AUTHORITY_EITHER_MATCH].freeze
89
MAX_ARCHIVES_TO_STORE = 3
910

1011
field :name, type: String

test/integration/places_api_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,36 @@ class PlacesAPITest < ActionDispatch::IntegrationTest
282282
assert_equal @place3.name, data[1]["name"]
283283
end
284284
end
285+
286+
context "when the service is bounded to either districts or counties" do
287+
setup do
288+
@service.update(local_authority_hierarchy_match_type: Service::LOCAL_AUTHORITY_EITHER_MATCH)
289+
end
290+
291+
should "return all places in order of nearness for postcodes in a county+district council hierarchy" do
292+
stub_locations_api_has_location("EX39 1QS", [{ "latitude" => 51.05318361810428, "longitude" => -4.191071523498792, "local_custodian_code" => 1234 }])
293+
stub_local_links_manager_has_a_district_and_county_local_authority("my-district", "my-county", district_snac: "18UK", county_snac: "18", local_custodian_code: 1234)
294+
295+
get "/places/#{@service.slug}.json?postcode=EX39+1QS"
296+
data = JSON.parse(last_response.body)
297+
assert_equal 4, data.length
298+
assert_equal @place6.name, data[0]["name"]
299+
assert_equal @place2.name, data[1]["name"]
300+
assert_equal @place5.name, data[2]["name"]
301+
assert_equal @place1.name, data[3]["name"]
302+
end
303+
304+
should "return all places in order of nearness for postcodes not in a county+district council hierarchy" do
305+
stub_locations_api_has_location("WC2B 6NH", [{ "latitude" => 51.51695975170424, "longitude" => -0.12058693935709164, "local_custodian_code" => 1234 }])
306+
stub_local_links_manager_has_a_local_authority("county1", country_name: "England", snac: "00AG", local_custodian_code: 1234)
307+
308+
get "/places/#{@service.slug}.json?postcode=WC2B+6NH"
309+
data = JSON.parse(last_response.body)
310+
assert_equal 2, data.length
311+
assert_equal @place4.name, data[0]["name"]
312+
assert_equal @place3.name, data[1]["name"]
313+
end
314+
end
285315
end
286316
end
287317
end

0 commit comments

Comments
 (0)