Browse Source

Add validation to scheme_id

pull/1038/head
Kat 4 years ago
parent
commit
b0c013179a
  1. 3
      app/models/location.rb
  2. 8
      app/models/scheme.rb
  3. 19
      app/models/validations/date_validations.rb
  4. 19
      app/models/validations/setup_validations.rb
  5. 50
      spec/models/validations/date_validations_spec.rb
  6. 50
      spec/models/validations/setup_validations_spec.rb

3
app/models/location.rb

@ -394,9 +394,10 @@ class Location < ApplicationRecord
end end
def status_during(date) def status_during(date)
return unless date.present?
closest_reactivation = location_deactivation_periods.reverse.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: closest_reactivation.reactivation_date } if closest_reactivation.present?
return { status: :reactivating_soon, date: available_from } if available_from > date return { status: :reactivating_soon, date: available_from } if available_from.present? && available_from > date
open_deactivation = location_deactivation_periods.deactivations_without_reactivation.first open_deactivation = location_deactivation_periods.deactivations_without_reactivation.first
return { status: :deactivated, date: open_deactivation.deactivation_date } if open_deactivation.present? && open_deactivation.deactivation_date < date return { status: :deactivated, date: open_deactivation.deactivation_date } if open_deactivation.present? && open_deactivation.deactivation_date < date

8
app/models/scheme.rb

@ -233,4 +233,12 @@ class Scheme < ApplicationRecord
def reactivating_soon? def reactivating_soon?
status == :reactivating_soon status == :reactivating_soon
end end
def status_during(date)
closest_reactivation = scheme_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?
open_deactivation = scheme_deactivation_periods.deactivations_without_reactivation.first
return { status: :deactivated, date: open_deactivation.deactivation_date } if open_deactivation.present? && open_deactivation.deactivation_date < date
end
end end

19
app/models/validations/date_validations.rb

@ -60,13 +60,22 @@ module Validations::DateValidations
record.errors.add :startdate, I18n.t("validations.setup.startdate.after_major_repair_date") record.errors.add :startdate, I18n.t("validations.setup.startdate.after_major_repair_date")
end end
status_during_startdate = record.location&.status_during(record.startdate) location_status_during_startdate = record.location&.status_during(record.startdate)
if status_during_startdate.present? && status_during_startdate[:status] == :deactivated if location_status_during_startdate.present? && location_status_during_startdate[:status] == :deactivated
record.errors.add :startdate, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: status_during_startdate[:date].to_formatted_s(:govuk_date)) record.errors.add :startdate, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date))
end end
if status_during_startdate.present? && status_during_startdate[:status] == :reactivating_soon if location_status_during_startdate.present? && location_status_during_startdate[:status] == :reactivating_soon
record.errors.add :startdate, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: status_during_startdate[:date].to_formatted_s(:govuk_date)) record.errors.add :startdate, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date))
end
scheme_status_during_startdate = record.scheme&.status_during(record.startdate)
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :deactivated
record.errors.add :startdate, I18n.t("validations.setup.startdate.during_deactivated_scheme", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date))
end
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :reactivating_soon
record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date))
end end
end end

19
app/models/validations/setup_validations.rb

@ -17,13 +17,22 @@ module Validations::SetupValidations
end end
def validate_scheme(record) def validate_scheme(record)
status_during_startdate = record.location&.status_during(record.startdate) location_status_during_startdate = record.location&.status_during(record.startdate)
if status_during_startdate.present? && status_during_startdate[:status] == :deactivated if location_status_during_startdate.present? && location_status_during_startdate[:status] == :deactivated
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: status_during_startdate[:date].to_formatted_s(:govuk_date)) record.errors.add :scheme_id, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date))
end end
if status_during_startdate.present? && status_during_startdate[:status] == :reactivating_soon if location_status_during_startdate.present? && location_status_during_startdate[:status] == :reactivating_soon
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: status_during_startdate[:date].to_formatted_s(:govuk_date)) record.errors.add :scheme_id, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date))
end
scheme_status_during_startdate = record.scheme&.status_during(record.startdate)
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :deactivated
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.during_deactivated_scheme", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date))
end
if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :reactivating_soon
record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date))
end end
end end

50
spec/models/validations/date_validations_spec.rb

@ -180,6 +180,56 @@ RSpec.describe Validations::DateValidations do
.to include(match I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: location.postcode, date: "15 September 2022")) .to include(match I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: location.postcode, date: "15 September 2022"))
end end
end end
context "with a scheme that is reactivating soon" do
let(:scheme) { create(:scheme) }
before do
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 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_reactivating_soon", name: scheme.service_name, date: "4 August 2022"))
end
it "produces no error when tenancy start date is during an active scheme period" do
record.startdate = Time.zone.local(2022, 9, 1)
record.scheme = scheme
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "with a scheme that has many reactivations soon" do
let(:scheme) { create(:scheme) }
before do
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
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"))
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
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
end
end end
describe "major repairs date" do describe "major repairs date" do

50
spec/models/validations/setup_validations_spec.rb

@ -101,6 +101,56 @@ RSpec.describe Validations::SetupValidations do
.to include(match I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: location.postcode, date: "15 September 2022")) .to include(match I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: location.postcode, date: "15 September 2022"))
end end
end end
context "with a scheme that is reactivating soon" do
let(:scheme) { create(:scheme) }
before do
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 scheme period" do
record.startdate = Time.zone.local(2022, 7, 5)
record.scheme = scheme
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"))
end
it "produces no error when tenancy start date is during an active scheme period" do
record.startdate = Time.zone.local(2022, 9, 1)
record.scheme = scheme
setup_validator.validate_scheme(record)
expect(record.errors["scheme_id"]).to be_empty
end
end
context "with a scheme that has many reactivations soon" do
let(:scheme) { create(:scheme) }
before do
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["scheme_id"])
.to include(match I18n.t("validations.setup.startdate.scheme_reactivating_soon", 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["scheme_id"]).to be_empty
end
end
end end
describe "#validate_location" do describe "#validate_location" do

Loading…
Cancel
Save