From 6bb0005e7db146a4ba6aa71c1949374b2e9b82f2 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Fri, 14 Apr 2023 10:42:42 +0100 Subject: [PATCH] CLDC-2023 Add livein soft validation (#1521) * Add buyer_livein_value_check column * Add validation form elements * Add validations * Add error messageand update import * Setup fixes * Typos * Add missing translation * Rename soft validation methods * Downcase ownership scheme --- .../sales/pages/buyer_live_in_value_check.rb | 21 ++ .../questions/buyer_live_in_value_check.rb | 24 +++ .../subsections/household_characteristics.rb | 2 + app/models/form/sales/subsections/setup.rb | 2 + app/models/sales_log.rb | 13 +- .../validations/sales/soft_validations.rb | 13 ++ .../imports/sales_logs_import_service.rb | 4 +- config/locales/en.yml | 4 + ...30405140343_add_buyer_livin_value_check.rb | 5 + db/schema.rb | 1 + .../pages/buyer_live_in_value_check_spec.rb | 64 ++++++ .../buyer_live_in_value_check_spec.rb | 62 ++++++ .../household_characteristics_spec.rb | 4 + .../form/sales/subsections/setup_spec.rb | 2 + spec/models/form_spec.rb | 4 +- spec/models/sales_log_spec.rb | 2 + .../sales/soft_validations_spec.rb | 183 ++++++++++++++++++ 17 files changed, 406 insertions(+), 4 deletions(-) create mode 100644 app/models/form/sales/pages/buyer_live_in_value_check.rb create mode 100644 app/models/form/sales/questions/buyer_live_in_value_check.rb create mode 100644 db/migrate/20230405140343_add_buyer_livin_value_check.rb create mode 100644 spec/models/form/sales/pages/buyer_live_in_value_check_spec.rb create mode 100644 spec/models/form/sales/questions/buyer_live_in_value_check_spec.rb diff --git a/app/models/form/sales/pages/buyer_live_in_value_check.rb b/app/models/form/sales/pages/buyer_live_in_value_check.rb new file mode 100644 index 000000000..067ba96ca --- /dev/null +++ b/app/models/form/sales/pages/buyer_live_in_value_check.rb @@ -0,0 +1,21 @@ +class Form::Sales::Pages::BuyerLiveInValueCheck < Form::Sales::Pages::Person + def initialize(id, hsh, subsection, person_index:) + super + @depends_on = [ + { + "buyer#{person_index}_livein_wrong_for_ownership_type?" => true, + }, + ] + @title_text = { + "translation" => "soft_validations.buyer#{person_index}_livein_wrong_for_ownership_type.title_text", + "arguments" => [{ "key" => "ownership_scheme", "label" => false, "i18n_template" => "ownership_scheme" }], + } + @informative_text = {} + end + + def questions + @questions ||= [ + Form::Sales::Questions::BuyerLiveInValueCheck.new(nil, nil, self, person_index: @person_index), + ] + end +end diff --git a/app/models/form/sales/questions/buyer_live_in_value_check.rb b/app/models/form/sales/questions/buyer_live_in_value_check.rb new file mode 100644 index 000000000..a4209c38c --- /dev/null +++ b/app/models/form/sales/questions/buyer_live_in_value_check.rb @@ -0,0 +1,24 @@ +class Form::Sales::Questions::BuyerLiveInValueCheck < ::Form::Question + def initialize(id, hsh, page, person_index:) + super(id, hsh, page) + @id = "buyer_livein_value_check" + @check_answer_label = "Buyer live in confirmation" + @type = "interruption_screen" + @answer_options = { + "0" => { "value" => "Yes" }, + "1" => { "value" => "No" }, + } + @hidden_in_check_answers = { + "depends_on" => [ + { + "buyer_livein_value_check" => 0, + }, + { + "buyer_livein_value_check" => 1, + }, + ], + } + @check_answers_card_number = person_index + @header = "Are you sure this is correct?" + end +end diff --git a/app/models/form/sales/subsections/household_characteristics.rb b/app/models/form/sales/subsections/household_characteristics.rb index 922d95edd..64b0b95ea 100644 --- a/app/models/form/sales/subsections/household_characteristics.rb +++ b/app/models/form/sales/subsections/household_characteristics.rb @@ -26,6 +26,7 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection Form::Sales::Pages::RetirementValueCheck.new("working_situation_1_retirement_value_check", nil, self, person_index: 1), Form::Sales::Pages::Buyer1IncomeValueCheck.new("working_situation_buyer_1_income_value_check", nil, self), Form::Sales::Pages::Buyer1LiveInProperty.new(nil, nil, self), + Form::Sales::Pages::BuyerLiveInValueCheck.new("buyer_1_live_in_property_value_check", nil, self, person_index: 1), Form::Sales::Pages::Buyer2RelationshipToBuyer1.new(nil, nil, self), Form::Sales::Pages::PersonStudentNotChildValueCheck.new("buyer_2_relationship_student_not_child_value_check", nil, self, person_index: 2), Form::Sales::Pages::Age2.new(nil, nil, self), @@ -40,6 +41,7 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection Form::Sales::Pages::Buyer2IncomeValueCheck.new("working_situation_buyer_2_income_value_check", nil, self), Form::Sales::Pages::PersonStudentNotChildValueCheck.new("buyer_2_working_situation_student_not_child_value_check", nil, self, person_index: 2), Form::Sales::Pages::Buyer2LiveInProperty.new(nil, nil, self), + Form::Sales::Pages::BuyerLiveInValueCheck.new("buyer_2_live_in_property_value_check", nil, self, person_index: 2), Form::Sales::Pages::NumberOfOthersInProperty.new("number_of_others_in_property", nil, self, joint_purchase: false), Form::Sales::Pages::NumberOfOthersInProperty.new("number_of_others_in_property_joint_purchase", nil, self, joint_purchase: true), Form::Sales::Pages::PersonKnown.new("person_2_known", nil, self, person_index: 2), diff --git a/app/models/form/sales/subsections/setup.rb b/app/models/form/sales/subsections/setup.rb index d1bb9dd6b..c723b732c 100644 --- a/app/models/form/sales/subsections/setup.rb +++ b/app/models/form/sales/subsections/setup.rb @@ -19,6 +19,8 @@ class Form::Sales::Subsections::Setup < ::Form::Subsection 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::DiscountedSaleValueCheck.new("discounted_sale_type_value_check", nil, self), + Form::Sales::Pages::BuyerLiveInValueCheck.new("buyer_1_live_in_property_type_value_check", nil, self, person_index: 1), + Form::Sales::Pages::BuyerLiveInValueCheck.new("buyer_2_live_in_property_type_value_check", nil, self, person_index: 2), Form::Sales::Pages::BuyerCompany.new(nil, nil, self), Form::Sales::Pages::BuyerLive.new(nil, nil, self), Form::Sales::Pages::JointPurchase.new(nil, nil, self), diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index a47edea0f..ebb3ef4e3 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -43,7 +43,7 @@ class SalesLog < Log } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) } - OPTIONAL_FIELDS = %w[saledate_check purchid monthly_charges_value_check old_persons_shared_ownership_value_check othtype discounted_sale_value_check].freeze + OPTIONAL_FIELDS = %w[saledate_check purchid monthly_charges_value_check old_persons_shared_ownership_value_check othtype discounted_sale_value_check buyer_livein_value_check].freeze RETIREMENT_AGES = { "M" => 65, "F" => 60, "X" => 65 }.freeze def lettings? @@ -345,4 +345,15 @@ class SalesLog < Log def beds_for_la_sale_range beds.nil? ? nil : [beds, LaSaleRange::MAX_BEDS].min end + + def ownership_scheme + case ownershipsch + when 1 + "shared ownership" + when 2 + "discounted ownership" + when 3 + "outright sale" + end + end end diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb index d72118d05..354bc7fd7 100644 --- a/app/models/validations/sales/soft_validations.rb +++ b/app/models/validations/sales/soft_validations.rb @@ -123,6 +123,19 @@ module Validations::Sales::SoftValidations mortgage_deposit_and_grant_total != value_with_discount && discounted_ownership_sale? end + def buyer1_livein_wrong_for_ownership_type? + return unless ownershipsch && buy1livein + + (discounted_ownership_sale? || shared_ownership_scheme?) && buy1livein == 2 + end + + def buyer2_livein_wrong_for_ownership_type? + return unless ownershipsch && buy2livein + return unless joint_purchase? + + (discounted_ownership_sale? || shared_ownership_scheme?) && buy2livein == 2 + end + private def sale_range diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index 2fb619619..e02d7d23c 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -155,6 +155,7 @@ module Imports attributes["monthly_charges_value_check"] = 0 attributes["student_not_child_value_check"] = 0 attributes["discounted_sale_value_check"] = 0 + attributes["buyer_livein_value_check"] = 0 # Sets the log creator owner_id = meta_field_value(xml_doc, "owner-user-id").strip @@ -278,7 +279,8 @@ module Imports hodate_check saledate_check student_not_child_value_check - discounted_sale_value_check] + discounted_sale_value_check + buyer_livein_value_check] end def check_status_completed(sales_log, previous_status) diff --git a/config/locales/en.yml b/config/locales/en.yml index dc36f5f64..d333accd8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -551,6 +551,10 @@ en: informative_text: "Your given mortgage, deposit and grant total is %{mortgage_deposit_and_grant_total}" care_home_charges: title_text: "Care home charges should be provided if this is a care home accommodation" + buyer1_livein_wrong_for_ownership_type: + title_text: "You told us that buyer 1 will not live in the property. For %{ownership_scheme} types, the buyer usually lives in the property." + buyer2_livein_wrong_for_ownership_type: + title_text: "You told us that buyer 2 will not live in the property. For %{ownership_scheme} types, the buyer usually lives in the property." devise: two_factor_authentication: diff --git a/db/migrate/20230405140343_add_buyer_livin_value_check.rb b/db/migrate/20230405140343_add_buyer_livin_value_check.rb new file mode 100644 index 000000000..feafc610b --- /dev/null +++ b/db/migrate/20230405140343_add_buyer_livin_value_check.rb @@ -0,0 +1,5 @@ +class AddBuyerLivinValueCheck < ActiveRecord::Migration[7.0] + def change + add_column :sales_logs, :buyer_livein_value_check, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 77ad1f3ea..8a14eed1d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -573,6 +573,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_12_143245) do t.string "county" t.integer "discounted_sale_value_check" t.integer "student_not_child_value_check" + t.integer "buyer_livein_value_check" 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 ["old_id"], name: "index_sales_logs_on_old_id", unique: true diff --git a/spec/models/form/sales/pages/buyer_live_in_value_check_spec.rb b/spec/models/form/sales/pages/buyer_live_in_value_check_spec.rb new file mode 100644 index 000000000..9d00b086b --- /dev/null +++ b/spec/models/form/sales/pages/buyer_live_in_value_check_spec.rb @@ -0,0 +1,64 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::BuyerLiveInValueCheck, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection, person_index:) } + + let(:page_id) { "buyer_1_live_in_value_check" } + let(:page_definition) { nil } + let(:person_index) { 1 } + 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[buyer_livein_value_check]) + end + + it "has the correct id" do + expect(page.id).to eq("buyer_1_live_in_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([ + { + "buyer1_livein_wrong_for_ownership_type?" => true, + }, + ]) + end + + it "is interruption screen page" do + expect(page.interruption_screen?).to eq(true) + end + + it "has the correct title_text" do + expect(page.title_text).to eq({ + "translation" => "soft_validations.buyer1_livein_wrong_for_ownership_type.title_text", + "arguments" => [{ "key" => "ownership_scheme", "label" => false, "i18n_template" => "ownership_scheme" }], + }) + end + + context "with buyer 2" do + let(:person_index) { 2 } + + it "has correct depends_on" do + expect(page.depends_on).to eq([ + { + "buyer2_livein_wrong_for_ownership_type?" => true, + }, + ]) + end + + it "has the correct title_text" do + expect(page.title_text).to eq({ + "translation" => "soft_validations.buyer2_livein_wrong_for_ownership_type.title_text", + "arguments" => [{ "key" => "ownership_scheme", "label" => false, "i18n_template" => "ownership_scheme" }], + }) + end + end +end diff --git a/spec/models/form/sales/questions/buyer_live_in_value_check_spec.rb b/spec/models/form/sales/questions/buyer_live_in_value_check_spec.rb new file mode 100644 index 000000000..0409f4f2e --- /dev/null +++ b/spec/models/form/sales/questions/buyer_live_in_value_check_spec.rb @@ -0,0 +1,62 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::BuyerLiveInValueCheck, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page, person_index:) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:person_index) { 1 } + 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("buyer_livein_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("Buyer live in 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 a correct check_answers_card_number" do + expect(question.check_answers_card_number).to eq(1) + 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" => [ + { + "buyer_livein_value_check" => 0, + }, + { + "buyer_livein_value_check" => 1, + }, + ], + }) + end +end diff --git a/spec/models/form/sales/subsections/household_characteristics_spec.rb b/spec/models/form/sales/subsections/household_characteristics_spec.rb index 57a9ce019..c926baa08 100644 --- a/spec/models/form/sales/subsections/household_characteristics_spec.rb +++ b/spec/models/form/sales/subsections/household_characteristics_spec.rb @@ -39,6 +39,7 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model working_situation_1_retirement_value_check working_situation_buyer_1_income_value_check buyer_1_live_in_property + buyer_1_live_in_property_value_check buyer_2_relationship_to_buyer_1 buyer_2_relationship_student_not_child_value_check buyer_2_age @@ -52,6 +53,7 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model working_situation_buyer_2_income_value_check buyer_2_working_situation_student_not_child_value_check buyer_2_live_in_property + buyer_2_live_in_property_value_check number_of_others_in_property number_of_others_in_property_joint_purchase person_2_known @@ -141,6 +143,7 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model working_situation_1_retirement_value_check working_situation_buyer_1_income_value_check buyer_1_live_in_property + buyer_1_live_in_property_value_check buyer_2_relationship_to_buyer_1 buyer_2_relationship_student_not_child_value_check buyer_2_age @@ -161,6 +164,7 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model working_situation_buyer_2_income_value_check buyer_2_working_situation_student_not_child_value_check buyer_2_live_in_property + buyer_2_live_in_property_value_check number_of_others_in_property number_of_others_in_property_joint_purchase person_2_known diff --git a/spec/models/form/sales/subsections/setup_spec.rb b/spec/models/form/sales/subsections/setup_spec.rb index 92271ba25..7f12a8498 100644 --- a/spec/models/form/sales/subsections/setup_spec.rb +++ b/spec/models/form/sales/subsections/setup_spec.rb @@ -26,6 +26,8 @@ RSpec.describe Form::Sales::Subsections::Setup, type: :model do ownership_type_old_persons_shared_ownership_value_check monthly_charges_type_value_check discounted_sale_type_value_check + buyer_1_live_in_property_type_value_check + buyer_2_live_in_property_type_value_check buyer_company buyer_live joint_purchase diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index a1cf650c0..327a79a2f 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -247,9 +247,9 @@ RSpec.describe Form, type: :model do expect(form.sections[0].class).to eq(Form::Sales::Sections::Setup) expect(form.subsections.count).to eq(1) expect(form.subsections.first.id).to eq("setup") - expect(form.pages.count).to eq(16) + expect(form.pages.count).to eq(18) expect(form.pages.first.id).to eq("organisation") - expect(form.questions.count).to eq(17) + expect(form.questions.count).to eq(19) expect(form.questions.first.id).to eq("owning_organisation_id") expect(form.start_date).to eq(Time.zone.parse("2022-04-01")) expect(form.end_date).to eq(Time.zone.parse("2023-06-07")) diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index ca72794a5..e9acd4548 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -61,6 +61,7 @@ RSpec.describe SalesLog, type: :model do old_persons_shared_ownership_value_check othtype discounted_sale_value_check + buyer_livein_value_check proplen mortlen frombeds @@ -79,6 +80,7 @@ RSpec.describe SalesLog, type: :model do old_persons_shared_ownership_value_check othtype discounted_sale_value_check + buyer_livein_value_check address_line2 county postcode_full diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index 712e53965..ae20e1111 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -913,4 +913,187 @@ RSpec.describe Validations::Sales::SoftValidations do end end end + + describe "#buyer1_livein_wrong_for_ownership_type?" do + context "when it's a shared ownership" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 1) } + + context "and buy1livein is no" do + before do + record.buy1livein = 2 + end + + it "returns true" do + expect(record).to be_buyer1_livein_wrong_for_ownership_type + end + end + + context "and buy1livein is yes" do + before do + record.buy1livein = 1 + end + + it "returns false" do + expect(record).not_to be_buyer1_livein_wrong_for_ownership_type + end + end + end + + context "when it's a discounted ownership" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 2) } + + context "and buy1livein is no" do + before do + record.buy1livein = 2 + end + + it "returns true" do + expect(record).to be_buyer1_livein_wrong_for_ownership_type + end + end + + context "and buy1livein is yes" do + before do + record.buy1livein = 1 + end + + it "returns false" do + expect(record).not_to be_buyer1_livein_wrong_for_ownership_type + end + end + end + + context "when it's a outright sale" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 3) } + + context "and buy1livein is no" do + before do + record.buy1livein = 2 + end + + it "returns false" do + expect(record).not_to be_buyer1_livein_wrong_for_ownership_type + end + end + + context "and buy1livein is yes" do + before do + record.buy1livein = 1 + end + + it "returns false" do + expect(record).not_to be_buyer1_livein_wrong_for_ownership_type + end + end + end + + context "when ownership is not given" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 3) } + + before do + record.ownershipsch = nil + end + + it "returns false" do + expect(record).not_to be_buyer1_livein_wrong_for_ownership_type + end + end + end + + describe "#buyer2_livein_wrong_for_ownership_type?" do + context "when it's a shared ownership" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 1, jointpur: 1) } + + context "and buy2livein is no" do + before do + record.buy2livein = 2 + end + + it "returns true" do + expect(record).to be_buyer2_livein_wrong_for_ownership_type + end + end + + context "and buy2livein is yes" do + before do + record.buy2livein = 1 + end + + it "returns false" do + expect(record).not_to be_buyer2_livein_wrong_for_ownership_type + end + end + + context "and not a joint purchase" do + before do + record.buy2livein = 2 + record.jointpur = 2 + end + + it "returns false" do + expect(record).not_to be_buyer2_livein_wrong_for_ownership_type + end + end + end + + context "when it's a discounted ownership" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 2, jointpur: 1) } + + context "and buy2livein is no" do + before do + record.buy2livein = 2 + end + + it "returns true" do + expect(record).to be_buyer2_livein_wrong_for_ownership_type + end + end + + context "and buy2livein is yes" do + before do + record.buy2livein = 1 + end + + it "returns false" do + expect(record).not_to be_buyer2_livein_wrong_for_ownership_type + end + end + end + + context "when it's a outright sale" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 3, jointpur: 1) } + + context "and buy2livein is no" do + before do + record.buy2livein = 2 + end + + it "returns false" do + expect(record).not_to be_buyer2_livein_wrong_for_ownership_type + end + end + + context "and buy2livein is yes" do + before do + record.buy2livein = 1 + end + + it "returns false" do + expect(record).not_to be_buyer2_livein_wrong_for_ownership_type + end + end + end + + context "when ownership is not given" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 3, jointpur: 1) } + + before do + record.ownershipsch = nil + end + + it "returns false" do + expect(record).not_to be_buyer2_livein_wrong_for_ownership_type + end + end + end end