From ae850fa20ed6ab7513fa6531bcfe741e3471f07e Mon Sep 17 00:00:00 2001 From: David May-Miller Date: Wed, 7 Sep 2022 09:46:20 +0100 Subject: [PATCH] CLDC-1428 Prevent creation of new 2021/22 logs (#853) --- app/models/validations/date_validations.rb | 14 ++++++++++++++ config/forms/2021_2022.json | 2 +- spec/models/lettings_log_spec.rb | 3 +++ spec/requests/lettings_logs_controller_spec.rb | 5 +++++ spec/services/csv/lettings_log_csv_service_spec.rb | 2 +- 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 6351a712b..8f8d87220 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -31,6 +31,12 @@ module Validations::DateValidations def validate_startdate(record) return unless record.startdate && date_valid?("startdate", record) + created_at = record.created_at || Time.zone.now + + if created_at > first_collection_end_date && record.startdate < second_collection_start_date + record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") + end + 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 @@ -57,6 +63,14 @@ private @first_collection_start_date ||= FormHandler.instance.forms.map { |form| form.second.start_date }.compact.min end + def first_collection_end_date + @first_collection_end_date ||= FormHandler.instance.forms.map { |form| form.second.end_date }.compact.min + end + + def second_collection_start_date + @second_collection_start_date ||= FormHandler.instance.forms.map { |form| form.second.start_date }.compact.max + end + def second_collection_end_date @second_collection_end_date ||= FormHandler.instance.forms.map { |form| form.second.end_date }.compact.max end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 1ffbc1228..4abe314f8 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1,7 +1,7 @@ { "form_type": "lettings", "start_date": "2021-04-01T00:00:00.000+01:00", - "end_date": "2022-07-01T00:00:00.000+01:00", + "end_date": "2022-09-05T00:00:00.000+01:00", "sections": { "tenancy_and_property": { "label": "Property and tenancy information", diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index d1febd01c..772255b19 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -204,6 +204,7 @@ RSpec.describe LettingsLog do rent_type: 4, hb: 1, hbrentshortfall: 1, + created_at: Time.utc(2022, 2, 8, 16, 52, 15), }) end @@ -1378,6 +1379,7 @@ RSpec.describe LettingsLog do created_by: created_by_user, renewal: 1, startdate: Time.zone.local(2021, 4, 10), + created_at: Time.utc(2022, 2, 8, 16, 52, 15), }) end @@ -1712,6 +1714,7 @@ RSpec.describe LettingsLog do location_id: location.id, renewal: 1, startdate: Time.zone.now, + created_at: Time.utc(2022, 2, 8, 16, 52, 15), }) end diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index 3063b3fb3..e70cace51 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/spec/requests/lettings_logs_controller_spec.rb @@ -49,9 +49,14 @@ RSpec.describe LettingsLogsController, type: :request do end before do + Timecop.freeze(Time.utc(2022, 2, 8)) post "/logs", headers:, params: params.to_json end + after do + Timecop.unfreeze + end + it "returns http success" do expect(response).to have_http_status(:success) end diff --git a/spec/services/csv/lettings_log_csv_service_spec.rb b/spec/services/csv/lettings_log_csv_service_spec.rb index 55f0c9b47..43a26440a 100644 --- a/spec/services/csv/lettings_log_csv_service_spec.rb +++ b/spec/services/csv/lettings_log_csv_service_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Csv::LettingsLogCsvService do let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } before do - LettingsLog.create!(startdate: "2021-10-10") + LettingsLog.create!(startdate: "2021-10-10", created_at: Time.utc(2022, 2, 8, 16, 52, 15)) allow(FormHandler.instance).to receive(:get_form).and_return(real_2021_2022_form) end