diff --git a/app/models/location.rb b/app/models/location.rb index 6d179df03..e4c6311fc 100644 --- a/app/models/location.rb +++ b/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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 99d93f930..85c32c290 100644 --- a/config/locales/en.yml +++ b/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: diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 4b3093e10..068fa4125 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/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