From 720a3ec1400c421a5468f42ff1f6f8c8a01272e3 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Wed, 19 Apr 2023 09:18:59 +0100 Subject: [PATCH] handle derivation of values around buyers living in the property - derive values where appropriate - clear these values when the derived state no longer holds - update the routing for pages holding questions about whether particular buyers will live in the property to reflect when they are derived - test the deriving and associated clearing of values - update tests on page routing and sales log factory --- .../derived_variables/sales_log_variables.rb | 11 ++++ .../sales/pages/buyer1_live_in_property.rb | 12 ++++ .../sales/pages/buyer2_live_in_property.rb | 14 ++++- app/models/sales_log.rb | 48 ++++++++++++++-- .../pages/buyer1_live_in_property_spec.rb | 21 ++++++- .../pages/buyer2_live_in_property_spec.rb | 14 ++++- spec/models/sales_log_spec.rb | 55 +++++++++++++++++++ .../sales/household_validations_spec.rb | 2 +- 8 files changed, 167 insertions(+), 10 deletions(-) diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index b4ff1a671..11905e480 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -30,6 +30,17 @@ module DerivedVariables::SalesLogVariables self.uprn = nil self.uprn_known = 0 end + + if buyers_will_not_live_in? + self.buy1livein = 2 + if joint_purchase? + self.buy2livein = 2 + end + end + + if buyers_will_live_in? && not_joint_purchase? + self.buy1livein = 1 + end end private diff --git a/app/models/form/sales/pages/buyer1_live_in_property.rb b/app/models/form/sales/pages/buyer1_live_in_property.rb index 17877ee7b..8ee63d26c 100644 --- a/app/models/form/sales/pages/buyer1_live_in_property.rb +++ b/app/models/form/sales/pages/buyer1_live_in_property.rb @@ -5,9 +5,21 @@ class Form::Sales::Pages::Buyer1LiveInProperty < ::Form::Page @depends_on = [ { "buyer_has_seen_privacy_notice?" => true, + "outright_sale?" => false, }, { "buyer_not_interviewed?" => true, + "outright_sale?" => false, + }, + { + "buyer_has_seen_privacy_notice?" => true, + "joint_purchase?" => true, + "buyers_will_live_in?" => true, + }, + { + "buyer_not_interviewed?" => true, + "joint_purchase?" => true, + "buyers_will_live_in?" => true, }, ] end diff --git a/app/models/form/sales/pages/buyer2_live_in_property.rb b/app/models/form/sales/pages/buyer2_live_in_property.rb index 193f434f8..a16638c0d 100644 --- a/app/models/form/sales/pages/buyer2_live_in_property.rb +++ b/app/models/form/sales/pages/buyer2_live_in_property.rb @@ -4,12 +4,24 @@ class Form::Sales::Pages::Buyer2LiveInProperty < ::Form::Page @id = "buyer_2_live_in_property" @depends_on = [ { - "joint_purchase?" => true, "buyer_has_seen_privacy_notice?" => true, + "outright_sale?" => false, + "joint_purchase?" => true, + }, + { + "buyer_not_interviewed?" => true, + "outright_sale?" => false, + "joint_purchase?" => true, }, { + "buyer_has_seen_privacy_notice?" => true, "joint_purchase?" => true, + "buyers_will_live_in?" => true, + }, + { "buyer_not_interviewed?" => true, + "joint_purchase?" => true, + "buyers_will_live_in?" => true, }, ] end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 35a596927..7921112b9 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -368,12 +368,48 @@ class SalesLog < Log def ownership_scheme case ownershipsch - when 1 - "shared ownership" - when 2 - "discounted ownership" - when 3 - "outright sale" + when 1 then "shared ownership" + when 2 then "discounted ownership" + when 3 then "outright sale" + end + end + +private + + def reset_invalidated_dependent_fields! + super + + reset_derived_questions! + end + + def reset_derived_questions! + dependencies = [ + { + parent_conditions: [ + { attribute: :buylivein, value: 2 }, + ], + derived_attributes: %i[buy1livein buy2livein], + }, + { + parent_conditions: [ + { attribute: :buylivein, value: 1 }, + { attribute: :jointpur, value: 1 }, + ], + derived_attributes: [:buy1livein], + }, + ] + + dependencies.each do |dependency| + any_parent_attributes_changed = dependency[:parent_conditions].any? { |parent_condition| send("#{parent_condition[:attribute]}_changed?") } + next unless any_parent_attributes_changed + + were_in_derived_state = dependency[:parent_conditions].all? { |parent_condition| send("#{parent_condition[:attribute]}_was") == parent_condition[:value] } + next unless were_in_derived_state + + dependency[:derived_attributes].each do |derived_attribute| + Rails.logger.debug("Cleared derived #{derived_attribute} value") + send("#{derived_attribute}=", nil) + end end end end diff --git a/spec/models/form/sales/pages/buyer1_live_in_property_spec.rb b/spec/models/form/sales/pages/buyer1_live_in_property_spec.rb index 27a00d36a..b42dfeef0 100644 --- a/spec/models/form/sales/pages/buyer1_live_in_property_spec.rb +++ b/spec/models/form/sales/pages/buyer1_live_in_property_spec.rb @@ -28,6 +28,25 @@ RSpec.describe Form::Sales::Pages::Buyer1LiveInProperty, type: :model do end it "has correct depends_on" do - expect(page.depends_on).to eq([{ "buyer_has_seen_privacy_notice?" => true }, { "buyer_not_interviewed?" => true }]) + expect(page.depends_on).to eq([ + { + "buyer_has_seen_privacy_notice?" => true, + "outright_sale?" => false, + }, + { + "buyer_not_interviewed?" => true, + "outright_sale?" => false, + }, + { + "buyer_has_seen_privacy_notice?" => true, + "joint_purchase?" => true, + "buyers_will_live_in?" => true, + }, + { + "buyer_not_interviewed?" => true, + "joint_purchase?" => true, + "buyers_will_live_in?" => true, + }, + ]) end end diff --git a/spec/models/form/sales/pages/buyer2_live_in_property_spec.rb b/spec/models/form/sales/pages/buyer2_live_in_property_spec.rb index bb6ca4b13..bbb54613a 100644 --- a/spec/models/form/sales/pages/buyer2_live_in_property_spec.rb +++ b/spec/models/form/sales/pages/buyer2_live_in_property_spec.rb @@ -30,12 +30,24 @@ RSpec.describe Form::Sales::Pages::Buyer2LiveInProperty, type: :model do it "has correct depends_on" do expect(page.depends_on).to eq([ { - "joint_purchase?" => true, "buyer_has_seen_privacy_notice?" => true, + "outright_sale?" => false, + "joint_purchase?" => true, + }, + { + "buyer_not_interviewed?" => true, + "outright_sale?" => false, + "joint_purchase?" => true, }, { + "buyer_has_seen_privacy_notice?" => true, "joint_purchase?" => true, + "buyers_will_live_in?" => true, + }, + { "buyer_not_interviewed?" => true, + "joint_purchase?" => true, + "buyers_will_live_in?" => true, }, ]) end diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 310fb48d1..ecdc7ec52 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -220,6 +220,61 @@ RSpec.describe SalesLog, type: :model do record_from_db = ActiveRecord::Base.connection.execute("select mortgage from sales_logs where id=#{sales_log.id}").to_a[0] expect(record_from_db["mortgage"]).to eq(0.0) end + + context "when outright sale and buyers will live in the property" do + let(:sales_log) { create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur:) } + + context "and the sale is not a joint purchase" do + let(:jointpur) { 2 } + + it "derives that buyer 1 will live in the property" do + expect(sales_log.buy1livein).to be 1 + end + + it "does not derive a value for whether buyer 2 will live in the property" do + expect(sales_log.buy2livein).to be nil + end + + it "clears that buyer 1 will live in the property if joint purchase is updated" do + sales_log.update!(jointpur: 1) + expect(sales_log.buy1livein).to be nil + end + end + + context "and the sale is a joint purchase" do + let(:jointpur) { 1 } + + it "does not derive values for whether buyer 1 or buyer 2 will live in the property" do + expect(sales_log.buy1livein).to be nil + expect(sales_log.buy2livein).to be nil + end + end + end + + context "when outright sale and buyers will not live in the property" do + let(:sales_log) { create(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur:) } + + context "and the sale is not a joint purchase" do + let(:jointpur) { 2 } + + it "derives that buyer 1 will not live in the property" do + expect(sales_log.buy1livein).to be 2 + end + + it "does not derive a value for whether buyer 2 will live in the property" do + expect(sales_log.buy2livein).to be nil + end + end + + context "and the sale is a joint purchase" do + let(:jointpur) { 1 } + + it "derives that neither buyer 1 nor buyer 2 will live in the property" do + expect(sales_log.buy1livein).to be 2 + expect(sales_log.buy2livein).to be 2 + end + end + end end context "when saving addresses" do diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 3f412efaf..5adc66f76 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -203,7 +203,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "validating fields about buyers living in the property" do - let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } + let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale_setup_complete, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } context "when buyers will live in the property and the sale is a joint purchase" do let(:buylivein) { 1 }