diff --git a/app/models/case_log.rb b/app/models/case_log.rb index a70dff945..81bc43613 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -39,6 +39,18 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_net_income(record) + return unless record.tenant_economic_status && record.weekly_net_income + + applicable_income_range = IncomeRange.find_by(economic_status: record.tenant_economic_status) + if record.weekly_net_income > applicable_income_range.hard_max + record.errors.add :net_income, "Net income cannot be greater than #{applicable_income_range.hard_max} given the tenant's working situation" + end + if record.weekly_net_income < applicable_income_range.hard_max + record.errors.add :net_income, "Net income cannot be less than #{applicable_income_range.hard_min} given the tenant's working situation" + end + end + def validate(record) # If we've come from the form UI we only want to validate the specific fields # that have just been submitted. If we're submitting a log via API or Bulk Upload @@ -94,6 +106,19 @@ class CaseLog < ApplicationRecord status == "in_progress" end + def weekly_net_income + case net_income_frequency + when "Weekly" + net_income + when "Monthly" + net_income / 4 + when "Yearly" + net_income / 12 + else + nil + end + end + private def update_status! diff --git a/db/migrate/20211022122436_change_field_types.rb b/db/migrate/20211022122436_change_field_types.rb new file mode 100644 index 000000000..b5b09ca53 --- /dev/null +++ b/db/migrate/20211022122436_change_field_types.rb @@ -0,0 +1,9 @@ +class ChangeFieldTypes < ActiveRecord::Migration[6.1] + def up + change_column :case_logs, :net_income, "integer USING CAST(property_number_of_times_relet AS integer)" + end + + def down + change_column :case_logs, :net_income, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index d2cdc5cd3..bcfd1d514 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_10_22_112906) do +ActiveRecord::Schema.define(version: 2021_10_22_122436) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -112,7 +112,7 @@ ActiveRecord::Schema.define(version: 2021_10_22_112906) do t.string "property_major_repairs_date" t.integer "property_number_of_times_relet" t.string "property_wheelchair_accessible" - t.string "net_income" + t.integer "net_income" t.string "net_income_frequency" t.string "net_income_uc_proportion" t.string "housing_benefit" diff --git a/spec/factories/income_range.rb b/spec/factories/income_range.rb new file mode 100644 index 000000000..1bbb5a4ee --- /dev/null +++ b/spec/factories/income_range.rb @@ -0,0 +1,21 @@ +FactoryBot.define do + factory :income_range do + sequence(:id) { |i| i } + trait :full_time do + economic_status { "full_time" } + soft_min { 143 } + soft_max { 730 } + hard_min { 90 } + hard_max { 1230 } + end + trait :retired do + economic_status { "retired" } + soft_min { 50 } + soft_max { 370 } + hard_min { 10 } + hard_max { 690 } + end + created_at { Time.zone.now } + updated_at { Time.zone.now } + end +end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index d3e4d74a9..2c301b5a0 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -26,6 +26,19 @@ RSpec.describe Form, type: :model do expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid) end + context "income ranges" do + let!(:income_range){ FactoryBot.create(:income_range, :full_time) } + + it "validates net income maximum" do + expect { + CaseLog.create!( + tenant_economic_status: "full_time", + net_income: 5000, + net_income_frequency: "Weekly" + ) }.to raise_error(ActiveRecord::RecordInvalid) + end + end + describe "reasonable preference validation" do it "if given reasonable preference is yes a reason must be selected" do expect {