From 9d96ad4319b796f8ce582c4e61f0a2c0b58a9d2a Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 21 Jun 2023 16:07:29 +0100 Subject: [PATCH] Allow logs to be edited if the date is before edit end date --- app/models/form_handler.rb | 5 +++ app/models/validations/setup_validations.rb | 16 ++++++- .../validations/setup_validations_spec.rb | 42 +++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 50a28fc18..360ad9899 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -112,6 +112,11 @@ class FormHandler forms.count { |form| now.between?(form.start_date, form.new_logs_end_date) } > 1 end + def lettings_in_edit_crossover_period?(now: Time.zone.now) + forms = lettings_forms.values + forms.count { |form| now.between?(form.start_date, form.edit_end_date) } > 1 + end + def sales_in_crossover_period?(now: Time.zone.now) forms = sales_forms.values forms.count { |form| now.between?(form.start_date, form.new_logs_end_date) } > 1 diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index fd71dd4bc..bb76a7668 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -5,7 +5,13 @@ module Validations::SetupValidations def validate_startdate_setup(record) return unless record.startdate && date_valid?("startdate", record) - unless record.startdate.between?(active_collection_start_date, current_collection_end_date) + first_collection_start_date = if record.startdate_was.present? + editable_collection_start_date + else + active_collection_start_date + end + + unless record.startdate.between?(first_collection_start_date, current_collection_end_date) record.errors.add :startdate, startdate_validation_error_message end end @@ -60,6 +66,14 @@ private end end + def editable_collection_start_date + if FormHandler.instance.lettings_in_edit_crossover_period? + previous_collection_start_date + else + current_collection_start_date + end + end + def startdate_validation_error_message current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y") diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index 51c157215..f85af32ff 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -85,6 +85,48 @@ RSpec.describe Validations::SetupValidations do expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") end end + + context "when after the new logs end date but before edit end date for the previous period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 8, 8)) + end + + it "cannot create new logs for the previous collection year" do + record.update!(startdate: nil) + record.startdate = Time.zone.local(2023, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") + end + + it "can edit already created logs logs for the previous collection year" do + record.startdate = Time.zone.local(2023, 1, 2) + record.save!(validate: false) + record.startdate = Time.zone.local(2023, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).not_to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") + end + end + + context "when after the new logs end date and after the edit end date for the previous period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 12, 8)) + end + + it "cannot create new logs for the previous collection year" do + record.update!(startdate: nil) + record.startdate = Time.zone.local(2023, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") + end + + it "cannot edit already created logs logs for the previous collection year" do + record.startdate = Time.zone.local(2023, 1, 2) + record.save!(validate: false) + record.startdate = Time.zone.local(2023, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") + end + end end end