Browse Source

Update sales validation to allow editing existing logs

pull/1716/head
Kat 2 years ago
parent
commit
39b7e4bccb
  1. 5
      app/models/form_handler.rb
  2. 16
      app/models/validations/sales/setup_validations.rb
  3. 46
      spec/models/validations/sales/setup_validations_spec.rb

5
app/models/form_handler.rb

@ -122,6 +122,11 @@ class FormHandler
forms.count { |form| now.between?(form.start_date, form.new_logs_end_date) } > 1
end
def sales_in_edit_crossover_period?(now: Time.zone.now)
forms = sales_forms.values
forms.count { |form| now.between?(form.start_date, form.edit_end_date) } > 1
end
def use_fake_forms!(fake_forms = nil)
@directories = ["spec/fixtures/forms"]
@forms = fake_forms || get_all_forms

16
app/models/validations/sales/setup_validations.rb

@ -5,7 +5,13 @@ module Validations::Sales::SetupValidations
def validate_saledate_collection_year(record)
return unless record.saledate && date_valid?("saledate", record) && FeatureToggle.saledate_collection_window_validation_enabled?
unless record.saledate.between?(active_collection_start_date, current_collection_end_date)
first_collection_start_date = if record.saledate_was.present?
editable_collection_start_date
else
active_collection_start_date
end
unless record.saledate.between?(first_collection_start_date, current_collection_end_date)
record.errors.add :saledate, saledate_validation_error_message
end
end
@ -28,6 +34,14 @@ private
end
end
def editable_collection_start_date
if FormHandler.instance.sales_in_edit_crossover_period?
previous_collection_start_date
else
current_collection_start_date
end
end
def saledate_validation_error_message
current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y")

46
spec/models/validations/sales/setup_validations_spec.rb

@ -105,6 +105,52 @@ RSpec.describe Validations::Sales::SetupValidations do
expect(record.errors[:saledate]).to include("Enter a date within the 23/24 or 24/25 collection years, which is between 1st April 2023 and 31st March 2025")
end
end
context "when after the new logs end date but before edit end date for the previous period" do
let(:record) { build(:sales_log, saledate: Time.zone.local(2025, 4, 1)) }
before do
allow(Time).to receive(:now).and_return(Time.zone.local(2024, 8, 8))
end
it "cannot create new logs for the previous collection year" do
record.update!(saledate: nil)
record.saledate = Time.zone.local(2024, 1, 1)
setup_validator.validate_saledate_collection_year(record)
expect(record.errors["saledate"]).to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025")
end
it "can edit already created logs logs for the previous collection year" do
record.saledate = Time.zone.local(2024, 1, 2)
record.save!(validate: false)
record.saledate = Time.zone.local(2024, 1, 1)
setup_validator.validate_saledate_collection_year(record)
expect(record.errors["saledate"]).not_to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025")
end
end
context "when after the new logs end date and after the edit end date for the previous period" do
let(:record) { build(:sales_log, saledate: Time.zone.local(2025, 4, 1)) }
before do
allow(Time).to receive(:now).and_return(Time.zone.local(2024, 12, 8))
end
it "cannot create new logs for the previous collection year" do
record.update!(saledate: nil)
record.saledate = Time.zone.local(2024, 1, 1)
setup_validator.validate_saledate_collection_year(record)
expect(record.errors["saledate"]).to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025")
end
it "cannot edit already created logs logs for the previous collection year" do
record.saledate = Time.zone.local(2024, 1, 2)
record.save!(validate: false)
record.saledate = Time.zone.local(2024, 1, 1)
setup_validator.validate_saledate_collection_year(record)
expect(record.errors["saledate"]).to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025")
end
end
end
end

Loading…
Cancel
Save