diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 3f17ff1be..24dbea376 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -11,6 +11,7 @@ end class SalesLog < Log include DerivedVariables::SalesLogVariables + include Validations::Sales::SoftValidations self.inheritance_column = :_type_disabled @@ -102,8 +103,4 @@ class SalesLog < Log def london_property? la && LONDON_BOROUGHS.include?(la) end - - def income1_under_soft_min? - true - end end diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb new file mode 100644 index 000000000..5d3dce615 --- /dev/null +++ b/app/models/validations/sales/soft_validations.rb @@ -0,0 +1,15 @@ +module Validations::Sales::SoftValidations + ALLOWED_INCOME_RANGES = { + 1 => OpenStruct.new(soft_min: 5000), + 2 => OpenStruct.new(soft_min: 1500), + 3 => OpenStruct.new(soft_min: 1000), + 5 => OpenStruct.new(soft_min: 2000), + 0 => OpenStruct.new(soft_min: 2000), + }.freeze + + def income1_under_soft_min? + return false unless ecstat1 && income1 && ALLOWED_INCOME_RANGES[ecstat1] + + income1 < ALLOWED_INCOME_RANGES[ecstat1][:soft_min] + end +end diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb new file mode 100644 index 000000000..f9cac3d47 --- /dev/null +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -0,0 +1,89 @@ +require "rails_helper" + +RSpec.describe Validations::Sales::SoftValidations do + let(:record) { FactoryBot.create(:sales_log) } + + describe "income1 min validations" do + context "when validating soft min" do + it "returns false if no income1 is given" do + record.income1 = nil + expect(record) + .not_to be_income1_under_soft_min + end + + it "returns false if no ecstat1 is given" do + record.ecstat1 = nil + expect(record) + .not_to be_income1_under_soft_min + end + + [ + { + income1: 4500, + ecstat1: 1, + }, + { + income1: 1400, + ecstat1: 2, + }, + { + income1: 999, + ecstat1: 3, + }, + { + income1: 1899, + ecstat1: 5, + }, + { + income1: 1888, + ecstat1: 0, + }, + ].each do |test_case| + it "returns true if income1 is below soft min for ecstat1 #{test_case[:ecstat1]}" do + record.income1 = test_case[:income1] + record.ecstat1 = test_case[:ecstat1] + expect(record) + .to be_income1_under_soft_min + end + end + + [ + { + income1: 5001, + ecstat1: 1, + }, + { + income1: 1600, + ecstat1: 2, + }, + { + income1: 1004, + ecstat1: 3, + }, + { + income1: 2899, + ecstat1: 4, + }, + { + income1: 2899, + ecstat1: 5, + }, + { + income1: 5, + ecstat1: 6, + }, + { + income1: 10_888, + ecstat1: 0, + }, + ].each do |test_case| + it "returns false if income1 is over soft min for ecstat1 #{test_case[:ecstat1]}" do + record.income1 = test_case[:income1] + record.ecstat1 = test_case[:ecstat1] + expect(record) + .not_to be_income1_under_soft_min + end + end + end + end +end