From d338d277bf8f995cdc5efd841ded6b40ce572eb2 Mon Sep 17 00:00:00 2001 From: Dushan <47317567+dushan-madetech@users.noreply.github.com> Date: Fri, 24 Jun 2022 10:18:41 +0100 Subject: [PATCH] CLDC-456-start-date-validations (#683) * start date validations * remove condition * move code * lint fixes * updates * content update * remove unneeded validations * validations * review changes * Update app/models/validations/date_validations.rb Co-authored-by: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> * update error messages * lint fixes * fix specs Co-authored-by: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> --- app/models/case_log.rb | 1 + app/models/scheme.rb | 1 + app/models/validations/date_validations.rb | 11 ++++++ config/initializers/feature_toggle.rb | 4 ++ config/locales/en.yml | 3 ++ ...0220616130451_add_reference_to_case_log.rb | 5 +++ .../20220617102313_add_end_date_to_schemes.rb | 5 +++ db/schema.rb | 6 ++- spec/models/case_log_spec.rb | 19 +++++++++- .../validations/date_validations_spec.rb | 37 ++++++++++++++++++ spec/requests/case_logs_controller_spec.rb | 38 ++++++++++++------- spec/requests/form_controller_spec.rb | 23 +++++++---- 12 files changed, 128 insertions(+), 25 deletions(-) create mode 100644 db/migrate/20220616130451_add_reference_to_case_log.rb create mode 100644 db/migrate/20220617102313_add_end_date_to_schemes.rb diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 7f9e1ecd6..038df317f 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -35,6 +35,7 @@ class CaseLog < ApplicationRecord belongs_to :owning_organisation, class_name: "Organisation", optional: true belongs_to :managing_organisation, class_name: "Organisation", optional: true belongs_to :created_by, class_name: "User", optional: true + belongs_to :scheme, optional: true scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :filter_by_status, ->(status, _user = nil) { where status: } diff --git a/app/models/scheme.rb b/app/models/scheme.rb index bc3273ef4..625ea28f1 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -1,6 +1,7 @@ class Scheme < ApplicationRecord belongs_to :organisation has_many :locations + has_many :case_logs scope :search_by_code, ->(code) { where("code ILIKE ?", "%#{code}%") } scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") } diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 669718868..774f9828c 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -34,6 +34,17 @@ module Validations::DateValidations if record.startdate < first_collection_start_date || record.startdate > second_collection_end_date record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") end + + if FeatureToggle.startdate_two_week_validation_enabled? && (record.startdate > Time.zone.today + 14) + record.errors.add :startdate, I18n.t("validations.setup.startdate.later_than_14_days_after") + end + + if record.scheme_id.present? + scheme_end_date = record.scheme.end_date + if scheme_end_date.present? && (record.startdate > scheme_end_date) + record.errors.add :startdate, I18n.t("validations.setup.startdate.before_scheme_end_date") + end + end end def validate_sale_completion_date(record) diff --git a/config/initializers/feature_toggle.rb b/config/initializers/feature_toggle.rb index 7cf1dbb76..452e0853b 100644 --- a/config/initializers/feature_toggle.rb +++ b/config/initializers/feature_toggle.rb @@ -4,4 +4,8 @@ class FeatureToggle false end + + def self.startdate_two_week_validation_enabled? + true + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 726dcacc0..0b76d9abf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -61,6 +61,9 @@ en: setup: intermediate_rent_product_name: blank: "Enter name of other intermediate rent product" + 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" property: mrcdate: diff --git a/db/migrate/20220616130451_add_reference_to_case_log.rb b/db/migrate/20220616130451_add_reference_to_case_log.rb new file mode 100644 index 000000000..52f7bfecb --- /dev/null +++ b/db/migrate/20220616130451_add_reference_to_case_log.rb @@ -0,0 +1,5 @@ +class AddReferenceToCaseLog < ActiveRecord::Migration[7.0] + def change + add_reference :case_logs, :scheme, foreign_key: true + end +end diff --git a/db/migrate/20220617102313_add_end_date_to_schemes.rb b/db/migrate/20220617102313_add_end_date_to_schemes.rb new file mode 100644 index 000000000..1c1946b20 --- /dev/null +++ b/db/migrate/20220617102313_add_end_date_to_schemes.rb @@ -0,0 +1,5 @@ +class AddEndDateToSchemes < ActiveRecord::Migration[7.0] + def change + add_column :schemes, :end_date, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index ca8c033ee..56f4c9b08 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_06_14_124115) do +ActiveRecord::Schema[7.0].define(version: 2022_06_17_102313) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -198,10 +198,12 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_14_124115) do t.integer "hhtype" t.integer "new_old" t.integer "vacdays" + t.bigint "scheme_id" t.index ["created_by_id"], name: "index_case_logs_on_created_by_id" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" t.index ["old_id"], name: "index_case_logs_on_old_id", unique: true t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" + t.index ["scheme_id"], name: "index_case_logs_on_scheme_id" end create_table "data_protection_confirmations", force: :cascade do |t| @@ -308,6 +310,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_14_124115) do t.integer "registered_under_care_act" t.integer "support_type" t.string "intended_stay" + t.datetime "end_date" t.index ["organisation_id"], name: "index_schemes_on_organisation_id" end @@ -364,6 +367,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_14_124115) do t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" end + add_foreign_key "case_logs", "schemes" add_foreign_key "locations", "schemes" add_foreign_key "schemes", "organisations" end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index eb7d82163..b07b913fe 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1881,9 +1881,14 @@ RSpec.describe CaseLog do let!(:case_log_2) { FactoryBot.create(:case_log, :completed, startdate: Time.utc(2021, 5, 3), created_by: created_by_user) } before do + Timecop.freeze(Time.utc(2022, 6, 3)) FactoryBot.create(:case_log, startdate: Time.utc(2022, 6, 3)) end + after do + Timecop.unfreeze + end + context "when searching logs" do let!(:case_log_to_search) { FactoryBot.create(:case_log, :completed) } @@ -1981,6 +1986,14 @@ RSpec.describe CaseLog do end context "when filtering by year" do + before do + Timecop.freeze(Time.utc(2021, 5, 3)) + end + + after do + Timecop.unfreeze + end + it "allows filtering on a single year" do expect(described_class.filter_by_years(%w[2021]).count).to eq(2) end @@ -1994,8 +2007,10 @@ RSpec.describe CaseLog do end it "filters based on date boundaries correctly" do - case_log_1.update!(startdate: Time.zone.local(2022, 4, 1)) - case_log_2.update!(startdate: Time.zone.local(2022, 3, 31)) + case_log_1.startdate = Time.zone.local(2022, 4, 1) + case_log_1.save!(validate: false) + case_log_2.startdate = Time.zone.local(2022, 3, 31) + case_log_2.save!(validate: false) expect(described_class.filter_by_years(%w[2021]).count).to eq(1) expect(described_class.filter_by_years(%w[2022]).count).to eq(2) diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 9cf8e1d8d..ca09c28c8 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -5,6 +5,8 @@ RSpec.describe Validations::DateValidations do let(:validator_class) { Class.new { include Validations::DateValidations } } let(:record) { FactoryBot.create(:case_log) } + let(:scheme) { FactoryBot.create(:scheme, end_date: Time.zone.today - 5.days) } + let(:scheme_no_end_date) { FactoryBot.create(:scheme, end_date: nil) } describe "tenancy start date" do it "cannot be before the first collection window start date" do @@ -30,6 +32,41 @@ RSpec.describe Validations::DateValidations do date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty end + + it "validates that the tenancy start date is before the end date of the chosen scheme if it has an end date" do + record.startdate = Time.zone.today - 3.days + record.scheme = scheme + date_validator.validate_startdate(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.before_scheme_end_date")) + end + + it "produces no error when the tenancy start date is before the end date of the chosen scheme if it has an end date" do + record.startdate = Time.zone.today - 30.days + record.scheme = scheme + date_validator.validate_startdate(record) + expect(record.errors["startdate"]).to be_empty + end + + it "produces no startdate error for scheme end dates when the chosen scheme does not have an end date" do + record.startdate = Time.zone.today + record.scheme = scheme_no_end_date + date_validator.validate_startdate(record) + expect(record.errors["startdate"]).to be_empty + end + + it "validates that the tenancy start date is not later than 14 days from the current date" do + record.startdate = Time.zone.today + 15.days + date_validator.validate_startdate(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.later_than_14_days_after")) + end + + it "produces no error when tenancy start date is not later than 14 days from the current date" do + record.startdate = Time.zone.today + 7.days + date_validator.validate_startdate(record) + expect(record.errors["startdate"]).to be_empty + end end describe "major repairs date" do diff --git a/spec/requests/case_logs_controller_spec.rb b/spec/requests/case_logs_controller_spec.rb index 4caeeeca9..4a19fbe82 100644 --- a/spec/requests/case_logs_controller_spec.rb +++ b/spec/requests/case_logs_controller_spec.rb @@ -249,12 +249,14 @@ RSpec.describe CaseLogsController, type: :request do managing_organisation: organisation) end let!(:case_log_2022) do - FactoryBot.create(:case_log, :completed, - owning_organisation: organisation, - mrcdate: Time.zone.local(2022, 2, 1), - startdate: Time.zone.local(2022, 12, 1), - tenancy: 6, - managing_organisation: organisation) + case_log = FactoryBot.build(:case_log, :completed, + owning_organisation: organisation, + mrcdate: Time.zone.local(2022, 2, 1), + startdate: Time.zone.local(2022, 12, 1), + tenancy: 6, + managing_organisation: organisation) + case_log.save!(validate: false) + case_log end it "shows case logs for multiple selected years" do @@ -271,6 +273,14 @@ RSpec.describe CaseLogsController, type: :request do end context "with year and status filter" do + before do + Timecop.freeze(Time.zone.local(2022, 12, 1)) + end + + after do + Timecop.unfreeze + end + let!(:case_log_2021) do FactoryBot.create(:case_log, :in_progress, owning_organisation: organisation, @@ -288,14 +298,14 @@ RSpec.describe CaseLogsController, type: :request do created_by: user) end let!(:case_log_2022_in_progress) do - FactoryBot.create(:case_log, :in_progress, - owning_organisation: organisation, - mrcdate: Time.zone.local(2022, 2, 1), - startdate: Time.zone.local(2022, 12, 1), - tenancy: 6, - managing_organisation: organisation, - tenancycode: nil, - created_by: user) + FactoryBot.build(:case_log, :in_progress, + owning_organisation: organisation, + mrcdate: Time.zone.local(2022, 2, 1), + startdate: Time.zone.local(2022, 12, 1), + tenancy: 6, + managing_organisation: organisation, + tenancycode: nil, + created_by: user) end it "shows case logs for multiple selected statuses and years" do diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 0d3e55521..9e220486f 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -37,14 +37,6 @@ RSpec.describe FormController, type: :request do managing_organisation: organisation, ) end - let(:case_log_2022) do - FactoryBot.create( - :case_log, - startdate: Time.zone.local(2022, 12, 1), - owning_organisation: organisation, - managing_organisation: organisation, - ) - end let(:headers) { { "Accept" => "text/html" } } context "when a user is not signed in" do @@ -112,10 +104,25 @@ RSpec.describe FormController, type: :request do end context "when no other sections are enabled" do + let(:case_log_2022) do + FactoryBot.create( + :case_log, + startdate: Time.zone.local(2022, 12, 1), + owning_organisation: organisation, + managing_organisation: organisation, + ) + end + let(:headers) { { "Accept" => "text/html" } } + before do + Timecop.freeze(Time.zone.local(2022, 12, 1)) get "/logs/#{case_log_2022.id}/setup/check-answers", headers: headers, params: {} end + after do + Timecop.unfreeze + end + it "does not show Save and go to next incomplete section button" do expect(page).not_to have_content("Save and go to next incomplete section") end