diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 50444d311..30a16a21e 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -56,6 +56,20 @@ class CaseLog < ApplicationRecord enum ecstat6: ECSTAT, _suffix: true enum ecstat7: ECSTAT, _suffix: true enum ecstat8: ECSTAT, _suffix: true + enum relat2: RELAT, _suffix: true + enum relat3: RELAT, _suffix: true + enum relat4: RELAT, _suffix: true + enum relat5: RELAT, _suffix: true + enum relat6: RELAT, _suffix: true + enum relat7: RELAT, _suffix: true + enum relat8: RELAT, _suffix: true + enum sex2: GENDER, _suffix: true + enum sex3: GENDER, _suffix: true + enum sex4: GENDER, _suffix: true + enum sex5: GENDER, _suffix: true + enum sex6: GENDER, _suffix: true + enum sex7: GENDER, _suffix: true + enum sex8: GENDER, _suffix: true enum prevten: PREVIOUS_TENANCY, _suffix: true enum homeless: HOMELESS, _suffix: true enum underoccupation_benefitcap: BENEFITCAP, _suffix: true @@ -119,7 +133,7 @@ class CaseLog < ApplicationRecord enum postcode_known: POLAR, _suffix: true enum la_known: POLAR, _suffix: true - AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype is_la_inferred].freeze + AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype is_la_inferred totchild].freeze OPTIONAL_FIELDS = %w[postcode_known la_known first_time_property_let_as_social_housing].freeze @@ -199,6 +213,12 @@ private self.lettype = "#{renttype} #{needstype} #{owning_organisation['Org type']}" if renttype.present? && needstype.present? && owning_organisation["Org type"].present? self.is_la_inferred = false if is_la_inferred.nil? self.la = get_la(property_postcode) + self.totchild = get_totchild + end + + def get_totchild + relationships = [relat2, relat3, relat4, relat5, relat6, relat7, relat8] + relationships.count("Child - includes young adult and grown-up") end def get_la(postcode) diff --git a/app/models/constants/case_log.rb b/app/models/constants/case_log.rb index 3a537035d..a7a5322a0 100644 --- a/app/models/constants/case_log.rb +++ b/app/models/constants/case_log.rb @@ -733,4 +733,18 @@ module Constants::CaseLog "Waltham Forest", "Wandsworth", "Westminster"].freeze + + RELAT = { + "Child - includes young adult and grown-up" => "C", + "Partner" => "P", + "Other" => "X", + "Prefer not to say" => "R", + }.freeze + + GENDER = { + "Female" => "F", + "Male" => "M", + "Non-binary" => "X", + "Prefer not to say" => "R", + }.freeze end diff --git a/db/migrate/20211214143841_add_totchild.rb b/db/migrate/20211214143841_add_totchild.rb new file mode 100644 index 000000000..e4fe045e5 --- /dev/null +++ b/db/migrate/20211214143841_add_totchild.rb @@ -0,0 +1,7 @@ +class AddTotchild < ActiveRecord::Migration[6.1] + def change + change_table :case_logs, bulk: true do |t| + t.column :totchild, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 70cdb5569..dce7cd20a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_12_13_122642) do +ActiveRecord::Schema.define(version: 2021_12_14_143841) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -175,6 +175,7 @@ ActiveRecord::Schema.define(version: 2021_12_13_122642) do t.integer "month" t.integer "year" t.boolean "is_la_inferred" + t.integer "totchild" t.index ["discarded_at"], name: "index_case_logs_on_discarded_at" 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" diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index c8ad9195e..ea4e353f7 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1059,5 +1059,26 @@ RSpec.describe Form, type: :model do expect(record_from_db["la"]).to eq(nil) end end + + context "household members derived vars" do + let!(:household_case_log) do + CaseLog.create({ + managing_organisation: organisation, + owning_organisation: organisation, + other_hhmemb: 4, + relat2: "Child - includes young adult and grown-up", + relat3: "Child - includes young adult and grown-up", + relat4: "Other", + relat5: "Child - includes young adult and grown-up", + }) + end + + it "correctly derives and saves totchild" do + household_case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select totchild from case_logs where id=#{household_case_log.id}").to_a[0] + expect(record_from_db["totchild"]).to eq(3) + end + end end end