From 65b1533aba74252f919747b8be0fe8d8cd8a7173 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Date: Mon, 30 Jan 2023 13:33:43 +0000 Subject: [PATCH] CLDC-1464 Add completion date validations (#1232) * feat: add 22/23 year validation * feat: wip commit * feat: update i18n * feat: add one year gap exchange/completion validations * tests: add tests for new validations * test: add setup validations tests * test: update factory to pass saledate validation * feat: update seeds * feat: update tests to reflect sales logs shouldn't exist in new service before 2022 * feat: merge with main, improve date behaviour * refactor: cleanup * feat: update tests * feat: enforce saledate not after 22/23 collection year end --- app/models/sales_log.rb | 1 + .../sales/sale_information_validations.rb | 14 ++++-- .../validations/sales/setup_validations.rb | 9 ++++ config/locales/en.yml | 15 +++--- db/seeds.rb | 6 +-- spec/factories/sales_log.rb | 8 +-- .../sale_information_validations_spec.rb | 8 ++- .../sales/setup_validations_spec.rb | 49 +++++++++++++++++++ spec/requests/sales_logs_controller_spec.rb | 26 +++++----- 9 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 app/models/validations/sales/setup_validations.rb create mode 100644 spec/models/validations/sales/setup_validations_spec.rb diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 5d97df33e..7a57be8ee 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -1,4 +1,5 @@ class SalesLogValidator < ActiveModel::Validator + include Validations::Sales::SetupValidations include Validations::Sales::HouseholdValidations include Validations::Sales::PropertyValidations include Validations::SharedValidations diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 53d76fd48..24fdff3ea 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -3,7 +3,7 @@ module Validations::Sales::SaleInformationValidations 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" + record.errors.add :hodate, I18n.t("validations.sale_information.hodate.must_be_before_exdate") end end @@ -23,11 +23,15 @@ module Validations::Sales::SaleInformationValidations 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 + if record.exdate > record.saledate + record.errors.add :exdate, I18n.t("validations.sale_information.exdate.must_be_before_saledate") + record.errors.add :saledate, I18n.t("validations.sale_information.saledate.must_be_after_exdate") + end - record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate")) + if record.saledate - record.exdate >= 1.year + record.errors.add :exdate, I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate") + record.errors.add :saledate, I18n.t("validations.sale_information.saledate.must_be_less_than_1_year_from_exdate") + end end def validate_previous_property_unit_type(record) diff --git a/app/models/validations/sales/setup_validations.rb b/app/models/validations/sales/setup_validations.rb new file mode 100644 index 000000000..0e7a759ee --- /dev/null +++ b/app/models/validations/sales/setup_validations.rb @@ -0,0 +1,9 @@ +module Validations::Sales::SetupValidations + def validate_saledate(record) + return unless record.saledate + + unless Time.zone.local(2022, 4, 1) <= record.saledate && record.saledate < Time.zone.local(2023, 4, 1) + record.errors.add :saledate, I18n.t("validations.setup.saledate.financial_year") + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 445dfc09f..1ec13a304 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -147,6 +147,8 @@ en: setup: intermediate_rent_product_name: blank: "Enter name of other intermediate rent product" + saledate: + financial_year: "Date must be from 22/23 financial year" startdate: later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date" before_scheme_end_date: "The tenancy start date must be before the end date for this supported housing scheme" @@ -415,15 +417,16 @@ en: proplen: social_homebuy: "Social HomeBuy buyers should not have lived here before" rent_to_buy: "Rent to Buy buyers should not have lived here before" + hodate: + must_be_before_exdate: "Practical completion or handover date must be before exchange date" 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 + 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" + saledate: + must_be_after_exdate: "Completion date must be less than 1 year after contract exchange date" + must_be_less_than_1_year_from_exdate: "Completion date must be less than 1 year after contract exchange date" previous_property_beds: property_type_bedsit: "Bedsit bedroom maximum 1" - previous_property_type: - property_type_bedsit: "A bedsit can not have more than 1 bedroom" discounted_ownership_value: "Mortgage, deposit, and grant total must equal £%{value_with_discount}" monthly_rent: higher_than_expected: "Basic monthly rent must be between £0 and £9,999" diff --git a/db/seeds.rb b/db/seeds.rb index 6a4098f9b..15d84a38d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -148,7 +148,7 @@ unless Rails.env.test? if (Rails.env.development? || Rails.env.review?) && SalesLog.count.zero? SalesLog.find_or_create_by!( - saledate: Date.new(1, 1, 1), + saledate: Date.new(2023, 1, 1), purchid: "1", ownershipsch: 1, type: 2, @@ -157,7 +157,7 @@ unless Rails.env.test? ) SalesLog.find_or_create_by!( - saledate: Date.new(1, 1, 1), + saledate: Date.new(2023, 1, 1), purchid: "1", ownershipsch: 2, type: 9, @@ -166,7 +166,7 @@ unless Rails.env.test? ) SalesLog.find_or_create_by!( - saledate: Date.new(1, 1, 1), + saledate: Date.new(2023, 1, 1), purchid: "1", ownershipsch: 3, type: 10, diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index f83d102d3..085d4ca03 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -2,18 +2,18 @@ FactoryBot.define do factory :sales_log do created_by { FactoryBot.create(:user) } owning_organisation { created_by.organisation } - created_at { Time.utc(2022, 2, 8, 16, 52, 15) } - updated_at { Time.utc(2022, 2, 8, 16, 52, 15) } + created_at { Time.utc(2023, 2, 8, 16, 52, 15) } + updated_at { Time.utc(2023, 2, 8, 16, 52, 15) } trait :in_progress do purchid { "PC123" } ownershipsch { 2 } type { 8 } - saledate { Time.utc(2022, 2, 2, 10, 36, 49) } + saledate { Time.utc(2023, 2, 2, 10, 36, 49) } end trait :completed do ownershipsch { 2 } type { 8 } - saledate { Time.utc(2022, 2, 2, 10, 36, 49) } + saledate { Time.utc(2023, 2, 2, 10, 36, 49) } companybuy { 1 } jointpur { 1 } beds { 2 } diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 8783efd09..7ca507c1d 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -111,12 +111,15 @@ RSpec.describe Validations::Sales::SaleInformationValidations do 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 + 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"], ) + expect(record.errors[:saledate]).to eq( + ["Completion date must be less than 1 year after contract exchange date"], + ) end end @@ -129,6 +132,9 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors[:exdate]).to eq( ["Contract exchange date must be less than 1 year before completion date"], ) + expect(record.errors[:saledate]).to eq( + ["Completion date must be less than 1 year after contract exchange date"], + ) end end diff --git a/spec/models/validations/sales/setup_validations_spec.rb b/spec/models/validations/sales/setup_validations_spec.rb new file mode 100644 index 000000000..74b7834ab --- /dev/null +++ b/spec/models/validations/sales/setup_validations_spec.rb @@ -0,0 +1,49 @@ +require "rails_helper" + +RSpec.describe Validations::Sales::SetupValidations do + subject(:setup_validator) { validator_class.new } + + let(:validator_class) { Class.new { include Validations::Sales::SetupValidations } } + + describe "#validate_saledate" do + context "when saledate is blank" do + let(:record) { FactoryBot.build(:sales_log, saledate: nil) } + + it "does not add an error" do + setup_validator.validate_saledate(record) + + expect(record.errors).to be_empty + end + end + + context "when saledate is in the 22/23 financial year" do + let(:record) { FactoryBot.build(:sales_log, saledate: Time.zone.local(2023, 1, 1)) } + + it "does not add an error" do + setup_validator.validate_saledate(record) + + expect(record.errors).to be_empty + end + end + + context "when saledate is before the 22/23 financial year" do + let(:record) { FactoryBot.build(:sales_log, saledate: Time.zone.local(2022, 1, 1)) } + + it "adds error" do + setup_validator.validate_saledate(record) + + expect(record.errors[:saledate]).to include(I18n.t("validations.setup.saledate.financial_year")) + end + end + + context "when saledate is after the 22/23 financial year" do + let(:record) { FactoryBot.build(:sales_log, saledate: Time.zone.local(2023, 4, 1)) } + + it "adds error" do + setup_validator.validate_saledate(record) + + expect(record.errors[:saledate]).to include(I18n.t("validations.setup.saledate.financial_year")) + end + end + end +end diff --git a/spec/requests/sales_logs_controller_spec.rb b/spec/requests/sales_logs_controller_spec.rb index f8690a71b..925e3f1f3 100644 --- a/spec/requests/sales_logs_controller_spec.rb +++ b/spec/requests/sales_logs_controller_spec.rb @@ -179,29 +179,29 @@ RSpec.describe SalesLogsController, type: :request do end context "with year filter" do - let!(:sales_log_2021) do + let!(:sales_log_2022) do FactoryBot.create(:sales_log, :in_progress, owning_organisation: organisation, - saledate: Time.zone.local(2022, 3, 1)) + saledate: Time.zone.local(2022, 4, 1)) end - let!(:sales_log_2022) do + let!(:sales_log_2023) do sales_log = FactoryBot.build(:sales_log, :completed, owning_organisation: organisation, - saledate: Time.zone.local(2022, 12, 1)) + saledate: Time.zone.local(2023, 1, 1)) sales_log.save!(validate: false) sales_log end it "shows sales logs for multiple selected years" do get "/sales-logs?years[]=2021&years[]=2022", headers: headers, params: {} - expect(page).to have_link(sales_log_2021.id.to_s) expect(page).to have_link(sales_log_2022.id.to_s) + expect(page).to have_link(sales_log_2023.id.to_s) end it "shows sales logs for one selected year" do - get "/sales-logs?years[]=2021", headers: headers, params: {} - expect(page).to have_link(sales_log_2021.id.to_s) - expect(page).not_to have_link(sales_log_2022.id.to_s) + get "/sales-logs?years[]=2022", headers: headers, params: {} + expect(page).to have_link(sales_log_2022.id.to_s) + expect(page).to have_link(sales_log_2023.id.to_s) end end @@ -214,23 +214,23 @@ RSpec.describe SalesLogsController, type: :request do Timecop.unfreeze end - let!(:sales_log_2021) do + let!(:sales_log_2022) do FactoryBot.create(:sales_log, :completed, owning_organisation: organisation, - saledate: Time.zone.local(2022, 3, 1), + saledate: Time.zone.local(2022, 4, 1), created_by: user) end - let!(:sales_log_2022) do + let!(:sales_log_2023) do FactoryBot.create(:sales_log, owning_organisation: organisation, - saledate: Time.zone.local(2022, 12, 1), + saledate: Time.zone.local(2023, 1, 1), created_by: user) end it "shows sales logs for multiple selected statuses and years" do get "/sales-logs?years[]=2021&years[]=2022&status[]=in_progress&status[]=completed", headers: headers, params: {} - expect(page).to have_link(sales_log_2021.id.to_s) expect(page).to have_link(sales_log_2022.id.to_s) + expect(page).to have_link(sales_log_2023.id.to_s) end end end