Browse Source

CLDC-1924 Add monthly charges validation (#1247)

* Add monthly_charges_value_check field to sales

* Add monthly_charges_over_soft_max soft validation

* Add monthly_charges_value_check page and question

* Add monthly_charges_value_check pages to the relevant subsections

* Fix optional field name, add validation message

* test
pull/1253/head^2
kosiakkatrina 2 years ago committed by GitHub
parent
commit
b2d5f9f546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      app/models/form/sales/pages/monthly_charges_value_check.rb
  2. 23
      app/models/form/sales/questions/monthly_charges_value_check.rb
  3. 1
      app/models/form/sales/subsections/discounted_ownership_scheme.rb
  4. 1
      app/models/form/sales/subsections/outright_sale.rb
  5. 1
      app/models/form/sales/subsections/property_information.rb
  6. 1
      app/models/form/sales/subsections/setup.rb
  7. 1
      app/models/form/sales/subsections/shared_ownership_scheme.rb
  8. 2
      app/models/sales_log.rb
  9. 7
      app/models/validations/sales/soft_validations.rb
  10. 2
      config/locales/en.yml
  11. 7
      db/migrate/20230127102334_add_monthly_charges_value_check.rb
  12. 3
      db/schema.rb
  13. 48
      spec/models/form/sales/pages/monthly_charges_value_check_spec.rb
  14. 57
      spec/models/form/sales/questions/monthly_charges_value_check_spec.rb
  15. 1
      spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb
  16. 1
      spec/models/form/sales/subsections/outright_sale_spec.rb
  17. 1
      spec/models/form/sales/subsections/property_information_spec.rb
  18. 1
      spec/models/form/sales/subsections/setup_spec.rb
  19. 1
      spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb
  20. 4
      spec/models/form_handler_spec.rb
  21. 4
      spec/models/form_spec.rb
  22. 2
      spec/models/sales_log_spec.rb
  23. 62
      spec/models/validations/sales/soft_validations_spec.rb

21
app/models/form/sales/pages/monthly_charges_value_check.rb

@ -0,0 +1,21 @@
class Form::Sales::Pages::MonthlyChargesValueCheck < ::Form::Page
def initialize(id, hsh, subsection)
super
@depends_on = [
{
"monthly_charges_over_soft_max?" => true,
},
]
@title_text = {
"translation" => "soft_validations.monthly_charges_over_soft_max.title_text",
"arguments" => [],
}
@informative_text = {}
end
def questions
@questions ||= [
Form::Sales::Questions::MonthlyChargesValueCheck.new(nil, nil, self),
]
end
end

23
app/models/form/sales/questions/monthly_charges_value_check.rb

@ -0,0 +1,23 @@
class Form::Sales::Questions::MonthlyChargesValueCheck < ::Form::Question
def initialize(id, hsh, page)
super
@id = "monthly_charges_value_check"
@check_answer_label = "Monthly charges confirmation"
@header = "Are you sure this is correct?"
@type = "interruption_screen"
@answer_options = {
"0" => { "value" => "Yes" },
"1" => { "value" => "No" },
}
@hidden_in_check_answers = {
"depends_on" => [
{
"monthly_charges_value_check" => 0,
},
{
"monthly_charges_value_check" => 1,
},
],
}
end
end

1
app/models/form/sales/subsections/discounted_ownership_scheme.rb

@ -29,6 +29,7 @@ class Form::Sales::Subsections::DiscountedOwnershipScheme < ::Form::Subsection
Form::Sales::Pages::DepositValueCheck.new("discounted_ownership_deposit_value_check", nil, self), Form::Sales::Pages::DepositValueCheck.new("discounted_ownership_deposit_value_check", nil, self),
Form::Sales::Pages::DepositAndMortgageValueCheck.new("discounted_ownership_deposit_and_mortgage_value_check_after_deposit", nil, self), Form::Sales::Pages::DepositAndMortgageValueCheck.new("discounted_ownership_deposit_and_mortgage_value_check_after_deposit", nil, self),
Form::Sales::Pages::LeaseholdCharges.new("leasehold_charges_discounted_ownership", nil, self), Form::Sales::Pages::LeaseholdCharges.new("leasehold_charges_discounted_ownership", nil, self),
Form::Sales::Pages::MonthlyChargesValueCheck.new("monthly_charges_discounted_ownership_value_check", nil, self),
] ]
end end

1
app/models/form/sales/subsections/outright_sale.rb

@ -18,6 +18,7 @@ class Form::Sales::Subsections::OutrightSale < ::Form::Subsection
Form::Sales::Pages::AboutDepositWithoutDiscount.new("about_deposit_outright_sale", nil, self), Form::Sales::Pages::AboutDepositWithoutDiscount.new("about_deposit_outright_sale", nil, self),
Form::Sales::Pages::DepositValueCheck.new("outright_sale_deposit_value_check", nil, self), Form::Sales::Pages::DepositValueCheck.new("outright_sale_deposit_value_check", nil, self),
Form::Sales::Pages::LeaseholdCharges.new("leasehold_charges_outright_sale", nil, self), Form::Sales::Pages::LeaseholdCharges.new("leasehold_charges_outright_sale", nil, self),
Form::Sales::Pages::MonthlyChargesValueCheck.new("monthly_charges_outright_sale_value_check", nil, self),
] ]
end end

1
app/models/form/sales/subsections/property_information.rb

@ -10,6 +10,7 @@ class Form::Sales::Subsections::PropertyInformation < ::Form::Subsection
@pages ||= [ @pages ||= [
Form::Sales::Pages::PropertyNumberOfBedrooms.new(nil, nil, self), Form::Sales::Pages::PropertyNumberOfBedrooms.new(nil, nil, self),
Form::Sales::Pages::PropertyUnitType.new(nil, nil, self), Form::Sales::Pages::PropertyUnitType.new(nil, nil, self),
Form::Sales::Pages::MonthlyChargesValueCheck.new("monthly_charges_property_type_value_check", nil, self),
Form::Sales::Pages::PropertyBuildingType.new(nil, nil, self), Form::Sales::Pages::PropertyBuildingType.new(nil, nil, self),
Form::Sales::Pages::Postcode.new(nil, nil, self), Form::Sales::Pages::Postcode.new(nil, nil, self),
Form::Sales::Pages::PropertyLocalAuthority.new(nil, nil, self), Form::Sales::Pages::PropertyLocalAuthority.new(nil, nil, self),

1
app/models/form/sales/subsections/setup.rb

@ -16,6 +16,7 @@ class Form::Sales::Subsections::Setup < ::Form::Subsection
Form::Sales::Pages::DiscountedOwnershipType.new(nil, nil, self), Form::Sales::Pages::DiscountedOwnershipType.new(nil, nil, self),
Form::Sales::Pages::OutrightOwnershipType.new(nil, nil, self), Form::Sales::Pages::OutrightOwnershipType.new(nil, nil, self),
Form::Sales::Pages::OldPersonsSharedOwnershipValueCheck.new("ownership_type_old_persons_shared_ownership_value_check", nil, self), Form::Sales::Pages::OldPersonsSharedOwnershipValueCheck.new("ownership_type_old_persons_shared_ownership_value_check", nil, self),
Form::Sales::Pages::MonthlyChargesValueCheck.new("monthly_charges_type_value_check", nil, self),
Form::Sales::Pages::BuyerCompany.new(nil, nil, self), Form::Sales::Pages::BuyerCompany.new(nil, nil, self),
Form::Sales::Pages::BuyerLive.new(nil, nil, self), Form::Sales::Pages::BuyerLive.new(nil, nil, self),
Form::Sales::Pages::JointPurchase.new(nil, nil, self), Form::Sales::Pages::JointPurchase.new(nil, nil, self),

1
app/models/form/sales/subsections/shared_ownership_scheme.rb

@ -36,6 +36,7 @@ class Form::Sales::Subsections::SharedOwnershipScheme < ::Form::Subsection
Form::Sales::Pages::SharedOwnershipDepositValueCheck.new("shared_ownership_deposit_value_check", nil, self), Form::Sales::Pages::SharedOwnershipDepositValueCheck.new("shared_ownership_deposit_value_check", nil, self),
Form::Sales::Pages::MonthlyRent.new(nil, nil, self), Form::Sales::Pages::MonthlyRent.new(nil, nil, self),
Form::Sales::Pages::LeaseholdCharges.new("leasehold_charges_shared_ownership", nil, self), Form::Sales::Pages::LeaseholdCharges.new("leasehold_charges_shared_ownership", nil, self),
Form::Sales::Pages::MonthlyChargesValueCheck.new("monthly_charges_shared_ownership_value_check", nil, self),
] ]
end end

2
app/models/sales_log.rb

@ -35,7 +35,7 @@ class SalesLog < Log
scope :search_by, ->(param) { filter_by_id(param) } scope :search_by, ->(param) { filter_by_id(param) }
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) }
OPTIONAL_FIELDS = %w[purchid old_persons_shared_ownership_value_check].freeze OPTIONAL_FIELDS = %w[purchid monthly_charges_value_check old_persons_shared_ownership_value_check].freeze
RETIREMENT_AGES = { "M" => 65, "F" => 60, "X" => 65 }.freeze RETIREMENT_AGES = { "M" => 65, "F" => 60, "X" => 65 }.freeze
def startdate def startdate

7
app/models/validations/sales/soft_validations.rb

@ -75,4 +75,11 @@ module Validations::Sales::SoftValidations
!grant.between?(9_000, 16_000) !grant.between?(9_000, 16_000)
end end
def monthly_charges_over_soft_max?
return unless type && mscharge && proptype
soft_max = old_persons_shared_ownership? ? 550 : 300
mscharge > soft_max
end
end end

2
config/locales/en.yml

@ -470,6 +470,8 @@ en:
shared_owhership_deposit: shared_owhership_deposit:
title_text: "Mortgage, deposit and cash discount total should equal £%{expected_shared_ownership_deposit_value}" title_text: "Mortgage, deposit and cash discount total should equal £%{expected_shared_ownership_deposit_value}"
old_persons_shared_ownership: "At least one buyer should be aged over 64 for Older persons’ shared ownership scheme" old_persons_shared_ownership: "At least one buyer should be aged over 64 for Older persons’ shared ownership scheme"
monthly_charges_over_soft_max:
title_text: "The amount of monthly charges is high for this type of property and sale type"
devise: devise:
two_factor_authentication: two_factor_authentication:

7
db/migrate/20230127102334_add_monthly_charges_value_check.rb

@ -0,0 +1,7 @@
class AddMonthlyChargesValueCheck < ActiveRecord::Migration[7.0]
def change
change_table :sales_logs, bulk: true do |t|
t.column :monthly_charges_value_check, :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. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_01_26_145529) do ActiveRecord::Schema[7.0].define(version: 2023_01_27_102334) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -509,6 +509,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_26_145529) do
t.integer "deposit_and_mortgage_value_check" t.integer "deposit_and_mortgage_value_check"
t.integer "shared_ownership_deposit_value_check" t.integer "shared_ownership_deposit_value_check"
t.integer "old_persons_shared_ownership_value_check" t.integer "old_persons_shared_ownership_value_check"
t.integer "monthly_charges_value_check"
t.index ["bulk_upload_id"], name: "index_sales_logs_on_bulk_upload_id" t.index ["bulk_upload_id"], name: "index_sales_logs_on_bulk_upload_id"
t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id"
t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id" t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id"

48
spec/models/form/sales/pages/monthly_charges_value_check_spec.rb

@ -0,0 +1,48 @@
require "rails_helper"
RSpec.describe Form::Sales::Pages::MonthlyChargesValueCheck, type: :model do
subject(:page) { described_class.new(page_id, page_definition, subsection) }
let(:page_id) { "monthly_charges_value_check" }
let(:page_definition) { nil }
let(:subsection) { instance_double(Form::Subsection) }
it "has correct subsection" do
expect(page.subsection).to eq(subsection)
end
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[monthly_charges_value_check])
end
it "has the correct id" do
expect(page.id).to eq("monthly_charges_value_check")
end
it "has the correct header" do
expect(page.header).to be_nil
end
it "has correct depends_on" do
expect(page.depends_on).to eq([
{
"monthly_charges_over_soft_max?" => true,
},
])
end
it "is interruption screen page" do
expect(page.interruption_screen?).to eq(true)
end
it "has correct title_text" do
expect(page.title_text).to eq({
"translation" => "soft_validations.monthly_charges_over_soft_max.title_text",
"arguments" => [],
})
end
it "has correct informative_text" do
expect(page.informative_text).to eq({})
end
end

57
spec/models/form/sales/questions/monthly_charges_value_check_spec.rb

@ -0,0 +1,57 @@
require "rails_helper"
RSpec.describe Form::Sales::Questions::MonthlyChargesValueCheck, type: :model do
subject(:question) { described_class.new(question_id, question_definition, page) }
let(:question_id) { "monthly_charges_value_check" }
let(:question_definition) { nil }
let(:page) { instance_double(Form::Page) }
it "has correct page" do
expect(question.page).to eq(page)
end
it "has the correct id" do
expect(question.id).to eq("monthly_charges_value_check")
end
it "has the correct header" do
expect(question.header).to eq("Are you sure this is correct?")
end
it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Monthly charges confirmation")
end
it "has the correct type" do
expect(question.type).to eq("interruption_screen")
end
it "is not marked as derived" do
expect(question.derived?).to be false
end
it "has the correct hint" do
expect(question.hint_text).to be_nil
end
it "has the correct answer_options" do
expect(question.answer_options).to eq({
"0" => { "value" => "Yes" },
"1" => { "value" => "No" },
})
end
it "has the correct hidden_in_check_answers" do
expect(question.hidden_in_check_answers).to eq({
"depends_on" => [
{
"monthly_charges_value_check" => 0,
},
{
"monthly_charges_value_check" => 1,
},
],
})
end
end

1
spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb

@ -35,6 +35,7 @@ RSpec.describe Form::Sales::Subsections::DiscountedOwnershipScheme, type: :model
discounted_ownership_deposit_value_check discounted_ownership_deposit_value_check
discounted_ownership_deposit_and_mortgage_value_check_after_deposit discounted_ownership_deposit_and_mortgage_value_check_after_deposit
leasehold_charges_discounted_ownership leasehold_charges_discounted_ownership
monthly_charges_discounted_ownership_value_check
], ],
) )
end end

1
spec/models/form/sales/subsections/outright_sale_spec.rb

@ -24,6 +24,7 @@ RSpec.describe Form::Sales::Subsections::OutrightSale, type: :model do
about_deposit_outright_sale about_deposit_outright_sale
outright_sale_deposit_value_check outright_sale_deposit_value_check
leasehold_charges_outright_sale leasehold_charges_outright_sale
monthly_charges_outright_sale_value_check
], ],
) )
end end

1
spec/models/form/sales/subsections/property_information_spec.rb

@ -16,6 +16,7 @@ RSpec.describe Form::Sales::Subsections::PropertyInformation, type: :model do
%w[ %w[
property_number_of_bedrooms property_number_of_bedrooms
property_unit_type property_unit_type
monthly_charges_property_type_value_check
property_building_type property_building_type
property_postcode property_postcode
property_local_authority property_local_authority

1
spec/models/form/sales/subsections/setup_spec.rb

@ -23,6 +23,7 @@ RSpec.describe Form::Sales::Subsections::Setup, type: :model do
discounted_ownership_type discounted_ownership_type
outright_ownership_type outright_ownership_type
ownership_type_old_persons_shared_ownership_value_check ownership_type_old_persons_shared_ownership_value_check
monthly_charges_type_value_check
buyer_company buyer_company
buyer_live buyer_live
joint_purchase joint_purchase

1
spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb

@ -42,6 +42,7 @@ RSpec.describe Form::Sales::Subsections::SharedOwnershipScheme, type: :model do
shared_ownership_deposit_value_check shared_ownership_deposit_value_check
monthly_rent monthly_rent
leasehold_charges_shared_ownership leasehold_charges_shared_ownership
monthly_charges_shared_ownership_value_check
], ],
) )
end end

4
spec/models/form_handler_spec.rb

@ -52,14 +52,14 @@ RSpec.describe FormHandler do
it "is able to load a current sales form" do it "is able to load a current sales form" do
form = form_handler.get_form("current_sales") form = form_handler.get_form("current_sales")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to eq(198) expect(form.pages.count).to eq(203)
expect(form.name).to eq("2022_2023_sales") expect(form.name).to eq("2022_2023_sales")
end end
it "is able to load a previous sales form" do it "is able to load a previous sales form" do
form = form_handler.get_form("previous_sales") form = form_handler.get_form("previous_sales")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to eq(198) expect(form.pages.count).to eq(203)
expect(form.name).to eq("2021_2022_sales") expect(form.name).to eq("2021_2022_sales")
end end
end end

4
spec/models/form_spec.rb

@ -218,9 +218,9 @@ RSpec.describe Form, type: :model do
expect(form.sections[0].class).to eq(Form::Sales::Sections::Setup) expect(form.sections[0].class).to eq(Form::Sales::Sections::Setup)
expect(form.subsections.count).to eq(1) expect(form.subsections.count).to eq(1)
expect(form.subsections.first.id).to eq("setup") expect(form.subsections.first.id).to eq("setup")
expect(form.pages.count).to eq(13) expect(form.pages.count).to eq(14)
expect(form.pages.first.id).to eq("organisation") expect(form.pages.first.id).to eq("organisation")
expect(form.questions.count).to eq(14) expect(form.questions.count).to eq(15)
expect(form.questions.first.id).to eq("owning_organisation_id") expect(form.questions.first.id).to eq("owning_organisation_id")
expect(form.start_date).to eq(Time.zone.parse("2022-04-01")) expect(form.start_date).to eq(Time.zone.parse("2022-04-01"))
expect(form.end_date).to eq(Time.zone.parse("2023-07-01")) expect(form.end_date).to eq(Time.zone.parse("2023-07-01"))

2
spec/models/sales_log_spec.rb

@ -47,7 +47,7 @@ RSpec.describe SalesLog, type: :model do
let(:sales_log) { build(:sales_log) } let(:sales_log) { build(:sales_log) }
it "returns optional fields" do it "returns optional fields" do
expect(sales_log.optional_fields).to eq(%w[purchid old_persons_shared_ownership_value_check]) expect(sales_log.optional_fields).to eq(%w[purchid monthly_charges_value_check old_persons_shared_ownership_value_check])
end end
end end

62
spec/models/validations/sales/soft_validations_spec.rb

@ -593,4 +593,66 @@ RSpec.describe Validations::Sales::SoftValidations do
expect(record).to be_staircase_bought_above_fifty expect(record).to be_staircase_bought_above_fifty
end end
end end
describe "#monthly_charges_over_soft_max?" do
it "returns false if mscharge is not given" do
record.mscharge = nil
record.proptype = 4
record.type = 2
expect(record).not_to be_monthly_charges_over_soft_max
end
it "returns false if proptype is not given" do
record.mscharge = 999
record.proptype = nil
record.type = 2
expect(record).not_to be_monthly_charges_over_soft_max
end
it "returns false if type is not given" do
record.mscharge = 999
record.proptype = 4
record.type = nil
expect(record).not_to be_monthly_charges_over_soft_max
end
context "with old persons shared ownership" do
it "returns false if the monthly charge is under 550" do
record.mscharge = 540
record.proptype = 4
record.type = 24
expect(record).not_to be_monthly_charges_over_soft_max
end
it "returns true if the monthly charge is over 550" do
record.mscharge = 999
record.proptype = 4
record.type = 24
expect(record).to be_monthly_charges_over_soft_max
end
end
context "with non old persons type of ownership" do
it "returns false if the monthly charge is under 300" do
record.mscharge = 280
record.proptype = 4
record.type = 18
expect(record).not_to be_monthly_charges_over_soft_max
end
it "returns true if the monthly charge is over 300" do
record.mscharge = 400
record.proptype = 4
record.type = 18
expect(record).to be_monthly_charges_over_soft_max
end
end
end
end end

Loading…
Cancel
Save