From 73698884eb3aa9e33db046e35a9fe6e28abcfa5d Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 11 Aug 2023 10:43:06 +0100 Subject: [PATCH] Import location code --- .../imports/scheme_location_import_service.rb | 6 ++++++ .../imports/scheme_location_import_service_spec.rb | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/services/imports/scheme_location_import_service.rb b/app/services/imports/scheme_location_import_service.rb index e01cd1106..76bab80f9 100644 --- a/app/services/imports/scheme_location_import_service.rb +++ b/app/services/imports/scheme_location_import_service.rb @@ -26,6 +26,10 @@ module Imports 4 => "(Registered nursing care home)", }.freeze + LA_NAME_TO_CODE = { + "134" => "E08000036", + }.freeze + def create_scheme(source_scheme, attributes) scheme = Scheme.new( scheme_type: attributes["scheme_type"], @@ -93,6 +97,7 @@ module Imports attributes["location_old_id"] = string_or_nil(xml_doc, "id") attributes["location_old_visible_id"] = string_or_nil(xml_doc, "visible-id") attributes["scheme_old_id"] = string_or_nil(xml_doc, "mgmtgroup") + attributes["location_code"] = LA_NAME_TO_CODE[string_or_nil(xml_doc, "local-authority-name")] attributes end @@ -106,6 +111,7 @@ module Imports old_visible_id: attributes["location_old_visible_id"], old_id: attributes["location_old_id"], startdate: attributes["start_date"], + location_code: attributes["location_code"], scheme:, ) if attributes["end_date"] diff --git a/spec/services/imports/scheme_location_import_service_spec.rb b/spec/services/imports/scheme_location_import_service_spec.rb index 5c45a92aa..ecb09d9c7 100644 --- a/spec/services/imports/scheme_location_import_service_spec.rb +++ b/spec/services/imports/scheme_location_import_service_spec.rb @@ -17,7 +17,7 @@ RSpec.describe Imports::SchemeLocationImportService do end before do - WebMock.stub_request(:get, /api.postcodes.io\/postcodes/) + WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/S446EJ/) .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Westminster","codes":{"admin_district":"E08000035"}}}', headers: {}) end @@ -143,6 +143,7 @@ RSpec.describe Imports::SchemeLocationImportService do location = location_service.create_scheme_location(location_xml) expect(location.name).to eq("Location 1") expect(location.postcode).to eq("S44 6EJ") + expect(location.location_code).to eq("E08000035") expect(location.units).to eq(5) expect(location.mobility_type).to eq("Fitted with equipment and adaptations") expect(location.type_of_unit).to eq("Bungalow") @@ -226,5 +227,15 @@ RSpec.describe Imports::SchemeLocationImportService do expect(location.scheme.support_type).to eq(nil) end end + + context "and postcode does not return a location code" do + before { location_xml.at_xpath("//scheme:postcode").content = "A1 1AA" } + + it "imports location code correctly" do + location = location_service.create_scheme_location(location_xml) + expect(location.postcode).to eq("A1 1AA") + expect(location.location_code).to eq("E08000036") + end + end end end