From 90461ae959a5b72acf16392694d2f781dcfdffe7 Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 8 Jan 2025 08:58:09 +0000 Subject: [PATCH] Add LA changes for 2025 --- app/models/log.rb | 36 +++--- .../2025_local_authorities.csv | 5 + db/seeds.rb | 8 +- spec/models/sales_log_spec.rb | 120 +++++++++++++++++- 4 files changed, 145 insertions(+), 24 deletions(-) create mode 100644 config/local_authorities_data/2025_local_authorities.csv diff --git a/app/models/log.rb b/app/models/log.rb index 94f0ff58d..66e996892 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -343,31 +343,27 @@ private PIO = PostcodeService.new LA_CHANGES = { - "E07000027" => "E06000064", # Barrow-in-Furness => Westmorland and Furness - "E07000030" => "E06000064", # Eden => Westmorland and Furness - "E07000031" => "E06000064", # South Lakeland => Westmorland and Furness - "E07000026" => "E06000063", # Allerdale => Cumberland - "E07000028" => "E06000063", # Carlisle => Cumberland - "E07000029" => "E06000063", # Copeland => Cumberland - "E07000163" => "E06000065", # Craven => North Yorkshire - "E07000164" => "E06000065", # Hambleton => North Yorkshire - "E07000165" => "E06000065", # Harrogate => North Yorkshire - "E07000166" => "E06000065", # Richmondshire => North Yorkshire - "E07000167" => "E06000065", # Ryedale => North Yorkshire - "E07000168" => "E06000065", # Scarborough => North Yorkshire - "E07000169" => "E06000065", # Selby => North Yorkshire - "E07000187" => "E06000066", # Mendip => Somerset - "E07000188" => "E06000066", # Sedgemoor => Somerset - "E07000246" => "E06000066", # Somerset West and Taunton => Somerset - "E07000189" => "E06000066", # South Somerset => Somerset + 2025 => { + "E08000016" => "E08000038", # Barnsley + "E08000019" => "E08000039", # Sheffield + }, + }.freeze + + BACKWARDS_LA_CHANGES = { + 2024 => { + "E08000038" => "E08000016", # Barnsley + "E08000039" => "E08000019", # Sheffield + }, }.freeze def get_inferred_la(postcode) result = PIO.lookup(postcode) location_code = result[:location_code] if result - if LA_CHANGES.key?(location_code) && form.start_date.year >= 2023 - LA_CHANGES[location_code] - elsif !(LA_CHANGES.value?(location_code) && form.start_date.year < 2023) + if LA_CHANGES[form.start_date.year]&.key?(location_code) + LA_CHANGES[form.start_date.year][location_code] + elsif BACKWARDS_LA_CHANGES[form.start_date.year]&.key?(location_code) + BACKWARDS_LA_CHANGES[form.start_date.year][location_code] + elsif !LA_CHANGES.value?(location_code) location_code end end diff --git a/config/local_authorities_data/2025_local_authorities.csv b/config/local_authorities_data/2025_local_authorities.csv new file mode 100644 index 000000000..ac5f2e319 --- /dev/null +++ b/config/local_authorities_data/2025_local_authorities.csv @@ -0,0 +1,5 @@ +code,name,start_year,end_year +E08000016,Barnsley,2021,2025 +E08000019,Sheffield,2021,2025 +E08000038,Barnsley,2025, +E08000039,Sheffield,2025, diff --git a/db/seeds.rb b/db/seeds.rb index f3dcb688d..e0f3e6672 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -175,7 +175,9 @@ unless Rails.env.test? end if LocalAuthority.count.zero? - path = "config/local_authorities_data/initial_local_authorities.csv" - service = Imports::LocalAuthoritiesService.new(path:) - service.call + paths = ["config/local_authorities_data/initial_local_authorities.csv", "config/local_authorities_data/2025_local_authorities.csv"] + paths.each do |path| + service = Imports::LocalAuthoritiesService.new(path:) + service.call + end end diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 20eb0b2a6..0f557090c 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -579,7 +579,7 @@ RSpec.describe SalesLog, type: :model do before do WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/CA101AA/) - .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Eden","codes":{"admin_district":"E07000030"}}}', headers: {}) + .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Eden","codes":{"admin_district":"E06000064"}}}', headers: {}) end it "sets previous postcode for discounted sale" do @@ -618,6 +618,124 @@ RSpec.describe SalesLog, type: :model do end end + context "when saving address with LAs that have changed E-codes (LA inferred from postcode)" do + context "when LA is inferred from postcode" do + let(:address_sales_log_24_25) do + create(:sales_log, :shared_ownership_setup_complete, uprn_known: 0, uprn: nil, postcode_full: "CA10 1AA", saledate: Time.zone.local(2024, 5, 2)) + end + + let(:address_sales_log_25_26) do + create(:sales_log, :shared_ownership_setup_complete, postcode_full: "CA10 1AA", saledate: Time.zone.local(2025, 5, 2)) + end + + before do + Timecop.freeze(Time.zone.local(2025, 5, 10)) + Singleton.__init__(FormHandler) + end + + after do + Timecop.return + end + + context "when old(2024) E-code gets returned" do + before do + WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/CA101AA/) + .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Barnsley","codes":{"admin_district":"E08000016"}}}', headers: {}) + end + + context "with 2024 log" do + it "keeps 2024 E-code" do + expect(address_sales_log_24_25.la).to eq("E08000016") + end + end + + context "with 2025 log" do + it "uses new 2025 E-code if" do + expect(address_sales_log_25_26.la).to eq("E08000038") + end + end + end + + context "when new(2025) E-code gets returned" do + before do + WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/CA101AA/) + .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Barnsley","codes":{"admin_district":"E08000038"}}}', headers: {}) + end + + context "with 2024 log" do + it "uses 2024 E-code" do + expect(address_sales_log_24_25.la).to eq("E08000016") + end + end + + context "with 2025 log" do + it "keeps 2025 E-code if new(2025) E-code gets returned" do + expect(address_sales_log_25_26.la).to eq("E08000038") + end + end + end + end + end + + context "when saving address with LAs that have changed E-codes" do + context "when address inferred from uprn - we still get LA from postcode" do + let(:address_sales_log_24_25) do + create(:sales_log, :shared_ownership_setup_complete, uprn_known: 1, uprn: 1, saledate: Time.zone.local(2024, 5, 2)) + end + + let(:address_sales_log_25_26) do + create(:sales_log, :shared_ownership_setup_complete, uprn_known: 1, uprn: 1, saledate: Time.zone.local(2025, 5, 2)) + end + + before do + Timecop.freeze(Time.zone.local(2025, 5, 10)) + Singleton.__init__(FormHandler) + end + + after do + Timecop.return + end + + context "when old(2024) E-code gets returned" do + before do + WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/AA11AA/) + .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Barnsley","codes":{"admin_district":"E08000016"}}}', headers: {}) + end + + context "with 2024 log" do + it "keeps 2024 E-code" do + expect(address_sales_log_24_25.la).to eq("E08000016") + end + end + + context "with 2025 log" do + it "uses new 2025 E-code if" do + expect(address_sales_log_25_26.la).to eq("E08000038") + end + end + end + + context "when new(2025) E-code gets returned" do + before do + WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/AA11AA/) + .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Barnsley","codes":{"admin_district":"E08000038"}}}', headers: {}) + end + + context "with 2024 log" do + it "uses 2024 E-code" do # currently returns nil + expect(address_sales_log_24_25.la).to eq("E08000016") + end + end + + context "with 2025 log" do + it "keeps 2025 E-code if new(2025) E-code gets returned" do + expect(address_sales_log_25_26.la).to eq("E08000038") + end + end + end + end + end + it "errors if the property postcode is emptied" do expect { address_sales_log.update!({ postcode_full: "" }) } .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/)