Browse Source

CLDC-1770 Activating soon locations validations (#1739)

* feat: update locations and schemes displayed

* feat: update tenancy/scheme/location validations

* feat: replace case block with hash lookpu

* feat: update tests

* feat: add tests for displayed_answer_options

* feat: update validation messages

* feat: update validation messages

* feat: update tests

* feat: tech review updates
pull/1770/head
natdeanlewissoftwire 1 year ago committed by GitHub
parent
commit
22b31dce6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/derived_variables/lettings_log_variables.rb
  2. 3
      app/models/form/lettings/questions/location_id.rb
  3. 3
      app/models/form/lettings/questions/scheme_id.rb
  4. 2
      app/models/location.rb
  5. 4
      app/models/validations/date_validations.rb
  6. 6
      app/models/validations/setup_validations.rb
  7. 11
      app/models/validations/shared_validations.rb
  8. 21
      config/locales/en.yml
  9. 16
      spec/models/form/lettings/questions/location_id_spec.rb
  10. 34
      spec/models/form/lettings/questions/scheme_id_spec.rb
  11. 72
      spec/models/validations/date_validations_spec.rb
  12. 228
      spec/models/validations/setup_validations_spec.rb

4
app/models/derived_variables/lettings_log_variables.rb

@ -32,7 +32,7 @@ module DerivedVariables::LettingsLogVariables
def scheme_has_multiple_locations?
return false unless scheme
@scheme_locations_count ||= scheme.locations.active.size
@scheme_locations_count ||= scheme.locations.active_in_2_weeks.size
@scheme_locations_count > 1
end
@ -258,7 +258,7 @@ private
def reset_scheme_location!
self.location = nil
if scheme && scheme.locations.active.size == 1
if scheme && scheme.locations.active_in_2_weeks.size == 1
self.location = scheme.locations.first
end
end

3
app/models/form/lettings/questions/location_id.rb

@ -19,8 +19,7 @@ class Form::Lettings::Questions::LocationId < ::Form::Question
answer_opts = {}
return answer_opts unless ActiveRecord::Base.connected?
Location.select(:id, :postcode, :name).where("startdate <= ? or startdate IS NULL",
Time.zone.today).each_with_object(answer_opts) do |location, hsh|
Location.started_in_2_weeks.select(:id, :postcode, :name).each_with_object(answer_opts) do |location, hsh|
hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name }
hsh
end

3
app/models/form/lettings/questions/scheme_id.rb

@ -35,8 +35,7 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question
else
Scheme.includes(:locations).select(:id).where(confirmed: true)
end
filtered_scheme_ids = schemes.joins(:locations).merge(Location.where("startdate <= ? or startdate IS NULL",
Time.zone.today)).map(&:id)
filtered_scheme_ids = schemes.joins(:locations).merge(Location.started_in_2_weeks).map(&:id)
answer_options.select do |k, _v|
filtered_scheme_ids.include?(k.to_i) || k.blank?
end

2
app/models/location.rb

@ -23,6 +23,8 @@ class Location < ApplicationRecord
scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) }
scope :started, -> { where("startdate <= ?", Time.zone.today).or(where(startdate: nil)) }
scope :active, -> { where(confirmed: true).and(started) }
scope :started_in_2_weeks, -> { where("startdate <= ?", Time.zone.today + 2.weeks).or(where(startdate: nil)) }
scope :active_in_2_weeks, -> { where(confirmed: true).and(started_in_2_weeks) }
scope :confirmed, -> { where(confirmed: true) }
scope :unconfirmed, -> { where.not(confirmed: true) }

4
app/models/validations/date_validations.rb

@ -54,8 +54,8 @@ module Validations::DateValidations
record.errors.add :startdate, I18n.t("validations.setup.startdate.ten_years_after_mrc_date")
end
location_during_startdate_validation(record, :startdate)
scheme_during_startdate_validation(record, :startdate)
location_during_startdate_validation(record)
scheme_during_startdate_validation(record)
end
private

6
app/models/validations/setup_validations.rb

@ -23,7 +23,7 @@ module Validations::SetupValidations
end
def validate_location(record)
location_during_startdate_validation(record, :location_id)
location_during_startdate_validation(record)
end
def validate_scheme_has_confirmed_locations_validation(record)
@ -35,8 +35,8 @@ module Validations::SetupValidations
end
def validate_scheme(record)
location_during_startdate_validation(record, :scheme_id)
scheme_during_startdate_validation(record, :scheme_id)
location_during_startdate_validation(record)
scheme_during_startdate_validation(record)
end
def validate_organisation(record)

11
app/models/validations/shared_validations.rb

@ -64,20 +64,23 @@ module Validations::SharedValidations
end
end
def location_during_startdate_validation(record, field)
def location_during_startdate_validation(record)
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 field, :not_active, message: I18n.t("validations.setup.startdate.location.#{scope}", postcode: record.location.postcode, date:, deactivation_date:)
record.errors.add :startdate, :not_active, message: I18n.t("validations.setup.startdate.location.#{scope}.startdate", postcode: record.location.postcode, date:, deactivation_date:)
record.errors.add :location_id, :not_active, message: I18n.t("validations.setup.startdate.location.#{scope}.location_id", postcode: record.location.postcode, date:, deactivation_date:)
record.errors.add :scheme_id, :not_active, message: I18n.t("validations.setup.startdate.location.#{scope}.location_id", postcode: record.location.postcode, date:, deactivation_date:)
end
end
def scheme_during_startdate_validation(record, field)
def scheme_during_startdate_validation(record)
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 field, I18n.t("validations.setup.startdate.scheme.#{scope}", name: record.scheme.service_name, date:, deactivation_date:)
record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme.#{scope}.startdate", name: record.scheme.service_name, date:, deactivation_date:)
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme.#{scope}.scheme_id", name: record.scheme.service_name, date:, deactivation_date:)
end
end

21
config/locales/en.yml

@ -248,13 +248,22 @@ en:
ten_years_after_mrc_date: "Enter a tenancy start date that is no more than 10 years after the major repairs completion date"
location:
deactivated: "The location %{postcode} was deactivated on %{date} and was not available on the day you entered."
reactivating_soon: "The location %{postcode} is not available until %{date}. Select another location or edit the tenancy start date"
activating_soon: "The location %{postcode} is not available until %{date}. Enter a tenancy start date after %{date}"
deactivated:
startdate: "The location %{postcode} was deactivated on %{date} and was not available on the day you entered. Select another location or edit the tenancy start date"
location_id: "The location %{postcode} was deactivated on %{date} and was not available on the day you entered. Select another location or edit the tenancy start date"
activating_soon:
startdate: "The location %{postcode} is not available until %{date}. Enter a tenancy start date after %{date}"
location_id: "The location %{postcode} is not available until %{date}. Select another location or edit the tenancy start date"
reactivating_soon:
startdate: "The location %{postcode} is not available until %{date}. Enter a tenancy start date after %{date}"
location_id: "The location %{postcode} is not available until %{date}. Select another location or edit the tenancy start date"
scheme:
deactivated: "%{name} was deactivated on %{date} and was not available on the day you entered"
reactivating_soon: "The scheme %{name} is not available until %{date}. Select another scheme or edit the tenancy start date"
activating_soon: "%{name} is not available until %{date}. Enter a tenancy start date after %{date}"
deactivated:
startdate: "The scheme %{name} was deactivated on %{date} and was not available on the day you entered. Select another scheme or edit the tenancy start date"
scheme_id: "The scheme %{name} was deactivated on %{date} and was not available on the day you entered. Select another scheme or edit the tenancy start date"
reactivating_soon:
startdate: "The scheme %{name} is not available until %{date}. Enter a tenancy start date after %{date}"
scheme_id: "The scheme %{name} is not available until %{date}. Select another scheme or edit the tenancy start date"
owning_organisation:
invalid: "Please select the owning organisation or managing organisation that you belong to"
data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the owning organisation."

16
spec/models/form/lettings/questions/location_id_spec.rb

@ -68,9 +68,9 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do
Timecop.unfreeze
end
context "and all the locations have a future startdate" do
context "and all the locations have a startdate more than 2 weeks in the future" do
before do
FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 5, 13))
FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 6, 1))
FactoryBot.create(:location, scheme:, startdate: Time.utc(2023, 1, 1))
lettings_log.update!(scheme:)
end
@ -80,6 +80,18 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do
end
end
context "and all but one of the locations have a startdate more than 2 weeks in the future" do
before do
FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 5, 13))
FactoryBot.create(:location, scheme:, startdate: Time.utc(2023, 1, 1))
lettings_log.update!(scheme:)
end
it "the displayed_answer_options shows the locations" do
expect(question.displayed_answer_options(lettings_log).count).to eq(1)
end
end
context "and the locations have no startdate" do
before do
FactoryBot.create(:location, scheme:, startdate: nil)

34
spec/models/form/lettings/questions/scheme_id_spec.rb

@ -90,13 +90,37 @@ RSpec.describe Form::Lettings::Questions::SchemeId, type: :model do
end
context "when a scheme with at least 1 location exists" do
before do
FactoryBot.create(:location, scheme:)
context "when the location is active" do
before do
FactoryBot.create(:location, startdate: Time.zone.yesterday, scheme:)
end
it "has the correct answer_options based on the schemes the user's organisation owns or manages" do
expected_answer = { "" => "Select an option", scheme.id.to_s => scheme }
expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer)
end
end
it "has the correct answer_options based on the schemes the user's organisation owns or manages" do
expected_answer = { "" => "Select an option", scheme.id.to_s => scheme }
expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer)
context "when the location is activating soon" do
before do
FactoryBot.create(:location, startdate: Time.zone.tomorrow, scheme:)
end
it "has the correct answer_options based on the schemes the user's organisation owns or manages" do
expected_answer = { "" => "Select an option", scheme.id.to_s => scheme }
expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer)
end
end
context "when the location is activating more than 2 weeks in the future" do
before do
FactoryBot.create(:location, startdate: Time.zone.today + 3.weeks, scheme:)
end
it "has the correct answer_options based on the schemes the user's organisation owns or manages" do
expected_answer = { "" => "Select an option" }
expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer)
end
end
end

72
spec/models/validations/date_validations_spec.rb

@ -78,7 +78,11 @@ RSpec.describe Validations::DateValidations do
record.location = location
date_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022"))
.to include(match I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: location.postcode, date: "4 June 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022"))
end
it "produces no error when tenancy start date is during an active location period" do
@ -86,6 +90,8 @@ RSpec.describe Validations::DateValidations do
record.location = location
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
expect(record.errors["scheme_id"]).to be_empty
end
end
@ -103,7 +109,11 @@ RSpec.describe Validations::DateValidations do
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 August 2022"))
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 August 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022"))
end
it "produces no error when tenancy start date is during an active location period" do
@ -111,6 +121,8 @@ RSpec.describe Validations::DateValidations do
record.location = location
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
expect(record.errors["scheme_id"]).to be_empty
end
end
@ -130,7 +142,11 @@ RSpec.describe Validations::DateValidations do
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"))
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 September 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022"))
end
it "produces no error when tenancy start date is during an active location period" do
@ -138,10 +154,12 @@ RSpec.describe Validations::DateValidations do
record.location = location
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
expect(record.errors["scheme_id"]).to be_empty
end
end
context "with a location with no deactivation periods" do
context "with a location that is activating soon (has no deactivation periods)" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) }
@ -150,6 +168,8 @@ RSpec.describe Validations::DateValidations do
record.location = location
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
expect(record.errors["scheme_id"]).to be_empty
end
it "produces an error when the date is before available_from date" do
@ -157,7 +177,39 @@ RSpec.describe Validations::DateValidations do
record.location = location
date_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022"))
.to include(match I18n.t("validations.setup.startdate.location.activating_soon.startdate", postcode: location.postcode, date: "15 September 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022"))
end
end
context "with a deactivated scheme" do
let(:scheme) { create(:scheme) }
before do
create(:location, scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:)
scheme.reload
end
it "produces error when tenancy start date is during deactivated scheme period" do
record.startdate = Time.zone.local(2022, 7, 5)
record.scheme = scheme
date_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.scheme.deactivated.startdate", name: scheme.service_name, date: "4 June 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.scheme.deactivated.scheme_id", name: scheme.service_name, date: "4 June 2022"))
end
it "produces no error when tenancy start date is during an active scheme period" do
record.startdate = Time.zone.local(2022, 6, 1)
record.scheme = scheme
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["scheme_id"]).to be_empty
end
end
@ -175,7 +227,9 @@ RSpec.describe Validations::DateValidations do
record.scheme = scheme
date_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 August 2022"))
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 August 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 August 2022"))
end
it "produces no error when tenancy start date is during an active scheme period" do
@ -183,6 +237,7 @@ RSpec.describe Validations::DateValidations do
record.scheme = scheme
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["scheme_id"]).to be_empty
end
end
@ -202,7 +257,9 @@ RSpec.describe Validations::DateValidations do
record.scheme = scheme
date_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 September 2022"))
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 September 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 September 2022"))
end
it "produces no error when tenancy start date is during an active scheme period" do
@ -210,6 +267,7 @@ RSpec.describe Validations::DateValidations do
record.scheme = scheme
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["scheme_id"]).to be_empty
end
end
end

228
spec/models/validations/setup_validations_spec.rb

@ -156,125 +156,198 @@ RSpec.describe Validations::SetupValidations do
end
describe "#validate_scheme" do
context "with a deactivated location" do
context "with a deactivated scheme" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:) }
before do
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:)
location.reload
create(:location, scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:)
scheme.reload
end
it "produces error when tenancy start date is during deactivated location period" do
it "produces error when tenancy start date is during deactivated scheme period" do
record.startdate = Time.zone.local(2022, 7, 5)
record.location = location
record.scheme = scheme
setup_validator.validate_scheme(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.scheme.deactivated.startdate", name: scheme.service_name, date: "4 June 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022"))
.to include(match I18n.t("validations.setup.startdate.scheme.deactivated.scheme_id", name: scheme.service_name, date: "4 June 2022"))
end
it "produces no error when tenancy start date is during an active location period" do
it "produces no error when tenancy start date is during an active scheme period" do
record.startdate = Time.zone.local(2022, 6, 1)
record.location = location
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 location that is reactivating soon" do
context "with a scheme that is reactivating soon" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:) }
before do
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:)
location.reload
create(:location, scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:)
scheme.reload
end
it "produces error when tenancy start date is during deactivated location period" do
it "produces error when tenancy start date is during deactivated scheme period" do
record.startdate = Time.zone.local(2022, 7, 5)
record.location = location
record.scheme = scheme
setup_validator.validate_scheme(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 August 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022"))
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 August 2022"))
end
it "produces no error when tenancy start date is during an active location period" do
it "produces no error when tenancy start date is during an active scheme period" do
record.startdate = Time.zone.local(2022, 9, 1)
record.location = location
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 location with no deactivation periods" do
let(:scheme) { create(:scheme, created_at: Time.zone.local(2022, 10, 3)) }
let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) }
context "with a scheme that has many reactivations soon" do
let(:scheme) { create(:scheme) }
it "produces no error" do
record.startdate = Time.zone.local(2022, 10, 15)
record.location = location
before do
create(:location, scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), scheme:)
scheme.reload
end
it "produces error when tenancy start date is during 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.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 September 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 September 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
it "produces an error when the date is before available_from date" do
record.startdate = Time.zone.local(2022, 8, 15)
context "with a deactivated location" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:) }
before do
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 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
setup_validator.validate_scheme(record)
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022"))
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: location.postcode, date: "4 June 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022"))
end
it "produces no error when tenancy start date is during an active location period" do
record.startdate = Time.zone.local(2022, 6, 1)
record.location = location
setup_validator.validate_scheme(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
end
end
context "with a scheme that is reactivating soon" do
let(:scheme) { create(:scheme, created_at: Time.zone.local(2022, 4, 1)) }
context "with a location that is reactivating soon" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:) }
before do
create(:location, scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:)
scheme.reload
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:)
location.reload
end
it "produces error when tenancy start date is during deactivated scheme period" do
it "produces error when tenancy start date is during deactivated location period" do
record.startdate = Time.zone.local(2022, 7, 5)
record.scheme = scheme
record.location = location
setup_validator.validate_scheme(record)
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 August 2022"))
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 August 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022"))
end
it "produces no error when tenancy start date is during an active scheme period" do
it "produces no error when tenancy start date is during an active location period" do
record.startdate = Time.zone.local(2022, 9, 1)
record.scheme = scheme
record.location = location
setup_validator.validate_scheme(record)
expect(record.errors["scheme_id"]).to be_empty
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
end
end
context "with a scheme that has many reactivations soon" do
let(:scheme) { create(:scheme, created_at: Time.zone.local(2022, 4, 1)) }
context "with a location that has many reactivations soon" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:) }
before do
create(:location, scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), scheme:)
scheme.reload
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 scheme period" do
it "produces error when tenancy start date is during deactivated location period" do
record.startdate = Time.zone.local(2022, 7, 5)
record.scheme = scheme
record.location = location
setup_validator.validate_scheme(record)
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 September 2022"))
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 September 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022"))
end
it "produces no error when tenancy start date is during an active scheme period" do
it "produces no error when tenancy start date is during an active location period" do
record.startdate = Time.zone.local(2022, 10, 1)
record.scheme = scheme
record.location = location
setup_validator.validate_scheme(record)
expect(record.errors["scheme_id"]).to be_empty
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
end
end
context "with a location that is activating soon (has no deactivation periods)" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) }
it "produces no error" do
record.startdate = Time.zone.local(2022, 10, 15)
record.location = location
setup_validator.validate_scheme(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
end
it "produces an error when the date is before available_from date" do
record.startdate = Time.zone.local(2022, 8, 15)
record.location = location
setup_validator.validate_scheme(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.activating_soon.startdate", postcode: location.postcode, date: "15 September 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022"))
end
end
end
@ -293,14 +366,17 @@ RSpec.describe Validations::SetupValidations do
record.startdate = Time.zone.local(2022, 7, 5)
record.location = location
setup_validator.validate_location(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: location.postcode, date: "4 June 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022"))
.to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022"))
end
it "produces no error when tenancy start date is during an active location period" do
record.startdate = Time.zone.local(2022, 6, 1)
record.location = location
setup_validator.validate_location(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
end
end
@ -318,19 +394,52 @@ RSpec.describe Validations::SetupValidations do
record.startdate = Time.zone.local(2022, 7, 5)
record.location = location
setup_validator.validate_location(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 August 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022"))
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022"))
end
it "produces no error when tenancy start date is during an active location period" do
record.startdate = Time.zone.local(2022, 9, 1)
record.location = location
setup_validator.validate_location(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
end
end
context "with a location that has many reactivations soon" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:) }
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
setup_validator.validate_location(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 September 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", 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
setup_validator.validate_location(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
end
end
context "with a location with no deactivation periods" do
context "with a location that is activating soon (has no deactivation periods)" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) }
@ -338,6 +447,7 @@ RSpec.describe Validations::SetupValidations do
record.startdate = Time.zone.local(2022, 10, 15)
record.location = location
setup_validator.validate_location(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["location_id"]).to be_empty
end
@ -345,8 +455,10 @@ RSpec.describe Validations::SetupValidations do
record.startdate = Time.zone.local(2022, 8, 15)
record.location = location
setup_validator.validate_location(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location.activating_soon.startdate", postcode: location.postcode, date: "15 September 2022"))
expect(record.errors["location_id"])
.to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022"))
.to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022"))
end
end
end

Loading…
Cancel
Save