Browse Source

CLDC-863 Add practical completion/handover date validations (#1198)

* [CLDC-863] Add hard validation for hodate

* Add soft validation
pull/1209/head v0.2.34
Jack 2 years ago committed by GitHub
parent
commit
d4e8e64c28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      app/models/form/sales/pages/handover_date_check.rb
  2. 23
      app/models/form/sales/questions/handover_date_check.rb
  3. 1
      app/models/form/sales/subsections/shared_ownership_scheme.rb
  4. 1
      app/models/sales_log.rb
  5. 8
      app/models/validations/sales/sale_information_validations.rb
  6. 6
      app/models/validations/sales/soft_validations.rb
  7. 7
      db/migrate/20230116151942_add_ho_date_check.rb
  8. 3
      db/schema.rb
  9. 33
      spec/models/form/sales/pages/handover_date_check_spec.rb
  10. 48
      spec/models/form/sales/questions/handover_date_check_spec.rb
  11. 1
      spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb
  12. 4
      spec/models/form_handler_spec.rb
  13. 62
      spec/models/validations/sales/sale_information_validations_spec.rb
  14. 39
      spec/models/validations/sales/soft_validations_spec.rb

13
app/models/form/sales/pages/handover_date_check.rb

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

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

@ -0,0 +1,23 @@
class Form::Sales::Questions::HandoverDateCheck < ::Form::Question
def initialize(id, hsh, page)
super
@id = "hodate_check"
@check_answer_label = "Practical completion or handover date check"
@header = "Are you sure practical completion or handover date is more than 3 years before exchange date?"
@type = "interruption_screen"
@answer_options = {
"0" => { "value" => "Yes" },
"1" => { "value" => "No" },
}
@hidden_in_check_answers = {
"depends_on" => [
{
"hodate_check" => 0,
},
{
"hodate_check" => 1,
},
],
}
end
end

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

@ -14,6 +14,7 @@ class Form::Sales::Subsections::SharedOwnershipScheme < ::Form::Subsection
Form::Sales::Pages::Resale.new(nil, nil, self), Form::Sales::Pages::Resale.new(nil, nil, self),
Form::Sales::Pages::ExchangeDate.new(nil, nil, self), Form::Sales::Pages::ExchangeDate.new(nil, nil, self),
Form::Sales::Pages::HandoverDate.new(nil, nil, self), Form::Sales::Pages::HandoverDate.new(nil, nil, self),
Form::Sales::Pages::HandoverDateCheck.new("handover_date_check", nil, self),
Form::Sales::Pages::LaNominations.new(nil, nil, self), Form::Sales::Pages::LaNominations.new(nil, nil, self),
Form::Sales::Pages::BuyerPrevious.new(nil, nil, self), Form::Sales::Pages::BuyerPrevious.new(nil, nil, self),
Form::Sales::Pages::PreviousBedrooms.new(nil, nil, self), Form::Sales::Pages::PreviousBedrooms.new(nil, nil, self),

1
app/models/sales_log.rb

@ -2,7 +2,6 @@ class SalesLogValidator < ActiveModel::Validator
include Validations::Sales::HouseholdValidations include Validations::Sales::HouseholdValidations
include Validations::Sales::FinancialValidations include Validations::Sales::FinancialValidations
include Validations::Sales::SaleInformationValidations include Validations::Sales::SaleInformationValidations
include Validations::SharedValidations include Validations::SharedValidations
include Validations::LocalAuthorityValidations include Validations::LocalAuthorityValidations

8
app/models/validations/sales/sale_information_validations.rb

@ -6,4 +6,12 @@ module Validations::Sales::SaleInformationValidations
record.errors.add :deposit, "Cash deposit must be £0 - £999,999" record.errors.add :deposit, "Cash deposit must be £0 - £999,999"
end end
end end
def validate_pratical_completion_date_before_saledate(record)
return if record.saledate.blank? || record.hodate.blank?
unless record.saledate > record.hodate
record.errors.add :hodate, "Practical completion or handover date must be before exchange date"
end
end
end end

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

@ -36,4 +36,10 @@ module Validations::Sales::SoftValidations
deposit > savings * 4 / 3 deposit > savings * 4 / 3
end end
def hodate_3_years_or_more_saledate?
return unless hodate && saledate
((saledate.to_date - hodate.to_date).to_i / 365) >= 3
end
end end

7
db/migrate/20230116151942_add_ho_date_check.rb

@ -0,0 +1,7 @@
class AddHoDateCheck < ActiveRecord::Migration[7.0]
def change
change_table :sales_logs, bulk: true do |t|
t.column :hodate_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_13_125117) do ActiveRecord::Schema[7.0].define(version: 2023_01_16_151942) 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"
@ -498,6 +498,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_13_125117) do
t.integer "pcodenk" t.integer "pcodenk"
t.string "postcode_full" t.string "postcode_full"
t.boolean "is_la_inferred" t.boolean "is_la_inferred"
t.integer "hodate_check"
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 ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_id" t.index ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_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"

33
spec/models/form/sales/pages/handover_date_check_spec.rb

@ -0,0 +1,33 @@
require "rails_helper"
RSpec.describe Form::Sales::Pages::HandoverDateCheck, type: :model do
subject(:page) { described_class.new(page_id, page_definition, subsection) }
let(:page_id) { "" }
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[hodate_check])
end
it "has the correct id" do
expect(page.id).to eq("")
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([
{
"hodate_3_years_or_more_saledate?" => true,
},
])
end
end

48
spec/models/form/sales/questions/handover_date_check_spec.rb

@ -0,0 +1,48 @@
require "rails_helper"
RSpec.describe Form::Sales::Questions::HandoverDateCheck, type: :model do
subject(:question) { described_class.new(question_id, question_definition, page) }
let(:question_id) { nil }
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("hodate_check")
end
it "has the correct header" do
expect(question.header).to eq("Are you sure practical completion or handover date is more than 3 years before exchange date?")
end
it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Practical completion or handover date check")
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" => [{ "hodate_check" => 0 }, { "hodate_check" => 1 }] })
end
end

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

@ -20,6 +20,7 @@ RSpec.describe Form::Sales::Subsections::SharedOwnershipScheme, type: :model do
resale resale
exchange_contracts exchange_contracts
handover_date handover_date
handover_date_check
la_nominations la_nominations
buyer_previous buyer_previous
previous_bedrooms previous_bedrooms

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(148) expect(form.pages.count).to eq(149)
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(148) expect(form.pages.count).to eq(149)
expect(form.name).to eq("2021_2022_sales") expect(form.name).to eq("2021_2022_sales")
end end
end end

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

@ -46,4 +46,66 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
end end
end end
describe "#validate_pratical_completion_date_before_saledate" do
context "when hodate blank" do
let(:record) { build(:sales_log, hodate: nil) }
it "does not add an error" do
sale_information_validator.validate_pratical_completion_date_before_saledate(record)
expect(record.errors).not_to be_present
end
end
context "when saledate blank" do
let(:record) { build(:sales_log, saledate: nil) }
it "does not add an error" do
sale_information_validator.validate_pratical_completion_date_before_saledate(record)
expect(record.errors).not_to be_present
end
end
context "when saledate and hodate blank" do
let(:record) { build(:sales_log, hodate: nil, saledate: nil) }
it "does not add an error" do
sale_information_validator.validate_pratical_completion_date_before_saledate(record)
expect(record.errors).not_to be_present
end
end
context "when hodate before saledate" do
let(:record) { build(:sales_log, hodate: 2.months.ago, saledate: 1.month.ago) }
it "does not add the error" do
sale_information_validator.validate_pratical_completion_date_before_saledate(record)
expect(record.errors).not_to be_present
end
end
context "when hodate after saledate" do
let(:record) { build(:sales_log, hodate: 1.month.ago, saledate: 2.months.ago) }
it "adds error" do
sale_information_validator.validate_pratical_completion_date_before_saledate(record)
expect(record.errors[:hodate]).to be_present
end
end
context "when hodate == saledate" do
let(:record) { build(:sales_log, hodate: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) }
it "does not add an error" do
sale_information_validator.validate_pratical_completion_date_before_saledate(record)
expect(record.errors[:hodate]).to be_present
end
end
end
end end

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

@ -1,7 +1,7 @@
require "rails_helper" require "rails_helper"
RSpec.describe Validations::Sales::SoftValidations do RSpec.describe Validations::Sales::SoftValidations do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { create(:sales_log) }
describe "income1 min validations" do describe "income1 min validations" do
context "when validating soft min" do context "when validating soft min" do
@ -255,4 +255,41 @@ RSpec.describe Validations::Sales::SoftValidations do
end end
end end
end end
describe "hodate_more_than_3_years_before_saledate" do
it "when hodate not set" do
record.saledate = Time.zone.now
record.hodate = nil
expect(record).not_to be_hodate_3_years_or_more_saledate
end
it "when saledate not set" do
record.saledate = nil
record.hodate = Time.zone.now
expect(record).not_to be_hodate_3_years_or_more_saledate
end
it "when saledate and hodate not set" do
record.saledate = nil
record.hodate = nil
expect(record).not_to be_hodate_3_years_or_more_saledate
end
it "when 3 years or more before saledate" do
record.saledate = Time.zone.now
record.hodate = record.saledate - 4.years
expect(record).to be_hodate_3_years_or_more_saledate
end
it "when less than 3 years before saledate" do
record.saledate = Time.zone.now
record.hodate = 2.months.ago
expect(record).not_to be_hodate_3_years_or_more_saledate
end
end
end end

Loading…
Cancel
Save