From 20754e5fe66bf811c0c0bbe66cd20c772838ac48 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 1 Dec 2021 11:30:15 +0000 Subject: [PATCH] Cldc 648 derived rent type (#123) * Add renttype column * infer renttype value from rent_type * Fix typo * Add renttype column * infer renttype value from rent_type * Fix typo * Update inferred fields in the db * Disable rubocop for datetime * remove new line * refactor --- app/constants/db_enums.rb | 8 ++ app/models/case_log.rb | 84 +++++++------------ config/forms/2021_2022.json | 2 +- db/migrate/20211130090246_add_rent_type.rb | 7 ++ db/schema.rb | 3 +- spec/controllers/case_logs_controller_spec.rb | 49 ----------- spec/fixtures/complete_case_log.json | 3 +- spec/models/case_log_spec.rb | 77 +++++++++++++++-- 8 files changed, 120 insertions(+), 113 deletions(-) create mode 100644 db/migrate/20211130090246_add_rent_type.rb diff --git a/app/constants/db_enums.rb b/app/constants/db_enums.rb index ad898c37a..0dc271baf 100644 --- a/app/constants/db_enums.rb +++ b/app/constants/db_enums.rb @@ -725,4 +725,12 @@ module DbEnums "A spouse / civil partner of a UK Armed Forces member who has separated or been bereaved within the last 2 years" => 5, } end + + def self.renttype + { + "Social Rent" => 1, + "Affordable Rent" => 2, + "Intermediate Rent" => 3, + } + end end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index acd65bb0c..478626381 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -109,8 +109,9 @@ class CaseLog < ApplicationRecord enum unitletas: DbEnums.unitletas, _suffix: true enum builtype: DbEnums.builtype, _suffix: true enum incref: DbEnums.polar, _suffix: true + enum renttype: DbEnums.renttype, _suffix: true - AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze + AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype].freeze OPTIONAL_FIELDS = %w[do_you_know_the_postcode do_you_know_the_local_authority first_time_property_let_as_social_housing].freeze @@ -144,66 +145,23 @@ class CaseLog < ApplicationRecord end end - def postcode - if property_postcode.present? - UKPostcode.parse(property_postcode).outcode - end - end - - def postcod2 - if property_postcode.present? - UKPostcode.parse(property_postcode).incode - end - end - - def ppostc1 - if previous_postcode.present? - UKPostcode.parse(previous_postcode).outcode - end - end - - def ppostc2 - if previous_postcode.present? - UKPostcode.parse(previous_postcode).incode - end - end - - def hhmemb - other_hhmemb.presence - end - def applicable_income_range return unless ecstat1 IncomeRange::ALLOWED[ecstat1.to_sym] end - def mrcday - if mrcdate.present? - mrcdate.day - end - end - - def mrcmonth - if mrcdate.present? - mrcdate.month - end - end - - def mrcyear - if mrcdate.present? - mrcdate.year - end - end - - def incref - if net_income_known == "Prefer not to say" - 1 - end - end - private + RENT_TYPE_MAPPING = { + "Social Rent" => "Social Rent", + "Affordable Rent" => "Affordable Rent", + "London Affordable Rent" => "Affordable Rent", + "Rent To Buy" => "Intermediate Rent", + "London Living Rent" => "Intermediate Rent", + "Other Intermediate Rent Product" => "Intermediate Rent", + }.freeze + def update_status! self.status = if all_fields_completed? && errors.empty? "completed" @@ -212,6 +170,26 @@ private else "in_progress" end + set_derived_fields + end + + def set_derived_fields + if property_postcode.present? + self.postcode = UKPostcode.parse(property_postcode).outcode + self.postcod2 = UKPostcode.parse(property_postcode).incode + end + if previous_postcode.present? + self.ppostc1 = UKPostcode.parse(previous_postcode).outcode + self.ppostc2 = UKPostcode.parse(previous_postcode).incode + end + if mrcdate.present? + self.mrcday = mrcdate.day + self.mrcmonth = mrcdate.month + self.mrcyear = mrcdate.year + end + self.incref = 1 if net_income_known == "Prefer not to say" + self.hhmemb = other_hhmemb + 1 if other_hhmemb.present? + self.renttype = RENT_TYPE_MAPPING[rent_type] end def all_fields_completed? diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index ed055cc3c..0ef875dc1 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1734,7 +1734,7 @@ "type": "radio", "answer_options": { "0": "No", - "1": "Yesx" + "1": "Yes" }, "conditional_for": { "mrcdate": ["Yes"] diff --git a/db/migrate/20211130090246_add_rent_type.rb b/db/migrate/20211130090246_add_rent_type.rb new file mode 100644 index 000000000..5df05be91 --- /dev/null +++ b/db/migrate/20211130090246_add_rent_type.rb @@ -0,0 +1,7 @@ +class AddRentType < ActiveRecord::Migration[6.1] + def change + change_table :case_logs, bulk: true do |t| + t.column :renttype, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e88f28f52..393589bb2 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_11_26_142105) do +ActiveRecord::Schema.define(version: 2021_11_30_090246) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -168,6 +168,7 @@ ActiveRecord::Schema.define(version: 2021_11_26_142105) do t.bigint "owning_organisation_id" t.bigint "managing_organisation_id" t.datetime "property_void_date" + t.integer "renttype" 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/controllers/case_logs_controller_spec.rb b/spec/controllers/case_logs_controller_spec.rb index 43458e1b8..1c784eaa3 100644 --- a/spec/controllers/case_logs_controller_spec.rb +++ b/spec/controllers/case_logs_controller_spec.rb @@ -168,54 +168,5 @@ RSpec.describe CaseLogsController, type: :controller do expect(response).to redirect_to("/case_logs/#{id}/conditional_question_no_page") end end - - context "partition postcode" do - let(:case_log_with_postcode) do - { - property_postcode: "M1 1AE", - previous_postcode: "M2 2AE", - page: "property_postcode", - } - end - it "saves full and partial postcodes" do - post :submit_form, params: { id: id, case_log: case_log_with_postcode } - case_log.reload - - expect(case_log.property_postcode).to eq("M1 1AE") - expect(case_log.postcode).to eq("M1") - expect(case_log.postcod2).to eq("1AE") - end - - it "saves full and partial previous postcodes" do - post :submit_form, params: { id: id, case_log: case_log_with_postcode } - case_log.reload - - expect(case_log.previous_postcode).to eq("M2 2AE") - expect(case_log.ppostc1).to eq("M2") - expect(case_log.ppostc2).to eq("2AE") - end - end - - context "partition date" do - let(:case_log_with_date) do - { - "mrcdate(1i)": "2021", - "mrcdate(2i)": "05", - "mrcdate(3i)": "04", - page: "property_major_repairs", - } - end - it "saves full and partial dates" do - post :submit_form, params: { id: id, case_log: case_log_with_date } - case_log.reload - - expect(case_log.mrcdate.day).to eq(4) - expect(case_log.mrcdate.month).to eq(5) - expect(case_log.mrcdate.year).to eq(2021) - expect(case_log.mrcday).to eq(4) - expect(case_log.mrcmonth).to eq(5) - expect(case_log.mrcyear).to eq(2021) - end - end end end diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index 88e72fc34..a01ae395a 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -143,6 +143,7 @@ "property_wheelchair_accessible": "Yes", "void_or_renewal_date": "05/05/2020", "tenant_same_property_renewal": "Yes", - "new_build_handover_date": "01/01/2019" + "new_build_handover_date": "01/01/2019", + "renttype": 1 } } diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 84602d1dc..b05d925c1 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -816,14 +816,6 @@ RSpec.describe Form, type: :model do end end - describe "incref" do - let(:case_log) { FactoryBot.build(:case_log, net_income_known: "Prefer not to say") } - - it "sets income refused to Yes" do - expect(case_log.incref).to eq(1) - end - end - describe "weekly_net_income" do let(:net_income) { 5000 } let(:case_log) { FactoryBot.build(:case_log, earnings: net_income) } @@ -843,4 +835,73 @@ RSpec.describe Form, type: :model do expect(case_log.weekly_net_income).to eq(417) end end + + describe "derived variables" do + require "date" + let(:organisation) { FactoryBot.create(:organisation) } + let!(:case_log) do + CaseLog.create({ + managing_organisation: organisation, + owning_organisation: organisation, + property_postcode: "M1 1AE", + previous_postcode: "M2 2AE", + # rubocop:disable Style/DateTime + mrcdate: DateTime.new(2021, 5, 4), + # rubocop:enable Style/DateTime + net_income_known: "Prefer not to say", + other_hhmemb: 6, + rent_type: "London Living Rent", + }) + end + + it "correctly derives and saves partial and full postcodes" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select postcode, postcod2 from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["postcode"]).to eq("M1") + expect(record_from_db["postcod2"]).to eq("1AE") + end + + it "correctly derives and saves partial and full previous postcodes" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select ppostc1, ppostc2 from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["ppostc1"]).to eq("M2") + expect(record_from_db["ppostc2"]).to eq("2AE") + end + + it "correctly derives and saves partial and full major repairs date" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select mrcday, mrcmonth, mrcyear, mrcdate from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["mrcdate"].day).to eq(4) + expect(record_from_db["mrcdate"].month).to eq(5) + expect(record_from_db["mrcdate"].year).to eq(2021) + expect(record_from_db["mrcday"]).to eq(4) + expect(record_from_db["mrcmonth"]).to eq(5) + expect(record_from_db["mrcyear"]).to eq(2021) + end + + it "correctly derives and saves incref" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select incref from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["incref"]).to eq(1) + end + + it "correctly derives and saves hhmemb" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select hhmemb from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["hhmemb"]).to eq(7) + end + + it "correctly derives and saves renttype" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select renttype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.renttype).to eq("Intermediate Rent") + expect(record_from_db["renttype"]).to eq(3) + end + end end