Browse Source

Add validation for reactivation date before deactivation date

pull/1007/head
Kat 4 years ago
parent
commit
2a26b142a6
  1. 5
      app/models/location.rb
  2. 2
      config/locales/en.yml
  3. 12
      spec/requests/locations_controller_spec.rb

5
app/models/location.rb

@ -426,6 +426,7 @@ class Location < ApplicationRecord
def reactivation_date_errors
return unless @run_reactivation_validations
recent_deactivation = location_deactivation_periods.deactivations_without_reactivation.first
if reactivation_date.blank?
if reactivation_date_type.blank?
errors.add(:reactivation_date_type, message: I18n.t("validations.location.toggle_date.not_selected"))
@ -434,8 +435,10 @@ class Location < ApplicationRecord
end
else
collection_start_date = FormHandler.instance.current_collection_start_date
unless reactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1))
if !reactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1))
errors.add(:reactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
elsif reactivation_date < recent_deactivation.deactivation_date
errors.add(:reactivation_date, message: I18n.t("validations.location.reactivation.before_deactivation", date: recent_deactivation.deactivation_date.to_formatted_s(:govuk_date)))
end
end
end

2
config/locales/en.yml

@ -323,6 +323,8 @@ en:
not_selected: "Select one of the options"
invalid: "Enter a valid day, month and year"
out_of_range: "The date must be on or after the %{date}"
reactivation:
before_deactivation: "This location was deactivated on %{date}\nThe reactivation date must be on or after deactivation date"
soft_validations:
net_income:

12
spec/requests/locations_controller_spec.rb

@ -1478,7 +1478,7 @@ RSpec.describe LocationsController, type: :request do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:location) { FactoryBot.create(:location, scheme:) }
let(:deactivation_date) { Time.zone.local(2022, 10, 10) }
let(:deactivation_date) { Time.zone.local(2022, 4, 1) }
let(:startdate) { Time.utc(2022, 10, 11) }
before do
@ -1594,6 +1594,16 @@ RSpec.describe LocationsController, type: :request do
expect(page).to have_content(I18n.t("validations.location.toggle_date.invalid"))
end
end
context "when the reactivation date is before deactivation date" do
let(:deactivation_date) { Time.zone.local(2022, 10, 10) }
let(:params) { { location: { reactivation_date_type: "other", "reactivation_date(3i)": "8", "reactivation_date(2i)": "9", "reactivation_date(1i)": "2022" } } }
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.location.reactivation.before_deactivation", date: "10 October 2022"))
end
end
end
end
end

Loading…
Cancel
Save