From 56a9cb4bdf9ed5f520d14ed676084399991c398c Mon Sep 17 00:00:00 2001 From: Jack <113976590+bibblobcode@users.noreply.github.com> Date: Thu, 26 Jan 2023 09:09:17 +0000 Subject: [PATCH] CLDC-864 Add contract exchange date validations (#1231) * CLDC-864 Add exdate validations * Raise missing translation errors * Fix missing translation * Remove unused method * Remove redundant check --- app/helpers/tab_nav_helper.rb | 7 -- .../sales/sale_information_validations.rb | 10 +++ config/environments/development.rb | 2 +- config/environments/test.rb | 2 +- config/locales/en.yml | 19 +++-- spec/helpers/tab_nav_helper_spec.rb | 22 ------ .../sale_information_validations_spec.rb | 76 +++++++++++++++++++ 7 files changed, 100 insertions(+), 38 deletions(-) diff --git a/app/helpers/tab_nav_helper.rb b/app/helpers/tab_nav_helper.rb index 801560b91..c29ae5d86 100644 --- a/app/helpers/tab_nav_helper.rb +++ b/app/helpers/tab_nav_helper.rb @@ -21,11 +21,4 @@ module TabNavHelper role = "#{user.role.to_s.humanize}" [user.organisation.name, role].join("\n") end - - def tab_items(user) - [ - { name: t("Details"), url: details_organisation_path(user.organisation) }, - { name: t("Users"), url: users_organisation_path(user.organisation) }, - ] - end end diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 7b0b32206..c70a8d57d 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -7,6 +7,16 @@ module Validations::Sales::SaleInformationValidations end end + def validate_exchange_date(record) + return unless record.exdate && record.saledate + + record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_before_saledate")) if record.exdate > record.saledate + + return if (record.saledate.to_date - record.exdate.to_date).to_i / 365 < 1 + + record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate")) + end + def validate_previous_property_unit_type(record) return unless record.fromprop && record.frombeds diff --git a/config/environments/development.rb b/config/environments/development.rb index 24155edfa..6261c29c8 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -73,7 +73,7 @@ Rails.application.configure do config.active_record.verbose_query_logs = true # Raises error for missing translations. - # config.i18n.raise_on_missing_translations = true + config.i18n.raise_on_missing_translations = true # Annotate rendered view with file names. # config.action_view.annotate_rendered_view_with_filenames = true diff --git a/config/environments/test.rb b/config/environments/test.rb index dca19f3d1..1c399b19d 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -57,7 +57,7 @@ Rails.application.configure do config.active_support.disallowed_deprecation_warnings = [] # Raises error for missing translations. - # config.i18n.raise_on_missing_translations = true + config.i18n.raise_on_missing_translations = true # Annotate rendered view with file names. # config.action_view.annotate_rendered_view_with_filenames = true diff --git a/config/locales/en.yml b/config/locales/en.yml index 68c7199de..cdad12a52 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -301,7 +301,7 @@ en: child_over_20: "Answer cannot be 20 or over as the relationship is ‘child’" not_student_16_19: "Answer cannot be between 16 and 19 as person %{person_num} is a child of the lead tenant but is not a full-time student" student_16_19: - cannot_be_16_19: + cannot_be_16_19: student_not_child: "Person cannot be aged 16-19 if they are a student but don't have relationship ‘child’" child_not_student: "Person cannot be aged 16-19 if they have relationship ‘child’ but are not a student" must_be_16_19: "Person must be aged 16-19 if they are a student and have relationship ‘child’" @@ -313,8 +313,8 @@ en: child_under_16: "Person’s %{person_num} working situation must be ’child under 16‘ as you told us they’re under 16" child_over_16: "Answer cannot be ‘child under 16’ as you told us the person %{person_num} is older than 16" not_student_16_19: "Person’s %{person_num} working situation must be full-time student or prefers not to say as you told us they’re between 16 and 19." - student_16_19: - cannot_be_student: + student_16_19: + cannot_be_student: child_not_16_19: "Person cannot be a student if they are not aged 16-19 but have relationship ‘child’" 16_19_not_child: "Person cannot be a student if they are aged 16-19 but don‘t have relationship ‘child’" must_be_student: "Person must be a student if they are aged 16-19 and have relationship ‘child’" @@ -326,8 +326,8 @@ en: child_over_20: "Answer cannot be ‘child’ if the person's age is 20 or over" one_partner: "Number of partners cannot be greater than 1" not_student_16_19: "Answer cannot be ‘child’ as you told us the person %{person_num} is between 16 and 19 and is not a full-time student" - student_16_19: - cannot_be_child: + student_16_19: + cannot_be_child: student_not_16_19: "Answer cannot be ‘child’ if the person is a student but not aged 16-19" 16_19_not_student: "Answer cannot be ‘child’ if the person is aged 16-19 but not a student" must_be_child: "Answer must be ‘child’ if the person is aged 16-19 and a student" @@ -413,9 +413,14 @@ en: deactivation: during_deactivated_period: "The location is already deactivated during this date, please enter a different date" sale_information: - previous_property_beds: + exdate: + must_be_before_saledate: + Contract exchange date must be less than 1 year before completion date + must_be_less_than_1_year_from_saledate: + Contract exchange date must be less than 1 year before completion date + previous_property_beds: property_type_bedsit: "Bedsit bedroom maximum 1" - previous_property_type: + previous_property_type: property_type_bedsit: "A bedsit can not have more than 1 bedroom" soft_validations: diff --git a/spec/helpers/tab_nav_helper_spec.rb b/spec/helpers/tab_nav_helper_spec.rb index b2273aa11..7b4efc5eb 100644 --- a/spec/helpers/tab_nav_helper_spec.rb +++ b/spec/helpers/tab_nav_helper_spec.rb @@ -34,26 +34,4 @@ RSpec.describe TabNavHelper do expect(scheme_cell(scheme)).to match(expected_html) end end - - describe "#tab_items" do - context "when user is a data_coordinator" do - let(:user) { FactoryBot.build(:user, :data_coordinator, organisation:) } - - it "returns details and user tabs" do - result = tab_items(user).map { |i| i[:name] } - expect(result.count).to eq(2) - expect(result.first).to match("Details") - expect(result.second).to match("Users") - end - end - - context "when user is a data_provider" do - it "returns details and user tabs" do - result = tab_items(user).map { |i| i[:name] } - expect(result.count).to eq(2) - expect(result.first).to match("Details") - expect(result.second).to match("Users") - end - end - end end diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 052e171be..409972040 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -67,6 +67,82 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end + describe "#validate_exchange_date" do + context "when exdate blank" do + let(:record) { build(:sales_log, exdate: nil) } + + it "does not add an error" do + sale_information_validator.validate_exchange_date(record) + + expect(record.errors[:exdate]).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_exchange_date(record) + + expect(record.errors[:exdate]).not_to be_present + end + end + + context "when saledate and exdate blank" do + let(:record) { build(:sales_log, exdate: nil, saledate: nil) } + + it "does not add an error" do + sale_information_validator.validate_exchange_date(record) + + expect(record.errors[:exdate]).not_to be_present + end + end + + context "when exdate before saledate" do + let(:record) { build(:sales_log, exdate: 2.months.ago, saledate: 1.month.ago) } + + it "does not add the error" do + sale_information_validator.validate_exchange_date(record) + + expect(record.errors[:exdate]).not_to be_present + end + end + + context "when exdate more than 1 year before saledate" do + let(:record) { build(:sales_log, exdate: 2.years.ago, saledate: 1.month.ago) } + + it "does not add the error" do + sale_information_validator.validate_exchange_date(record) + + expect(record.errors[:exdate]).to eq( + ["Contract exchange date must be less than 1 year before completion date"], + ) + end + end + + context "when exdate after saledate" do + let(:record) { build(:sales_log, exdate: 1.month.ago, saledate: 2.months.ago) } + + it "adds error" do + sale_information_validator.validate_exchange_date(record) + + expect(record.errors[:exdate]).to eq( + ["Contract exchange date must be less than 1 year before completion date"], + ) + end + end + + context "when exdate == saledate" do + let(:record) { build(:sales_log, exdate: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) } + + it "does not add an error" do + sale_information_validator.validate_exchange_date(record) + + expect(record.errors[:exdate]).not_to be_present + end + end + end + describe "#validate_previous_property_unit_type" do context "when number of bedrooms is <= 1" do let(:record) { FactoryBot.build(:sales_log, frombeds: 1, fromprop: 2) }