From aac247b1fa70aedc073905de68ebd3ca3494e040 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 23 Nov 2022 08:59:50 +0000 Subject: [PATCH] choose newest reactivation date --- app/models/location.rb | 3 +-- .../validations/date_validations_spec.rb | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/models/location.rb b/app/models/location.rb index c14f40324..bbcf7af4d 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -394,9 +394,8 @@ class Location < ApplicationRecord end def status_during(date) - closest_reactivation = location_deactivation_periods.find { |period| period.reactivation_date.present? && date.between?(period.deactivation_date, period.reactivation_date) } + closest_reactivation = location_deactivation_periods.reverse.find { |period| period.reactivation_date.present? && date.between?(period.deactivation_date, period.reactivation_date) } return { status: :reactivating_soon, date: closest_reactivation.reactivation_date } if closest_reactivation.present? - return { status: :reactivating_soon, date: available_from } if available_from > date open_deactivation = location_deactivation_periods.deactivations_without_reactivation.first diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 5ca1b95e7..dafb2b86d 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -134,6 +134,33 @@ RSpec.describe Validations::DateValidations do end end + context "with a location that has many reactivations soon" do + let(:scheme) { create(:scheme) } + let(:location) { create(:location, scheme:, startdate: nil) } + + before do + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:) + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), location:) + location.reload + end + + it "produces error when tenancy start date is during deactivated location period" do + record.startdate = Time.zone.local(2022, 7, 5) + record.location = location + date_validator.validate_startdate(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: location.postcode, date: "4 September 2022")) + end + + it "produces no error when tenancy start date is during an active location period" do + record.startdate = Time.zone.local(2022, 10, 1) + record.location = location + date_validator.validate_startdate(record) + expect(record.errors["startdate"]).to be_empty + end + end + context "with a location with no deactivation periods" do let(:scheme) { create(:scheme) } let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) }