Browse Source

Prevent deactivations during deactivated periods

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

2
app/models/location.rb

@ -424,6 +424,8 @@ class Location < ApplicationRecord
elsif deactivation_date_type == "other" elsif deactivation_date_type == "other"
errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.invalid")) errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.invalid"))
end 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 else
unless deactivation_date.between?(available_from, Time.zone.local(2200, 1, 1)) 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))) errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: available_from.to_formatted_s(:govuk_date)))

2
config/locales/en.yml

@ -325,6 +325,8 @@ en:
out_of_range: "The date must be on or after the %{date}" out_of_range: "The date must be on or after the %{date}"
reactivation: reactivation:
before_deactivation: "This location was deactivated on %{date}\nThe reactivation date must be on or after deactivation date" 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: soft_validations:
net_income: net_income:

14
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(:deactivation_date) { Time.utc(2022, 10, 10) }
let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) } let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) }
let(:startdate) { Time.utc(2022, 10, 11) } let(:startdate) { Time.utc(2022, 10, 11) }
let(:add_deactivations) { nil }
before do before do
Timecop.freeze(Time.utc(2022, 10, 10)) Timecop.freeze(Time.utc(2022, 10, 10))
sign_in user sign_in user
add_deactivations
location.save
patch "/schemes/#{scheme.id}/locations/#{location.id}/new-deactivation", params: patch "/schemes/#{scheme.id}/locations/#{location.id}/new-deactivation", params:
end end
@ -1371,6 +1374,17 @@ RSpec.describe LocationsController, type: :request do
expect(page).to have_content(I18n.t("validations.location.toggle_date.invalid")) expect(page).to have_content(I18n.t("validations.location.toggle_date.invalid"))
end end
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
end end

Loading…
Cancel
Save