diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 76c47297c..39a17709f 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -425,6 +425,13 @@ private self.prevten = 30 if managing_organisation.provider_type == "LA" end end + (2..8).each do |idx| + if public_send("age#{idx}") && public_send("age#{idx}") < 16 + self["ecstat#{idx}"] = 9 + elsif public_send("ecstat#{idx}") == 9 && (public_send("age#{idx}").nil? || public_send("age#{idx}") >= 16) + self["ecstat#{idx}"] = nil + end + end end def process_postcode_changes! diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 63051a7a0..5777f4306 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1372,6 +1372,17 @@ RSpec.describe CaseLog do record_from_db = ActiveRecord::Base.connection.execute("select totadult from case_logs where id=#{household_case_log.id}").to_a[0] expect(record_from_db["totadult"]).to eq(3) end + + it "correctly derives economic status for tenants under 16" do + record_from_db = ActiveRecord::Base.connection.execute("select ecstat7 from case_logs where id=#{household_case_log.id}").to_a[0] + expect(record_from_db["ecstat7"]).to eq(9) + end + + it "correctly resets economic status when age changes from under 16" do + household_case_log.update!(age7: 17) + record_from_db = ActiveRecord::Base.connection.execute("select ecstat7 from case_logs where id=#{household_case_log.id}").to_a[0] + expect(record_from_db["ecstat7"]).to eq(nil) + end end it "correctly derives and saves has_benefits" do