diff --git a/app/models/location.rb b/app/models/location.rb index e16c01dbd..de272a415 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -396,7 +396,7 @@ class Location < ApplicationRecord periods << ActivePeriod.new(deactivation.reactivation_date, nil) end - periods.select { |period| (period.to.present? || period.from.present?) && (period.to.nil? || (period.from.present? && period.from <= period.to)) } + periods.select { |period| (period.from != period.to) && (period.to.nil? || (period.from.present? && period.from <= period.to)) } end def active? @@ -424,7 +424,7 @@ 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) } + elsif location_deactivation_periods.any? { |period| deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) } 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)) diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index f6a06d276..e6b9d5a21 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -214,6 +214,15 @@ RSpec.describe Location, type: :model do expect(location.active_periods.first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: nil) end + it "ignores reactivations that were deactivated on the same day" do + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4)) + location.save! + + expect(location.active_periods.count).to eq(1) + expect(location.active_periods.first).to have_attributes(from: Time.zone.local(2022, 4, 1), to: Time.zone.local(2022, 5, 5)) + end + it "returns sequential non reactivated active periods" do location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 4)) location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 7, 6))