diff --git a/app/models/location.rb b/app/models/location.rb
index 4812e2cd5..5edf0caa9 100644
--- a/app/models/location.rb
+++ b/app/models/location.rb
@@ -50,5 +50,6 @@ private
def infer_la!
self.location_code = PIO.infer_la(postcode)
+ self.location_admin_district = PIO.infer_admin_district(postcode)
end
end
diff --git a/app/services/postcode_service.rb b/app/services/postcode_service.rb
index a82dcc2aa..546443ef0 100644
--- a/app/services/postcode_service.rb
+++ b/app/services/postcode_service.rb
@@ -20,6 +20,23 @@ class PostcodeService
end
end
+ def infer_admin_district(postcode)
+ # Avoid network calls when postcode is invalid
+ return unless postcode.match(POSTCODE_REGEXP)
+
+ postcode_lookup = nil
+ begin
+ # URI encoding only supports ASCII characters
+ ascii_postcode = self.class.clean(postcode)
+ Timeout.timeout(5) { postcode_lookup = @pio.lookup(ascii_postcode) }
+ rescue Timeout::Error
+ Rails.logger.warn("Postcodes.io lookup timed out")
+ end
+ if postcode_lookup && postcode_lookup.info.present?
+ postcode_lookup.admin_district
+ end
+ end
+
def self.clean(postcode)
postcode.encode("ASCII", "UTF-8", invalid: :replace, undef: :replace, replace: "").delete(" ").upcase
end
diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb
index d8b203cf7..b5bbef300 100644
--- a/app/views/locations/index.html.erb
+++ b/app/views/locations/index.html.erb
@@ -46,7 +46,7 @@
<% row.cell(text: location.units) %>
<% row.cell(text: simple_format("#{location.type_of_unit}")) %>
<% row.cell(text: location.mobility_type) %>
- <% row.cell(text: location.location_code) %>
+ <% row.cell(text: location.location_admin_district) %>
<% end %>
<% end %>
<% end %>
diff --git a/app/views/schemes/check_answers.html.erb b/app/views/schemes/check_answers.html.erb
index b9e79d34c..9080a7716 100644
--- a/app/views/schemes/check_answers.html.erb
+++ b/app/views/schemes/check_answers.html.erb
@@ -106,7 +106,7 @@
<% row.cell(text: location.units) %>
<% row.cell(text: simple_format("#{location.type_of_unit}")) %>
<% row.cell(text: location.mobility_type) %>
- <% row.cell(text: location.location_code) %>
+ <% row.cell(text: location.location_admin_district) %>
<% end %>
<% end %>
<% end %>
diff --git a/db/schema.rb b/db/schema.rb
index c05d38920..39bc35e94 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.0].define(version: 2022_07_20_111635) do
+ActiveRecord::Schema[7.0].define(version: 2022_07_22_083835) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -250,6 +250,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_20_111635) do
t.integer "old_visible_id"
t.string "mobility_type"
t.datetime "startdate", precision: nil
+ t.string "location_admin_district"
t.index ["old_id"], name: "index_locations_on_old_id", unique: true
t.index ["scheme_id"], name: "index_locations_on_scheme_id"
end
@@ -320,6 +321,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_20_111635) do
t.string "old_id"
t.integer "old_visible_id"
t.integer "total_units"
+ t.boolean "confirmed"
t.index ["managing_organisation_id"], name: "index_schemes_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id"
end
diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb
index 0d6d9f27b..14c6090aa 100644
--- a/spec/features/schemes_spec.rb
+++ b/spec/features/schemes_spec.rb
@@ -199,7 +199,7 @@ RSpec.describe "Schemes scheme Features" do
expect(page).to have_content(location.units)
expect(page).to have_content(location.type_of_unit)
expect(page).to have_content(location.mobility_type)
- expect(page).to have_content(location.location_code)
+ expect(page).to have_content(location.location_admin_district)
end
end
end
@@ -389,7 +389,7 @@ RSpec.describe "Schemes scheme Features" do
expect(page).to have_content "Self-contained house"
expect(page).to have_content "None"
expect(page).to have_content "Local authority"
- expect(page).to have_content "E09000033"
+ expect(page).to have_content "Westminster"
end
it "displays information about another location" do
diff --git a/spec/request_helper.rb b/spec/request_helper.rb
index 4985de582..93dc6f1dc 100644
--- a/spec/request_helper.rb
+++ b/spec/request_helper.rb
@@ -7,7 +7,7 @@ module RequestHelper
.to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {})
WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/AA11AA")
- .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 1AA\",\"nuts\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {})
+ .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 1AA\",\"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {})
WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/email/)
.to_return(status: 200, body: "", headers: {})