From 3fc7366444fdaba710952512b7b4db9e020fad82 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 21 Nov 2022 09:17:07 +0000 Subject: [PATCH] Prevent deactivations during deactivated periods --- app/models/location.rb | 2 ++ config/locales/en.yml | 2 ++ spec/requests/locations_controller_spec.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/app/models/location.rb b/app/models/location.rb index 5ff14e8e0..30855cbcd 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -424,6 +424,8 @@ class Location < ApplicationRecord elsif deactivation_date_type == "other" errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.invalid")) end + elsif location_deactivation_periods.any? { |period| deactivation_date.between?(period.deactivation_date, period.reactivation_date) } + errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation.during_deactivated_period")) else unless deactivation_date.between?(available_from, Time.zone.local(2200, 1, 1)) errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: available_from.to_formatted_s(:govuk_date))) diff --git a/config/locales/en.yml b/config/locales/en.yml index 85c32c290..f839ccf7e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -325,6 +325,8 @@ en: 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" + deactivation: + during_deactivated_period: "The location is already deactivated during this date, please enter a different date" soft_validations: net_income: diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 71413cc91..9202ccf4e 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1242,10 +1242,13 @@ RSpec.describe LocationsController, type: :request do let(:deactivation_date) { Time.utc(2022, 10, 10) } let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) } let(:startdate) { Time.utc(2022, 10, 11) } + let(:add_deactivations) { nil } before do Timecop.freeze(Time.utc(2022, 10, 10)) sign_in user + add_deactivations + location.save patch "/schemes/#{scheme.id}/locations/#{location.id}/new-deactivation", params: end @@ -1371,6 +1374,17 @@ RSpec.describe LocationsController, type: :request do expect(page).to have_content(I18n.t("validations.location.toggle_date.invalid")) end end + + context "when deactivation date is during a deactivated period" do + let(:deactivation_date) { Time.zone.local(2022, 10, 10) } + let(:params) { { location: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2022" } } } + let(:add_deactivations) { location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 10, 12)) } + + 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.deactivation.during_deactivated_period")) + end + end end end