Browse Source

CLDC-3482 Correct routing to property LA (#2449)

* Correct routing to property LA

* Add rake task to update LAs
pull/2464/head
kosiakkatrina 7 months ago committed by GitHub
parent
commit
7d871edf74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      app/models/form/lettings/pages/property_local_authority.rb
  2. 3
      app/models/form/sales/pages/property_local_authority.rb
  3. 22
      app/models/lettings_log.rb
  4. 2
      app/models/sales_log.rb
  5. 14
      lib/tasks/reinfer_local_authority.rake
  6. 88
      spec/lib/tasks/reinfer_local_authority_spec.rb
  7. 76
      spec/models/form/lettings/pages/property_local_authority_spec.rb
  8. 54
      spec/models/form/sales/pages/property_local_authority_spec.rb

3
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

3
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

22
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?

2
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

14
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

88
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

76
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

54
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

Loading…
Cancel
Save