diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 394dd62bf..ee325522a 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -371,6 +371,7 @@ private self.nocharge = household_charge&.zero? ? 1 : 0 self.underoccupation_benefitcap = 3 if renewal == 1 && year == 2021 self.ethnic = ethnic || ethnic_group + self.housingneeds = get_housingneeds if is_renewal? self.underoccupation_benefitcap = 2 if year == 2021 self.homeless = 2 @@ -480,6 +481,19 @@ private end end + def get_housingneeds + if [housingneeds_a, housingneeds_b, housingneeds_c, housingneeds_f].any?(1) + return 1 + end + if housingneeds_g == 1 + return 2 + end + + if housingneeds_h == 1 + 3 + end + end + def all_fields_completed? mandatory_fields.none? { |field| public_send(field).nil? if respond_to?(field) } end diff --git a/db/migrate/20220318140754_add_housing_needs_field.rb b/db/migrate/20220318140754_add_housing_needs_field.rb new file mode 100644 index 000000000..79e1a36e0 --- /dev/null +++ b/db/migrate/20220318140754_add_housing_needs_field.rb @@ -0,0 +1,7 @@ +class AddHousingNeedsField < ActiveRecord::Migration[7.0] + def change + change_table :case_logs, bulk: true do |t| + t.column :housingneeds, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 79e41a8ea..38fcc3d26 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -224,6 +224,7 @@ ActiveRecord::Schema[7.0].define(version: 202202071123100) do t.decimal "wsupchrg", precision: 10, scale: 2 t.decimal "wtcharge", precision: 10, scale: 2 t.decimal "wtshortfall", precision: 10, scale: 2 + t.integer "housingneeds" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" end diff --git a/spec/fixtures/exports/case_logs.xml b/spec/fixtures/exports/case_logs.xml index bcb090842..cbd70ecd6 100644 --- a/spec/fixtures/exports/case_logs.xml +++ b/spec/fixtures/exports/case_logs.xml @@ -166,5 +166,6 @@ 17.5 162.5 6.0 + 1 diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 15b50a4e9..e80e7216e 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1366,6 +1366,33 @@ RSpec.describe CaseLog do expect(case_log["referral"]).to eq(0) end end + + context "when the data provider is filling in household needs" do + let!(:case_log) do + described_class.create({ + managing_organisation: organisation, + owning_organisation: organisation, + }) + end + + it "correctly derives and saves housing neeeds as 1" do + case_log.update!(housingneeds_a: 1) + record_from_db = ActiveRecord::Base.connection.execute("select housingneeds from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["housingneeds"]).to eq(1) + end + + it "correctly derives and saves housing neeeds as 2" do + case_log.update!(housingneeds_g: 1) + record_from_db = ActiveRecord::Base.connection.execute("select housingneeds from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["housingneeds"]).to eq(2) + end + + it "correctly derives and saves housing neeeds as 3" do + case_log.update!(housingneeds_h: 1) + record_from_db = ActiveRecord::Base.connection.execute("select housingneeds from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["housingneeds"]).to eq(3) + end + end end describe "resetting invalidated fields" do