diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index b80d969a0..5fb8f5ffa 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -210,12 +210,22 @@ class LocationsController < ApplicationController end def new_reactivation - @location_deactivation_period = @location.location_deactivation_periods.deactivations_without_reactivation.first + open_deactivations = @location.location_deactivation_periods&.deactivations_without_reactivation + if open_deactivations.blank? + render_not_found and return + end + + @location_deactivation_period = open_deactivations.first render "toggle_active", locals: { action: "reactivate" } end def reactivate - @location_deactivation_period = @location.location_deactivation_periods.deactivations_without_reactivation.first + open_deactivations = @location.location_deactivation_periods&.deactivations_without_reactivation + if open_deactivations.blank? + render_not_found and return + end + + @location_deactivation_period = open_deactivations.first @location_deactivation_period.reactivation_date = toggle_date("reactivation_date") @location_deactivation_period.reactivation_date_type = params[:location_deactivation_period][:reactivation_date_type] diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 27504e9de..4b018ee34 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -88,13 +88,22 @@ class SchemesController < ApplicationController end def new_reactivation - @scheme_deactivation_period = @scheme.scheme_deactivation_periods.deactivations_without_reactivation.first + open_deactivations = @scheme.scheme_deactivation_periods&.deactivations_without_reactivation + if open_deactivations.blank? + render_not_found and return + end + + @scheme_deactivation_period = open_deactivations.first render "toggle_active", locals: { action: "reactivate" } end def reactivate - @scheme_deactivation_period = @scheme.scheme_deactivation_periods.deactivations_without_reactivation.first + open_deactivations = @scheme.scheme_deactivation_periods&.deactivations_without_reactivation + if open_deactivations.blank? + render_not_found and return + end + @scheme_deactivation_period = open_deactivations.first @scheme_deactivation_period.reactivation_date = toggle_date("reactivation_date") @scheme_deactivation_period.reactivation_date_type = params[:scheme_deactivation_period][:reactivation_date_type] diff --git a/app/models/location.rb b/app/models/location.rb index 0f5616c16..4d9084f8c 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -220,8 +220,11 @@ class Location < ApplicationRecord location_deactivation_periods.deactivations_without_reactivation.first end - def last_deactivation_before(date) - location_deactivation_periods.where("deactivation_date <= ?", date).order("created_at").last + def reactivation_date_after(date) + return nil if location_deactivation_periods.deactivations_without_reactivation.any? + + periods_ending_in_future = location_deactivation_periods.deactivations_with_reactivation.where("reactivation_date > ?", date).all + periods_ending_in_future.select { |period| %i[active deactivating_soon].include?(status_at(period.reactivation_date)) }.map(&:reactivation_date).min end def status @@ -235,7 +238,7 @@ class Location < ApplicationRecord open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date || scheme.status_at(date) == :deactivated return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date || scheme.status_at(date) == :deactivating_soon return :activating_soon if startdate.present? && date < startdate - return :reactivating_soon if last_deactivation_before(date)&.reactivation_date.present? && date < last_deactivation_before(date).reactivation_date || scheme.status_at(date) == :reactivating_soon + return :reactivating_soon if location_deactivation_periods.deactivations_with_reactivation.any? { |p| p.includes_date?(date) } || scheme.status_at(date) == :reactivating_soon :active end diff --git a/app/models/location_deactivation_period.rb b/app/models/location_deactivation_period.rb index 3f222509e..aeee3a7ff 100644 --- a/app/models/location_deactivation_period.rb +++ b/app/models/location_deactivation_period.rb @@ -3,9 +3,15 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator def validate(record) location = record.location - recent_deactivation = location.location_deactivation_periods.deactivations_without_reactivation.first - if recent_deactivation.present? && recent_deactivation.deactivation_date <= 6.months.from_now - validate_reactivation(record, recent_deactivation, location) + open_deactivation = location.location_deactivation_periods.deactivations_without_reactivation.first + + # The LocationsController validates deactivation periods in three places: + # 1. new_deactivation builds a temporary location_deactivation_period object using the open deactivation if it starts in over six months, or otherwise builds a new period, and validates this (want validate_deactivation) + # 2. `deactivate` takes the open deactivation if present (any start date) and update!s it with the deactivation date, or else create!s a new deactivation with the deactivation date (want validate_deactivation) + # 3. `reactivate` takes the open deactivation (any start date) and update!s it with the reactivation date (want validate_reactivation) + # In toggle_location_link in the LocationsHelper, we display a link to one or neither of new_deactivation and new_reactivation depending on status now, status in six months, and scheme status. + if open_deactivation.present? && open_deactivation.deactivation_date <= 6.months.from_now + validate_reactivation(record, open_deactivation, location) else validate_deactivation(record, location) end @@ -48,4 +54,9 @@ class LocationDeactivationPeriod < ApplicationRecord attr_accessor :deactivation_date_type, :reactivation_date_type scope :deactivations_without_reactivation, -> { where(reactivation_date: nil) } + scope :deactivations_with_reactivation, -> { where.not(reactivation_date: nil) } + + def includes_date?(date) + deactivation_date <= date && (reactivation_date.nil? or reactivation_date > date) + end end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index ae772b30b..f563573b5 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -315,8 +315,11 @@ class Scheme < ApplicationRecord scheme_deactivation_periods.deactivations_without_reactivation.first end - def last_deactivation_before(date) - scheme_deactivation_periods.where("deactivation_date <= ?", date).order("created_at").last + def reactivation_date_after(date) + return nil if scheme_deactivation_periods.deactivations_without_reactivation.any? + + periods_ending_in_future = scheme_deactivation_periods.deactivations_with_reactivation.where("reactivation_date > ?", date).all + periods_ending_in_future.select { |period| %i[active deactivating_soon].include?(status_at(period.reactivation_date)) }.map(&:reactivation_date).min end def last_deactivation_date @@ -333,7 +336,7 @@ class Scheme < ApplicationRecord (open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date) return :incomplete unless confirmed && locations.confirmed.any? return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date - return :reactivating_soon if last_deactivation_before(date)&.reactivation_date.present? && date < last_deactivation_before(date).reactivation_date + return :reactivating_soon if scheme_deactivation_periods.deactivations_with_reactivation.any? { |p| p.includes_date?(date) } return :activating_soon if startdate.present? && date < startdate :active diff --git a/app/models/scheme_deactivation_period.rb b/app/models/scheme_deactivation_period.rb index cb27534f7..1d8895ce2 100644 --- a/app/models/scheme_deactivation_period.rb +++ b/app/models/scheme_deactivation_period.rb @@ -3,9 +3,15 @@ class SchemeDeactivationPeriodValidator < ActiveModel::Validator def validate(record) scheme = record.scheme - recent_deactivation = scheme.scheme_deactivation_periods.deactivations_without_reactivation.first - if recent_deactivation.present? && recent_deactivation.deactivation_date <= 6.months.from_now - validate_reactivation(record, recent_deactivation, scheme) + open_deactivation = scheme.scheme_deactivation_periods.deactivations_without_reactivation.first + + # The SchemesController validates deactivation periods in three places: + # 1. new_deactivation builds a temporary scheme_deactivation_period object using the open deactivation if it starts in over six months, or otherwise builds a new period, and validates this (want validate_deactivation) + # 2. `deactivate` takes the open deactivation if present (any start date) and update!s it with the deactivation date, or else create!s a new deactivation with the deactivation date (want validate_deactivation) + # 3. `reactivate` takes the open deactivation (any start date) and update!s it with the reactivation date (want validate_reactivation) + # In toggle_scheme_link in SchemesHelper, we display a link to one or neither of new_deactivation and new_reactivation depending on status now and status in six months. + if open_deactivation.present? && open_deactivation.deactivation_date <= 6.months.from_now + validate_reactivation(record, open_deactivation, scheme) else validate_deactivation(record, scheme) end @@ -49,4 +55,8 @@ class SchemeDeactivationPeriod < ApplicationRecord scope :deactivations_without_reactivation, -> { where(reactivation_date: nil) } scope :deactivations_with_reactivation, -> { where.not(reactivation_date: nil) } + + def includes_date?(date) + deactivation_date <= date && (reactivation_date.nil? or reactivation_date > date) + end end diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index 6fd2c1a95..d7e624527 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -102,10 +102,10 @@ module Validations::SetupValidations location_inactive_status = inactive_status(record.startdate, record.location) if location_inactive_status.present? - date, scope, deactivation_date = location_inactive_status.values_at(:date, :scope, :deactivation_date) - record.errors.add :startdate, :not_active, message: I18n.t("validations.lettings.setup.startdate.location.#{scope}.startdate", postcode: record.location.postcode, date:, deactivation_date:) - record.errors.add :location_id, :not_active, message: I18n.t("validations.lettings.setup.startdate.location.#{scope}.location_id", postcode: record.location.postcode, date:, deactivation_date:) - record.errors.add :scheme_id, :not_active, message: I18n.t("validations.lettings.setup.startdate.location.#{scope}.location_id", postcode: record.location.postcode, date:, deactivation_date:) + date, scope = location_inactive_status.values_at(:date, :scope) + record.errors.add :startdate, :not_active, message: I18n.t("validations.lettings.setup.startdate.location.#{scope}.startdate", postcode: record.location.postcode, date:) + record.errors.add :location_id, :not_active, message: I18n.t("validations.lettings.setup.startdate.location.#{scope}.location_id", postcode: record.location.postcode, date:) + record.errors.add :scheme_id, :not_active, message: I18n.t("validations.lettings.setup.startdate.location.#{scope}.location_id", postcode: record.location.postcode, date:) end end @@ -115,9 +115,9 @@ module Validations::SetupValidations scheme_inactive_status = inactive_status(record.startdate, record.scheme) if scheme_inactive_status.present? - date, scope, deactivation_date = scheme_inactive_status.values_at(:date, :scope, :deactivation_date) - record.errors.add :startdate, I18n.t("validations.lettings.setup.startdate.scheme.#{scope}.startdate", name: record.scheme.service_name, date:, deactivation_date:) - record.errors.add :scheme_id, I18n.t("validations.lettings.setup.startdate.scheme.#{scope}.scheme_id", name: record.scheme.service_name, date:, deactivation_date:) + date, scope = scheme_inactive_status.values_at(:date, :scope) + record.errors.add :startdate, I18n.t("validations.lettings.setup.startdate.scheme.#{scope}.startdate", name: record.scheme.service_name, date:) + record.errors.add :scheme_id, I18n.t("validations.lettings.setup.startdate.scheme.#{scope}.scheme_id", name: record.scheme.service_name, date:) end end diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index 7b0ecc420..530735213 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -73,7 +73,6 @@ module Validations::SharedValidations status = resource.status_at(date) return unless %i[reactivating_soon activating_soon deactivated].include?(status) - closest_reactivation = resource.last_deactivation_before(date) open_deactivation = if resource.is_a?(Location) resource.open_deactivation || resource.scheme.open_deactivation else @@ -81,12 +80,12 @@ module Validations::SharedValidations end date = case status - when :reactivating_soon then closest_reactivation.reactivation_date + when :reactivating_soon then resource.reactivation_date_after(date) when :activating_soon then resource&.available_from when :deactivated then open_deactivation.deactivation_date end - { scope: status, date: date&.to_formatted_s(:govuk_date), deactivation_date: closest_reactivation&.deactivation_date&.to_formatted_s(:govuk_date) } + { scope: status, date: date&.to_formatted_s(:govuk_date) } end def date_valid?(question, record) diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index 0a8eed96f..48374661c 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -115,7 +115,7 @@ RSpec.describe LocationsHelper do expect(location_active_periods(location).third).to have_attributes(from: over_a_year_ago + 3.months, to: nil) end - it "returns correct active periods when reactivation happends during a deactivated period" do + it "returns correct active periods when reactivation happens during a deactivated period" do FactoryBot.create(:location_deactivation_period, deactivation_date: over_a_year_ago, reactivation_date: one_year_ago, location:) FactoryBot.create(:location_deactivation_period, deactivation_date: beginning_of_collection + 2.days, reactivation_date: over_a_year_ago + 1.month, location:) location.reload diff --git a/spec/helpers/schemes_helper_spec.rb b/spec/helpers/schemes_helper_spec.rb index 8ffde636a..70ace2e24 100644 --- a/spec/helpers/schemes_helper_spec.rb +++ b/spec/helpers/schemes_helper_spec.rb @@ -62,7 +62,7 @@ RSpec.describe SchemesHelper do expect(scheme_active_periods(scheme).third).to have_attributes(from: Time.zone.local(2022, 8, 5), to: nil) end - it "returns correct active periods when reactivation happends during a deactivated period" do + it "returns correct active periods when reactivation happens during a deactivated period" do FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 11, 11), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 6), reactivation_date: Time.zone.local(2022, 7, 7), scheme:) scheme.reload diff --git a/spec/models/lettings_log_derived_fields_spec.rb b/spec/models/lettings_log_derived_fields_spec.rb index e4edb194e..46a09d7eb 100644 --- a/spec/models/lettings_log_derived_fields_spec.rb +++ b/spec/models/lettings_log_derived_fields_spec.rb @@ -1194,6 +1194,10 @@ RSpec.describe LettingsLog, type: :model do Timecop.freeze(Time.zone.local(2025, 5, 5)) end + after do + Timecop.return + end + it "derives the most recent let type as London Living Rent basis if it is a renewal" do log.assign_attributes(renewal: 1, rent_type:) diff --git a/spec/models/location_deactivation_period_spec.rb b/spec/models/location_deactivation_period_spec.rb index 6157b893b..709aec749 100644 --- a/spec/models/location_deactivation_period_spec.rb +++ b/spec/models/location_deactivation_period_spec.rb @@ -85,5 +85,83 @@ RSpec.describe LocationDeactivationPeriod do end end end + + context "when there is an open deactivation period less than six months in the future" do # validate_reactivation + let!(:location) { FactoryBot.build(:location, created_at: previous_collection_start_date - 2.years) } + + before do + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 5.months, location:) + end + + context "when reactivation date is nil" do + let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now, location:) } + + it "adds an error" do + validator.validate(record) + expect(record.errors.count).to eq(1) + expect(record.errors[:reactivation_date_type]).to include("Select one of the options.") + end + end + + context "when reactivation date is present" do + context "when reactivation date is before the existing period's start" do + let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, location:) } + + it "adds an error" do + validator.validate(record) + expect(record.errors[:reactivation_date].count).to eq(1) + expect(record.errors[:reactivation_date][0]).to match("The reactivation date must be on or after deactivation date.") + end + end + + context "when reactivation date is after the existing period's start" do + let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 6.months, location:) } + + it "does not add an error" do + validator.validate(record) + expect(record.errors).to be_empty + end + end + end + end + + context "when there is not an open deactivation period within six months" do # validate_deactivation + let!(:location) { FactoryBot.create(:location, created_at: previous_collection_start_date - 2.years) } + + before do + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 7.months, reactivation_date: Time.zone.now + 8.months, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 1.month, reactivation_date: Time.zone.now + 2.months, location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 9.months, location:) + end + + context "when reactivation date is nil" do + let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now, location:) } + + it "does not add an error" do + validator.validate(record) + expect(record.errors).to be_empty + end + end + + context "when reactivation date is present" do + context "when deactivation date is less than six months in the future" do + let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, location:) } + + it "does not add an error" do + validator.validate(record) + expect(record.errors).to be_empty + end + end + + context "when deactivation date is more than six months in the future" do + let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 9.months, reactivation_date: Time.zone.now + 10.months, location:) } + + it "does not add an error" do + validator.validate(record) + expect(record.errors).to be_empty + end + end + end + end end end diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index 6d876381d..a98487d86 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -1082,6 +1082,16 @@ RSpec.describe Location, type: :model do it "returns active if the location has no relevant deactivation records" do expect(location.status_at(Time.zone.today - 2.months)).to eq(:active) end + + context "when the most recently created deactivation is not the current one" do + before do + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today - 80.days, reactivation_date: Time.zone.today - 70.days, location:) + end + + it "returns reactivating_soon" do + expect(location.status_at(Time.zone.today - 3.days)).to eq(:reactivating_soon) + end + end end end diff --git a/spec/models/scheme_deactivation_period_spec.rb b/spec/models/scheme_deactivation_period_spec.rb index 5d4e6f6e2..d050f7132 100644 --- a/spec/models/scheme_deactivation_period_spec.rb +++ b/spec/models/scheme_deactivation_period_spec.rb @@ -4,8 +4,6 @@ RSpec.describe SchemeDeactivationPeriod do let(:validator) { SchemeDeactivationPeriodValidator.new } let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) } let(:current_collection_start_date) { Time.zone.local(2023, 4, 1) } - let(:scheme) { FactoryBot.create(:scheme, created_at: previous_collection_start_date - 2.years) } - let(:record) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: current_collection_start_date, scheme:) } describe "#validate" do before do @@ -14,6 +12,9 @@ RSpec.describe SchemeDeactivationPeriod do end context "when not in a crossover period" do + let(:scheme) { FactoryBot.create(:scheme, created_at: previous_collection_start_date - 2.years) } + let(:record) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: current_collection_start_date, scheme:) } + before do allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false) end @@ -38,6 +39,9 @@ RSpec.describe SchemeDeactivationPeriod do end context "when in a crossover period" do + let(:scheme) { FactoryBot.create(:scheme, created_at: previous_collection_start_date - 2.years) } + let(:record) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: current_collection_start_date, scheme:) } + before do allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) end @@ -69,5 +73,83 @@ RSpec.describe SchemeDeactivationPeriod do end end end + + context "when there is an open deactivation period less than six months in the future" do # validate_reactivation + let!(:scheme) { FactoryBot.build(:scheme, created_at: previous_collection_start_date - 2.years) } + + before do + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.now + 5.months, scheme:) + end + + context "when reactivation date is nil" do + let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now, scheme:) } + + it "adds an error" do + validator.validate(record) + expect(record.errors.count).to eq(1) + expect(record.errors[:reactivation_date_type]).to include("Select one of the options.") + end + end + + context "when reactivation date is present" do + context "when reactivation date is before the existing period's start" do + let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, scheme:) } + + it "adds an error" do + validator.validate(record) + expect(record.errors[:reactivation_date].count).to eq(1) + expect(record.errors[:reactivation_date][0]).to match("The reactivation date must be on or after deactivation date.") + end + end + + context "when reactivation date is after the existing period's start" do + let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 6.months, scheme:) } + + it "does not add an error" do + validator.validate(record) + expect(record.errors).to be_empty + end + end + end + end + + context "when there is not an open deactivation period within six months" do # validate_deactivation + let!(:scheme) { FactoryBot.create(:scheme, created_at: previous_collection_start_date - 2.years) } + + before do + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.now + 7.months, reactivation_date: Time.zone.now + 8.months, scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.now + 1.month, reactivation_date: Time.zone.now + 2.months, scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.now + 9.months, scheme:) + end + + context "when reactivation date is nil" do + let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now, scheme:) } + + it "does not add an error" do + validator.validate(record) + expect(record.errors).to be_empty + end + end + + context "when reactivation date is present" do + context "when deactivation date is less than six months in the future" do + let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, scheme:) } + + it "does not add an error" do + validator.validate(record) + expect(record.errors).to be_empty + end + end + + context "when deactivation date is more than six months in the future" do + let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now + 9.months, reactivation_date: Time.zone.now + 10.months, scheme:) } + + it "does not add an error" do + validator.validate(record) + expect(record.errors).to be_empty + end + end + end + end end end diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index a0afad6fa..72c7595a5 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -470,6 +470,16 @@ RSpec.describe Scheme, type: :model do it "returns active if the scheme has no relevant deactivation records" do expect(scheme.status_at(Time.zone.today - 1.month)).to eq(:active) end + + context "when the most recently created deactivation is not the current one" do + before do + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 300.days, reactivation_date: Time.zone.today - 200.days, scheme:) + end + + it "returns reactivating_soon" do + expect(scheme.status_at(Time.zone.today - 3.days)).to eq(:reactivating_soon) + end + end end end diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index 1104cc23d..eeac39d1d 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -512,6 +512,47 @@ RSpec.describe Validations::SetupValidations do end end + context "with a scheme whose chronologically latest deactivation period is not most recently created" do + let(:scheme) { create(:scheme) } + + before do + create(:location, scheme:) + scheme_deactivation_period = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:) + scheme_deactivation_period_2 = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 2, 4), reactivation_date: Time.zone.local(2022, 3, 4), scheme:) + scheme_deactivation_period.save!(validate: false) + scheme_deactivation_period_2.save!(validate: false) + scheme.reload + end + + it "produces error when tenancy start date is during later deactivated scheme period" do + record.startdate = Time.zone.local(2022, 7, 5) + record.scheme = scheme + setup_validator.validate_scheme(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.lettings.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "3 August 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.lettings.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "3 August 2022")) + end + + it "produces error when tenancy start date is during earlier deactivated scheme period" do + record.startdate = Time.zone.local(2022, 2, 5) + record.scheme = scheme + setup_validator.validate_scheme(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.lettings.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 March 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.lettings.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 March 2022")) + end + + it "produces no error when tenancy start date is during an active scheme period" do + record.startdate = Time.zone.local(2022, 10, 1) + record.scheme = scheme + setup_validator.validate_scheme(record) + expect(record.errors["startdate"]).to be_empty + expect(record.errors["scheme_id"]).to be_empty + end + end + context "with a scheme with no locations active on the start date & no location set" do let(:scheme) { create(:scheme) } let(:location) { create(:location, scheme:) } diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index e8024bf76..cf8959510 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -2169,6 +2169,29 @@ RSpec.describe LocationsController, type: :request do expect(page).to have_content(I18n.t("validations.location.reactivation.before_deactivation", date: "10 October 2022")) end end + + context "when there is no open deactivation period" do + let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "8/9/2022" } } } + + before do + location.location_deactivation_periods.clear + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 1), reactivation_date: Time.zone.local(2022, 7, 5), updated_at: Time.zone.local(2000, 1, 1), location:) + create(:location_deactivation_period, deactivation_date: Time.zone.local(2023, 1, 1), reactivation_date: Time.zone.local(2023, 4, 5), updated_at: Time.zone.local(2000, 1, 1), location:) + location.save! + patch "/schemes/#{scheme.id}/locations/#{location.id}/reactivate", params: + end + + it "renders not found" do + expect(response).to have_http_status(:not_found) + end + + it "does not update deactivation periods" do + location.reload + expect(location.location_deactivation_periods.count).to eq(2) + expect(location.location_deactivation_periods[0].updated_at).to eq(Time.zone.local(2000, 1, 1)) + expect(location.location_deactivation_periods[1].updated_at).to eq(Time.zone.local(2000, 1, 1)) + end + end end end diff --git a/spec/requests/sales_logs_controller_spec.rb b/spec/requests/sales_logs_controller_spec.rb index e85ecb813..8a65e66d5 100644 --- a/spec/requests/sales_logs_controller_spec.rb +++ b/spec/requests/sales_logs_controller_spec.rb @@ -341,13 +341,6 @@ RSpec.describe SalesLogsController, type: :request do end context "with year filter" do - around do |example| - Timecop.freeze(2022, 12, 1) do - example.run - end - Timecop.return - end - before do Timecop.freeze(2022, 4, 1) sales_log_2022.update!(saledate: Time.zone.local(2022, 4, 1)) @@ -386,13 +379,6 @@ RSpec.describe SalesLogsController, type: :request do end context "with year and status filter" do - around do |example| - Timecop.freeze(2022, 12, 1) do - example.run - end - Timecop.return - end - before do Timecop.freeze(2022, 4, 1) sales_log_2022.update!(saledate: Time.zone.local(2022, 4, 1)) diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index e5a96992f..5da12a36c 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -2850,15 +2850,13 @@ RSpec.describe SchemesController, type: :request do end end - context "and there already is a deactivation period" do - let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) } - + context "and there already is an open deactivation period" do before do create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) patch "/schemes/#{scheme.id}/deactivate", params: end - it "updates existing scheme with valid deactivation date and renders scheme page" do + it "updates existing period with valid deactivation date and renders scheme page" do follow_redirect! follow_redirect! expect(response).to have_http_status(:ok) @@ -2868,10 +2866,37 @@ RSpec.describe SchemesController, type: :request do expect(scheme.scheme_deactivation_periods.first.deactivation_date).to eq(deactivation_date) end - it "clears the scheme and scheme answers" do + it "clears the scheme answer" do expect(lettings_log.scheme).to eq(scheme) lettings_log.reload expect(lettings_log.scheme).to eq(nil) + end + + it "marks log as needing attention" do + expect(lettings_log.unresolved).to eq(nil) + lettings_log.reload + expect(lettings_log.unresolved).to eq(true) + end + end + + context "and there already is a closed deactivation period" do + before do + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: Time.zone.local(2023, 5, 5), scheme:) + patch "/schemes/#{scheme.id}/deactivate", params: + end + + it "creates new deactivation period with valid deactivation date and renders scheme page" do + follow_redirect! + follow_redirect! + expect(response).to have_http_status(:ok) + scheme.reload + expect(scheme.scheme_deactivation_periods.count).to eq(2) + expect(scheme.scheme_deactivation_periods.map(&:deactivation_date)).to include(deactivation_date) + end + + it "clears the scheme answer" do + expect(lettings_log.scheme).to eq(scheme) + lettings_log.reload expect(lettings_log.scheme).to eq(nil) end @@ -2967,7 +2992,6 @@ RSpec.describe SchemesController, type: :request do end context "when there is a later open deactivation" do - let(:deactivation_date) { Time.zone.local(2022, 10, 10) } let(:params) { { scheme_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "8/9/2022" } } } let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) } @@ -2980,6 +3004,214 @@ RSpec.describe SchemesController, type: :request do end end + describe "#reactivate" do + context "when not signed in" do + it "redirects to the sign in page" do + patch "/schemes/1/reactivate" + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when signed in as a data provider" do + let(:user) { create(:user) } + let(:scheme) { create(:scheme, owning_organisation: user.organisation) } + + before do + sign_in user + patch "/schemes/#{scheme.id}/reactivate" + end + + it "returns 401 unauthorized" do + expect(response).to be_unauthorized + end + end + + context "when signed in as a data coordinator" do + let(:user) { create(:user, :data_coordinator) } + let!(:scheme) { create(:scheme, owning_organisation: user.organisation, created_at: Time.zone.local(2023, 10, 11)) } + let(:deactivation_date) { Time.utc(2022, 10, 10) } + let(:startdate) { Time.utc(2022, 10, 11) } + let(:params) { { scheme_deactivation_period: { reactivation_date: "5/8/2023", reactivation_date_type: "other" } } } + + let(:add_deactivations) {} + + before do + allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true) + Timecop.freeze(Time.utc(2023, 10, 10)) + sign_in user + add_deactivations + scheme.save! + get "/schemes/#{scheme.id}/new-reactivation" + end + + after do + Timecop.unfreeze + end + + context "when there is no open deactivation period" do + let(:add_deactivations) do + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 4, 1), reactivation_date: Time.zone.local(2023, 5, 5), updated_at: Time.zone.local(2000, 1, 1), scheme:) + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2024, 1, 1), reactivation_date: Time.zone.local(2026, 4, 5), updated_at: Time.zone.local(2000, 1, 1), scheme:) + end + + it "renders not found" do + expect(response).to have_http_status(:not_found) + end + + it "does not update deactivation periods" do + scheme.reload + expect(scheme.scheme_deactivation_periods.count).to eq(2) + expect(scheme.scheme_deactivation_periods[0].updated_at).to eq(Time.zone.local(2000, 1, 1)) + expect(scheme.scheme_deactivation_periods[1].updated_at).to eq(Time.zone.local(2000, 1, 1)) + end + end + + context "when there is an open deactivation period starting after reactivation date" do + let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 9, 15), reactivation_date: nil, updated_at: Time.zone.local(2000, 1, 1), scheme:) } + + before do + patch "/schemes/#{scheme.id}/reactivate", params: + end + + it "shows an unprocessable content error" do + expect(response).to have_http_status(:unprocessable_content) + expect(page).to have_content(I18n.t("validations.scheme.reactivation.before_deactivation", date: "15 September 2023")) + end + + it "does not update the deactivation period" do + scheme.reload + expect(scheme.scheme_deactivation_periods.count).to eq(1) + expect(scheme.scheme_deactivation_periods[0].updated_at).to eq(Time.zone.local(2000, 1, 1)) + end + end + + context "when there is an open deactivation period starting before reactivation date" do + let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 7, 15), reactivation_date: nil, scheme:) } + + before do + patch "/schemes/#{scheme.id}/reactivate", params: + end + + it "redirects to scheme page" do + follow_redirect! + follow_redirect! + expect(response).to have_http_status(:ok) + expect(path).to match("/schemes/#{scheme.id}") + end + + it "ends the existing deactivation period" do + scheme.reload + expect(scheme.scheme_deactivation_periods.count).to eq(1) + expect(scheme.scheme_deactivation_periods.first.deactivation_date).to eq(Time.zone.local(2023, 7, 15)) + expect(scheme.scheme_deactivation_periods.first.reactivation_date).to eq(Time.zone.local(2023, 8, 5)) + end + + context "with default date" do + let(:add_deactivations) {} + let(:params) { { scheme_deactivation_period: { reactivation_date_type: "default" } } } + + before do + allow(FormHandler.instance).to receive(:current_collection_start_year).and_return(2023) + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 9, 15), reactivation_date: nil, scheme:) + allow(FormHandler.instance).to receive(:current_collection_start_year).and_return(2024) + patch "/schemes/#{scheme.id}/reactivate", params: + end + + it "redirects to the scheme details page" do + expect(response).to redirect_to("/schemes/#{scheme.id}/details") + follow_redirect! + follow_redirect! + expect(response).to have_http_status(:ok) + end + + it "updates existing scheme deactivations with valid reactivation date" do + follow_redirect! + scheme.reload + expect(scheme.scheme_deactivation_periods.count).to eq(1) + expect(scheme.scheme_deactivation_periods.first.reactivation_date).to eq(Time.zone.local(2024, 4, 1)) + end + end + + context "with other date" do + let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "10/9/2023" } } } + + it "redirects to the scheme details page" do + expect(response).to redirect_to("/schemes/#{scheme.id}/details") + end + + it "updates existing scheme deactivations with valid reactivation date" do + follow_redirect! + scheme.reload + expect(scheme.scheme_deactivation_periods.count).to eq(1) + expect(scheme.scheme_deactivation_periods.first.reactivation_date).to eq(Time.zone.local(2023, 9, 10)) + end + end + + context "with other future date" do + let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "14/12/2099" } } } + + it "redirects to the scheme details page" do + expect(response).to redirect_to("/schemes/#{scheme.id}/details") + end + end + + context "when the date is not selected" do + let(:params) { { scheme_deactivation_period: { "reactivation_date": "" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_content) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.not_selected")) + end + end + + context "when invalid date is entered" do + let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "10/44/2022" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_content) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid")) + end + end + + context "when the date is entered is before the beginning of current collection window" do + let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "10/4/2020" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_content) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.out_of_range", date: "1 April 2022")) + end + end + + context "when the day is not entered" do + let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "/2/2022" } } } + + it "displays page with an error message" do + expect(response).to have_http_status(:unprocessable_content) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid")) + end + end + + context "when the month is not entered" do + let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "2//2022" } } } + + it "displays page with an error message" do + expect(response).to have_http_status(:unprocessable_content) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid")) + end + end + + context "when the year is not entered" do + let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "2/2/" } } } + + it "displays page with an error message" do + expect(response).to have_http_status(:unprocessable_content) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid")) + end + end + end + end + end + describe "#delete-confirmation" do let(:scheme) { create(:scheme, owning_organisation: user.organisation) }