diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index fb174a9c1..d0aab14cb 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -115,6 +115,8 @@ module DerivedVariables::LettingsLogVariables self.uprn_known = 0 end + self.nationality_all = nationality_all_group if nationality_uk_or_prefers_not_to_say? + reset_address_fields! if is_supported_housing? end diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index 6532b228f..12d860492 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -35,6 +35,9 @@ module DerivedVariables::SalesLogVariables self.uprn_known = 0 end + self.nationality_all = nationality_all_group if nationality_uk_or_prefers_not_to_say? + self.nationality_all_buyer2 = nationality_all_buyer2_group if nationality2_uk_or_prefers_not_to_say? + set_encoded_derived_values!(DEPENDENCIES) end diff --git a/app/models/log.rb b/app/models/log.rb index 8814e771d..5de8b924c 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -209,6 +209,10 @@ class Log < ApplicationRecord end end + def nationality_uk_or_prefers_not_to_say? + nationality_all_group == 13 || nationality_all_group == 826 + end + private # Handle logs that are older than previous collection start date diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 5acd409de..1591e4a14 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -473,4 +473,8 @@ class SalesLog < Log def duplicates SalesLog.where.not(duplicate_set_id: nil).where(duplicate_set_id:).where.not(id:) end + + def nationality2_uk_or_prefers_not_to_say? + nationality_all_buyer2_group == 13 || nationality_all_buyer2_group == 826 + end end diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index f2bbbe4be..7d2fcc8c2 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -2330,6 +2330,43 @@ RSpec.describe LettingsLog do expect(record_from_db["irproduct"]).to eq(nil) end end + + context "when updating nationality_all_group" do + let!(:lettings_log) do + described_class.create({ + managing_organisation: owning_organisation, + owning_organisation:, + created_by: created_by_user, + startdate: Time.zone.local(2024, 4, 10), + needstype: 1, + renewal: 1, + rent_type: 1, + }) + end + + before do + Timecop.freeze(Time.zone.local(2024, 4, 10)) + Singleton.__init__(FormHandler) + end + + after do + Timecop.return + Singleton.__init__(FormHandler) + end + + it "correctly derives nationality_all when it's UK" do + expect { lettings_log.update!(nationality_all_group: 826, declaration: 1) }.to change(lettings_log, :nationality_all).to 826 + end + + it "correctly derives nationality_all when it's prefers not to say" do + expect { lettings_log.update!(nationality_all_group: 13, declaration: 1) }.to change(lettings_log, :nationality_all).to 13 + end + + it "does not derive nationality_all when it's other or not given" do + expect { lettings_log.update!(nationality_all_group: 12, declaration: 1) }.not_to change(lettings_log, :nationality_all) + expect { lettings_log.update!(nationality_all_group: nil, declaration: 1) }.not_to change(lettings_log, :nationality_all) + end + end end describe "optional fields" do diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index edaf4b60a..87c9b32f4 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -577,6 +577,25 @@ RSpec.describe SalesLog, type: :model do end end end + + context "when deriving nationality variables" do + it "correctly derives nationality_all/nationality_all_buyer2 when it's UK" do + expect { sales_log.update!(nationality_all_group: 826) }.to change(sales_log, :nationality_all).to 826 + expect { sales_log.update!(nationality_all_buyer2_group: 826) }.to change(sales_log, :nationality_all_buyer2).to 826 + end + + it "correctly derives nationality_all/nationality_all_buyer2 when buyer prefers not to say" do + expect { sales_log.update!(nationality_all_group: 13) }.to change(sales_log, :nationality_all).to 13 + expect { sales_log.update!(nationality_all_buyer2_group: 13) }.to change(sales_log, :nationality_all_buyer2).to 13 + end + + it "does not derive nationality_all/nationality_all_buyer2 when it is other or not given" do + expect { sales_log.update!(nationality_all_group: 12) }.not_to change(sales_log, :nationality_all) + expect { sales_log.update!(nationality_all_buyer2_group: 12) }.not_to change(sales_log, :nationality_all_buyer2) + expect { sales_log.update!(nationality_all_group: nil) }.not_to change(sales_log, :nationality_all) + expect { sales_log.update!(nationality_all_buyer2_group: nil) }.not_to change(sales_log, :nationality_all_buyer2) + end + end end context "when saving addresses" do