Browse Source

Add base errors for start date

pull/1022/head
Kat 4 years ago
parent
commit
bf596f4d24
  1. 7
      app/models/location.rb
  2. 8
      app/models/validations/date_validations.rb
  3. 70
      spec/models/validations/date_validations_spec.rb

7
app/models/location.rb

@ -393,6 +393,13 @@ class Location < ApplicationRecord
status == :reactivating_soon status == :reactivating_soon
end end
def status_during(date)
return :reactivating_soon if location_deactivation_periods.any? { |period| period.reactivation_date.present? && date.between?(period.deactivation_date, period.reactivation_date) } || available_from > date
open_deactivation = location_deactivation_periods.deactivations_without_reactivation.first
return :deactivated if open_deactivation.present? && open_deactivation.deactivation_date < date
end
private private
PIO = PostcodeService.new PIO = PostcodeService.new

8
app/models/validations/date_validations.rb

@ -59,6 +59,14 @@ module Validations::DateValidations
if record["mrcdate"].present? && record.startdate < record["mrcdate"] if record["mrcdate"].present? && record.startdate < record["mrcdate"]
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
if record.location&.status_during(record.startdate) == :deactivated
record.errors.add :startdate, I18n.t("validations.setup.startdate.during_deactivated_location")
end
if record.location&.status_during(record.startdate) == :reactivating_soon
record.errors.add :startdate, I18n.t("validations.setup.startdate.location_reactivating_soon")
end
end end
private private

70
spec/models/validations/date_validations_spec.rb

@ -83,6 +83,76 @@ RSpec.describe Validations::DateValidations do
date_validator.validate_startdate(record) date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
context "with a deactivated location" 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), 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.during_deactivated_location"))
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
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "with a location that is reactivating 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:)
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"))
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
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)) }
it "produces no error" do
record.startdate = Time.zone.local(2022, 10, 15)
record.location = location
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).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
date_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.location_reactivating_soon"))
end
end
end end
describe "major repairs date" do describe "major repairs date" do

Loading…
Cancel
Save