Browse Source

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
pull/122/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
20754e5fe6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/constants/db_enums.rb
  2. 84
      app/models/case_log.rb
  3. 2
      config/forms/2021_2022.json
  4. 7
      db/migrate/20211130090246_add_rent_type.rb
  5. 3
      db/schema.rb
  6. 49
      spec/controllers/case_logs_controller_spec.rb
  7. 3
      spec/fixtures/complete_case_log.json
  8. 77
      spec/models/case_log_spec.rb

8
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

84
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?

2
config/forms/2021_2022.json

@ -1734,7 +1734,7 @@
"type": "radio",
"answer_options": {
"0": "No",
"1": "Yesx"
"1": "Yes"
},
"conditional_for": {
"mrcdate": ["Yes"]

7
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

3
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"

49
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

3
spec/fixtures/complete_case_log.json vendored

@ -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
}
}

77
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

Loading…
Cancel
Save