Browse Source

Allow logs to be edited if the date is before edit end date

pull/1716/head
Kat 2 years ago
parent
commit
9d96ad4319
  1. 5
      app/models/form_handler.rb
  2. 16
      app/models/validations/setup_validations.rb
  3. 42
      spec/models/validations/setup_validations_spec.rb

5
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

16
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")

42
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

Loading…
Cancel
Save