From 7d871edf74551366663c40cb74d27ccc9d204bf9 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 11 Jun 2024 13:55:33 +0100 Subject: [PATCH] CLDC-3482 Correct routing to property LA (#2449) * Correct routing to property LA * Add rake task to update LAs --- .../pages/property_local_authority.rb | 3 +- .../sales/pages/property_local_authority.rb | 3 +- app/models/lettings_log.rb | 22 ++--- app/models/sales_log.rb | 2 +- lib/tasks/reinfer_local_authority.rake | 14 +++ .../lib/tasks/reinfer_local_authority_spec.rb | 88 +++++++++++++++++++ .../pages/property_local_authority_spec.rb | 76 ++++++++++++++-- .../pages/property_local_authority_spec.rb | 54 +++++++++++- 8 files changed, 239 insertions(+), 23 deletions(-) create mode 100644 lib/tasks/reinfer_local_authority.rake create mode 100644 spec/lib/tasks/reinfer_local_authority_spec.rb diff --git a/app/models/form/lettings/pages/property_local_authority.rb b/app/models/form/lettings/pages/property_local_authority.rb index 9b36b0936..1e3d34bd2 100644 --- a/app/models/form/lettings/pages/property_local_authority.rb +++ b/app/models/form/lettings/pages/property_local_authority.rb @@ -3,7 +3,8 @@ class Form::Lettings::Pages::PropertyLocalAuthority < ::Form::Page super @id = "property_local_authority" @depends_on = [ - { "is_la_inferred" => false, "is_general_needs?" => true, "address_search_given?" => true }, + { "is_la_inferred" => false, "is_general_needs?" => true, "form.start_year_after_2024?" => false }, + { "is_la_inferred" => false, "is_general_needs?" => true, "form.start_year_after_2024?" => true, "address_search_given?" => true }, ] end diff --git a/app/models/form/sales/pages/property_local_authority.rb b/app/models/form/sales/pages/property_local_authority.rb index 59403a20c..e0289839d 100644 --- a/app/models/form/sales/pages/property_local_authority.rb +++ b/app/models/form/sales/pages/property_local_authority.rb @@ -3,7 +3,8 @@ class Form::Sales::Pages::PropertyLocalAuthority < ::Form::Page super @id = "property_local_authority" @depends_on = [ - { "is_la_inferred" => false, "address_search_given?" => true }, + { "is_la_inferred" => false, "form.start_year_after_2024?" => false }, + { "is_la_inferred" => false, "form.start_year_after_2024?" => true, "address_search_given?" => true }, ] end diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 7ff728a6b..cc67a6ae2 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -693,7 +693,17 @@ class LettingsLog < Log end def address_search_given? - address_line1_input.present? && postcode_full.present? + address_line1_input.present? && postcode_full_input.present? + end + + def process_postcode_changes! + self.postcode_full = upcase_and_remove_whitespace(postcode_full) + return if postcode_full.blank? + + self.postcode_known = 1 + inferred_la = get_inferred_la(postcode_full) + self.is_la_inferred = inferred_la.present? + self.la = inferred_la if inferred_la.present? end private @@ -752,16 +762,6 @@ private collection_start_year >= 2022 && !is_fixed_term_tenancy? end - def process_postcode_changes! - self.postcode_full = upcase_and_remove_whitespace(postcode_full) - return if postcode_full.blank? - - self.postcode_known = 1 - inferred_la = get_inferred_la(postcode_full) - self.is_la_inferred = inferred_la.present? - self.la = inferred_la if inferred_la.present? - end - def process_previous_postcode_changes! self.ppostcode_full = upcase_and_remove_whitespace(ppostcode_full) return if ppostcode_full.blank? diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index e43220917..6ac2979c8 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -540,6 +540,6 @@ class SalesLog < Log end def address_search_given? - address_line1_input.present? && postcode_full.present? + address_line1_input.present? && postcode_full_input.present? end end diff --git a/lib/tasks/reinfer_local_authority.rake b/lib/tasks/reinfer_local_authority.rake new file mode 100644 index 000000000..a17d7eed6 --- /dev/null +++ b/lib/tasks/reinfer_local_authority.rake @@ -0,0 +1,14 @@ +desc "Reinfers LA from postcode where it's missing" +task reinfer_local_authority: :environment do + LettingsLog.filter_by_year(2023).where(needstype: 1, la: nil).where.not(postcode_full: nil).find_each do |log| + log.process_postcode_changes! + + Rails.logger.info "Invalid lettings log: #{log.id}" unless log.save + end + + SalesLog.filter_by_year(2023).where(la: nil).where.not(postcode_full: nil).find_each do |log| + log.process_postcode_changes! + + Rails.logger.info "Invalid sales log: #{log.id}" unless log.save + end +end diff --git a/spec/lib/tasks/reinfer_local_authority_spec.rb b/spec/lib/tasks/reinfer_local_authority_spec.rb new file mode 100644 index 000000000..c92d227dc --- /dev/null +++ b/spec/lib/tasks/reinfer_local_authority_spec.rb @@ -0,0 +1,88 @@ +require "rails_helper" +require "rake" + +RSpec.describe "reinfer_local_authority" do + describe ":reinfer_local_authority", type: :task do + subject(:task) { Rake::Task["reinfer_local_authority"] } + + before do + Rake.application.rake_require("tasks/reinfer_local_authority") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + context "and there is a general needs type lettings log with postcode and without LA" do + let(:log) { create(:lettings_log, :completed, postcode_full: "AA1 1AA", status: "completed", startdate: Time.zone.local(2023, 4, 1)) } + + it "updates the la if it can be inferred" do + log.la = nil + log.save!(validate: false) + task.invoke + + log.reload + expect(log.la).to eq("E09000033") + expect(log.status).to eq("completed") + end + + it "does not update the la if it cannot be inferred and sets status to in_progress" do + log.la = nil + log.postcode_full = "B11AB" + log.save!(validate: false) + task.invoke + + log.reload + expect(log.la).to be_nil + expect(log.status).to eq("in_progress") + end + end + + context "and the lettings log has a validation error" do + let(:log) { build(:lettings_log, :completed, postcode_full: "some fake postcode", la: nil, status: "completed", startdate: Time.zone.local(2023, 4, 1)) } + + it "logs invalid log ID" do + log.save!(validate: false) + expect(Rails.logger).to receive(:info).with("Invalid lettings log: #{log.id}") + + task.invoke + end + end + + context "and there is a sales log with postcode and without LA" do + let(:log) { create(:sales_log, :completed, postcode_full: "AA1 1AA", status: "completed", saledate: Time.zone.local(2023, 4, 1)) } + + it "updates the la if it can be inferred" do + log.la = nil + log.save!(validate: false) + task.invoke + + log.reload + expect(log.la).to eq("E09000033") + expect(log.status).to eq("completed") + end + + it "does not update the la if it cannot be inferred and sets status to in_progress" do + log.la = nil + log.postcode_full = "B11AB" + log.save!(validate: false) + task.invoke + + log.reload + expect(log.la).to be_nil + expect(log.status).to eq("in_progress") + end + end + + context "and the sales log has a validation error" do + let(:log) { build(:sales_log, :completed, postcode_full: "some fake postcode", la: nil, status: "completed", saledate: Time.zone.local(2023, 4, 1)) } + + it "logs invalid log ID" do + log.save!(validate: false) + expect(Rails.logger).to receive(:info).with("Invalid sales log: #{log.id}") + + task.invoke + end + end + end + end +end diff --git a/spec/models/form/lettings/pages/property_local_authority_spec.rb b/spec/models/form/lettings/pages/property_local_authority_spec.rb index 1936587b0..b3f73e505 100644 --- a/spec/models/form/lettings/pages/property_local_authority_spec.rb +++ b/spec/models/form/lettings/pages/property_local_authority_spec.rb @@ -5,8 +5,12 @@ RSpec.describe Form::Lettings::Pages::PropertyLocalAuthority, type: :model do let(:page_id) { nil } let(:page_definition) { nil } - let(:subsection) { instance_double(Form::Subsection, form: instance_double(Form, start_date:)) } - let(:start_date) { Time.utc(2022, 4, 1) } + let(:form) { FormHandler.instance.forms["current_lettings"] } + let(:subsection) { instance_double(Form::Subsection, form:, enabled?: true) } + + before do + allow(form).to receive(:start_date).and_return(Time.utc(2022, 4, 1)) + end it "has correct subsection" do expect(page.subsection).to eq(subsection) @@ -32,9 +36,69 @@ RSpec.describe Form::Lettings::Pages::PropertyLocalAuthority, type: :model do expect(page.description).to be_nil end - it "has the correct depends_on" do - expect(page.depends_on).to match([{ "address_search_given?" => true, - "is_general_needs?" => true, - "is_la_inferred" => false }]) + context "when routing to the page" do + let(:log) { build(:lettings_log) } + + context "with form before 2024" do + before do + allow(form).to receive(:start_year_after_2024?).and_return(false) + end + + it "is routed to when la is not inferred and it is general needs log" do + log.needstype = 1 + log.is_la_inferred = false + expect(page).to be_routed_to(log, nil) + end + + it "is not routed to when la is inferred" do + log.needstype = 1 + log.is_la_inferred = true + expect(page).not_to be_routed_to(log, nil) + end + + it "is not routed to when it's a supported housing log" do + log.needstype = 2 + log.is_la_inferred = false + expect(page).not_to be_routed_to(log, nil) + end + end + + context "with form after 2024" do + before do + allow(form).to receive(:start_year_after_2024?).and_return(true) + end + + it "is routed to when la is not inferred, it is general needs log and address search has been given" do + log.needstype = 1 + log.is_la_inferred = false + log.address_line1_input = "1" + log.postcode_full_input = "A11AA" + expect(page).to be_routed_to(log, nil) + end + + it "is not routed to when la is inferred" do + log.needstype = 1 + log.is_la_inferred = true + log.address_line1_input = "1" + log.postcode_full_input = "A11AA" + expect(page).not_to be_routed_to(log, nil) + end + + it "is not routed to when it's a supported housing log" do + log.needstype = 2 + log.is_la_inferred = false + log.address_line1_input = "1" + log.postcode_full_input = "A11AA" + expect(page).not_to be_routed_to(log, nil) + end + + it "is not routed to when address search is not given" do + log.needstype = 1 + log.is_la_inferred = false + log.address_line1_input = nil + log.postcode_full_input = "A11AA" + expect(page).not_to be_routed_to(log, nil) + end + end end end diff --git a/spec/models/form/sales/pages/property_local_authority_spec.rb b/spec/models/form/sales/pages/property_local_authority_spec.rb index 49c39f8e9..6d94beed0 100644 --- a/spec/models/form/sales/pages/property_local_authority_spec.rb +++ b/spec/models/form/sales/pages/property_local_authority_spec.rb @@ -5,9 +5,14 @@ RSpec.describe Form::Sales::Pages::PropertyLocalAuthority, type: :model do let(:page_id) { nil } let(:page_definition) { nil } - let(:subsection) { instance_double(Form::Subsection, form: instance_double(Form, start_date:)) } + let(:form) { FormHandler.instance.forms["current_sales"] } + let(:subsection) { instance_double(Form::Subsection, form:, enabled?: true) } let(:start_date) { Time.utc(2022, 4, 1) } + before do + allow(form).to receive(:start_date).and_return(start_date) + end + it "has correct subsection" do expect(page.subsection).to eq(subsection) end @@ -51,7 +56,50 @@ RSpec.describe Form::Sales::Pages::PropertyLocalAuthority, type: :model do expect(page.description).to be_nil end - it "has the correct depends_on" do - expect(page.depends_on).to eq([{ "address_search_given?" => true, "is_la_inferred" => false }]) + context "when routing to the page" do + let(:log) { build(:sales_log) } + + context "with form before 2024" do + before do + allow(form).to receive(:start_year_after_2024?).and_return(false) + end + + it "is routed to when la is not inferred" do + log.is_la_inferred = false + expect(page).to be_routed_to(log, nil) + end + + it "is not routed to when la is inferred" do + log.is_la_inferred = true + expect(page).not_to be_routed_to(log, nil) + end + end + + context "with form after 2024" do + before do + allow(form).to receive(:start_year_after_2024?).and_return(true) + end + + it "is routed to when la is not inferred and address search has been given" do + log.is_la_inferred = false + log.address_line1_input = "1" + log.postcode_full_input = "A11AA" + expect(page).to be_routed_to(log, nil) + end + + it "is not routed to when la is inferred" do + log.is_la_inferred = true + log.address_line1_input = "1" + log.postcode_full_input = "A11AA" + expect(page).not_to be_routed_to(log, nil) + end + + it "is not routed to when address search is not given" do + log.is_la_inferred = false + log.address_line1_input = nil + log.postcode_full_input = "A11AA" + expect(page).not_to be_routed_to(log, nil) + end + end end end