diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 83e7b6550..c8ab60ed0 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -603,7 +603,8 @@ private end def get_inferred_la(postcode) - PIO.infer_la(postcode) + result = PIO.lookup(postcode) + result[:location_code] if result end def get_has_benefits diff --git a/app/models/location.rb b/app/models/location.rb index 22d3b9607..c21917b70 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -3,7 +3,7 @@ class Location < ApplicationRecord validates :units, :type_of_unit, :mobility_type, presence: true belongs_to :scheme - before_save :infer_la!, if: :postcode_changed? + before_save :lookup_postcode!, if: :postcode_changed? attr_accessor :add_another_location @@ -52,7 +52,11 @@ private end end - def infer_la! - self.location_code = PIO.infer_la(postcode) + def lookup_postcode! + result = PIO.lookup(postcode) + if result + self.location_code = result[:location_code] + self.location_admin_district = result[:location_admin_district] + end end end diff --git a/app/services/postcode_service.rb b/app/services/postcode_service.rb index a82dcc2aa..74c1f4895 100644 --- a/app/services/postcode_service.rb +++ b/app/services/postcode_service.rb @@ -3,7 +3,7 @@ class PostcodeService @pio = Postcodes::IO.new end - def infer_la(postcode) + def lookup(postcode) # Avoid network calls when postcode is invalid return unless postcode.match(POSTCODE_REGEXP) @@ -16,7 +16,10 @@ class PostcodeService Rails.logger.warn("Postcodes.io lookup timed out") end if postcode_lookup && postcode_lookup.info.present? - postcode_lookup.codes["admin_district"] + { + location_code: postcode_lookup.codes["admin_district"], + location_admin_district: postcode_lookup&.admin_district, + } end end diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb index 20b6c7beb..9dad4e9fe 100644 --- a/app/views/locations/index.html.erb +++ b/app/views/locations/index.html.erb @@ -38,6 +38,9 @@ <% row.cell(header: true, text: "Mobility type", html_attributes: { scope: "col", }) %> + <% row.cell(header: true, text: "Local authority", html_attributes: { + scope: "col", + }) %> <% end %> <% end %> <% @locations.each do |location| %> @@ -48,6 +51,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_admin_district) %> <% end %> <% end %> <% end %> diff --git a/app/views/schemes/check_answers.html.erb b/app/views/schemes/check_answers.html.erb index 179a1afca..845b75560 100644 --- a/app/views/schemes/check_answers.html.erb +++ b/app/views/schemes/check_answers.html.erb @@ -105,6 +105,9 @@ <% row.cell(header: true, text: "Mobility type", html_attributes: { scope: "col", }) %> + <% row.cell(header: true, text: "Local authority", html_attributes: { + scope: "col", + }) %> <% end %> <% end %> <% @scheme.locations.each do |location| %> @@ -115,6 +118,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_admin_district) %> <% end %> <% end %> <% end %> diff --git a/db/migrate/20220722083835_add_location_admin_district_to_locations.rb b/db/migrate/20220722083835_add_location_admin_district_to_locations.rb new file mode 100644 index 000000000..05c354099 --- /dev/null +++ b/db/migrate/20220722083835_add_location_admin_district_to_locations.rb @@ -0,0 +1,5 @@ +class AddLocationAdminDistrictToLocations < ActiveRecord::Migration[7.0] + def change + add_column :locations, :location_admin_district, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 978fc7af6..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 diff --git a/spec/features/schemes_helpers.rb b/spec/features/schemes_helpers.rb index 7bf087bab..3814a2f4c 100644 --- a/spec/features/schemes_helpers.rb +++ b/spec/features/schemes_helpers.rb @@ -55,7 +55,7 @@ module SchemesHelpers end def fill_in_and_save_location - fill_in "Postcode", with: "SW1P 4DF" + fill_in "Postcode", with: "AA1 1AA" fill_in "Location name (optional)", with: "Some name" fill_in "Total number of units at this location", with: 1 choose "Self-contained house" diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index 078d0ab36..ae8ed5aba 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -175,7 +175,7 @@ RSpec.describe "Schemes scheme Features" do context "when there are locations that belong to the selected scheme" do let!(:schemes) { FactoryBot.create_list(:scheme, 5) } let(:scheme) { schemes.first } - let!(:locations) { FactoryBot.create_list(:location, 3, scheme:) } + let!(:locations) { FactoryBot.create_list(:location, 3, scheme:, postcode: "AA11AA") } before do visit("schemes") @@ -199,6 +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_admin_district) end end end @@ -468,10 +469,12 @@ RSpec.describe "Schemes scheme Features" do end it "displays information about the first created location" do - expect(page).to have_content "SW1P4DF" + expect(page).to have_content "AA11AA" expect(page).to have_content "Some name" 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 "Westminster" end it "displays information about another location" do diff --git a/spec/request_helper.rb b/spec/request_helper.rb index d2bef6d5c..93dc6f1dc 100644 --- a/spec/request_helper.rb +++ b/spec/request_helper.rb @@ -5,6 +5,10 @@ module RequestHelper WebMock.disable_net_connect!(allow_localhost: true) WebMock.stub_request(:get, /api.postcodes.io/) .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\",\"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: {}) WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/sms/) diff --git a/spec/services/imports/case_logs_import_service_spec.rb b/spec/services/imports/case_logs_import_service_spec.rb index 281a7b600..e763099b9 100644 --- a/spec/services/imports/case_logs_import_service_spec.rb +++ b/spec/services/imports/case_logs_import_service_spec.rb @@ -20,7 +20,7 @@ RSpec.describe Imports::CaseLogsImportService do before do WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/LS166FT/) - .to_return(status: 200, body: '{"status":200,"result":{"codes":{"admin_district":"E08000035"}}}', headers: {}) + .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Westminster","codes":{"admin_district":"E08000035"}}}', headers: {}) allow(Organisation).to receive(:find_by).and_return(nil) allow(Organisation).to receive(:find_by).with(old_visible_id: organisation.old_visible_id.to_i).and_return(organisation)