Browse Source

Use sales_in_crossover_period?

pull/1317/head
Jack S 3 years ago
parent
commit
bdb5bcd54a
  1. 8
      app/helpers/collection_time_helper.rb
  2. 31
      app/models/validations/sales/setup_validations.rb
  3. 9
      config/initializers/feature_toggle.rb
  4. 6
      config/locales/en.yml
  5. 5
      spec/helpers/tasklist_helper_spec.rb
  6. 29
      spec/models/validations/sales/setup_validations_spec.rb

8
app/helpers/collection_time_helper.rb

@ -24,8 +24,8 @@ module CollectionTimeHelper
Time.zone.local(current_collection_start_year + 1, 3, 31)
end
def next_collection_end_date
current_collection_end_date + 1.year
def previous_collection_end_date
current_collection_end_date - 1.year
end
def next_collection_start_year
@ -35,4 +35,8 @@ module CollectionTimeHelper
def previous_collection_start_year
current_collection_start_year - 1
end
def previous_collection_start_date
current_collection_start_date - 1.year
end
end

31
app/models/validations/sales/setup_validations.rb

@ -5,39 +5,40 @@ module Validations::Sales::SetupValidations
def validate_saledate(record)
return unless record.saledate && date_valid?("saledate", record)
unless record.saledate.between?(current_collection_start_date, active_collection_end_date)
unless record.saledate.between?(active_collection_start_date, current_collection_end_date) || !FeatureToggle.saledate_collection_window_validation_enabled?
record.errors.add :saledate, validation_error_message
end
end
private
def active_collection_end_date
if FeatureToggle.saledate_next_collection_year_validation_enabled?
next_collection_end_date
def active_collection_start_date
if FormHandler.instance.sales_in_crossover_period?
previous_collection_start_date
else
current_collection_end_date
current_collection_start_date
end
end
def validation_error_message
start_date = current_collection_start_date
if FeatureToggle.saledate_next_collection_year_validation_enabled?
current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y")
if FormHandler.instance.sales_in_crossover_period?
I18n.t(
"validations.setup.saledate.current_and_next_financial_year",
current_start_year_short: start_date.strftime("%y"),
"validations.setup.saledate.previous_and_current_financial_year",
previous_start_year_short: previous_collection_start_date.strftime("%y"),
previous_end_year_short: previous_collection_end_date.strftime("%y"),
previous_start_year_long: previous_collection_start_date.strftime("#{previous_collection_start_date.day.ordinalize} %B %Y"),
current_end_year_short: current_collection_end_date.strftime("%y"),
current_start_year_long: start_date.strftime("%Y"),
next_end_year_short: next_collection_end_date.strftime("%y"),
next_end_year_long: next_collection_end_date.strftime("%Y"),
current_end_year_long:,
)
else
I18n.t(
"validations.setup.saledate.current_financial_year",
current_start_year_short: start_date.strftime("%y"),
current_start_year_short: current_collection_start_date.strftime("%y"),
current_end_year_short: current_collection_end_date.strftime("%y"),
current_start_year_long: start_date.strftime("%Y"),
current_end_year_long: current_collection_end_date.strftime("%Y"),
current_start_year_long: current_collection_start_date.strftime("#{current_collection_start_date.day.ordinalize} %B %Y"),
current_end_year_long:,
)
end
end

9
config/initializers/feature_toggle.rb

@ -1,14 +1,15 @@
class FeatureToggle
def self.startdate_two_week_validation_enabled?
# Disable check on preview apps to allow for testing of future forms
def self.saledate_collection_window_validation_enabled?
Rails.env.production? || Rails.env.test? || Rails.env.staging?
end
def self.startdate_collection_window_validation_enabled?
def self.startdate_two_week_validation_enabled?
Rails.env.production? || Rails.env.test? || Rails.env.staging?
end
def self.saledate_next_collection_year_validation_enabled?
!Rails.env.production?
def self.startdate_collection_window_validation_enabled?
Rails.env.production? || Rails.env.test? || Rails.env.staging?
end
def self.sales_log_enabled?

6
config/locales/en.yml

@ -151,9 +151,9 @@ en:
blank: "Enter name of other intermediate rent product"
saledate:
current_financial_year:
Enter a date within the %{current_start_year_short}/%{current_end_year_short} financial year, which is between 1st April %{current_start_year_long} and 31st March %{current_end_year_long}
current_and_next_financial_year:
"Enter a date within the %{current_start_year_short}/%{current_end_year_short} or %{current_end_year_short}/%{next_end_year_short} financial years, which is between 1st April %{current_start_year_long} and 31st March %{next_end_year_long}"
Enter a date within the %{current_start_year_short}/%{current_end_year_short} financial year, which is between %{current_start_year_long} and %{current_end_year_long}
previous_and_current_financial_year:
"Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} financial years, which is between %{previous_start_year_long} and %{current_end_year_long}"
startdate:
later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date"

5
spec/helpers/tasklist_helper_spec.rb

@ -14,7 +14,7 @@ RSpec.describe TasklistHelper do
describe "with lettings" do
let(:empty_lettings_log) { create(:lettings_log) }
let(:lettings_log) { create(:lettings_log, :in_progress, needstype: 1) }
let(:lettings_log) { build(:lettings_log, :in_progress, needstype: 1) }
describe "get next incomplete section" do
it "returns the first subsection name if it is not completed" do
@ -41,7 +41,7 @@ RSpec.describe TasklistHelper do
end
it "returns the number of sections in progress" do
expect(get_subsections_count(lettings_log, :in_progress)).to eq(3)
expect(get_subsections_count(lettings_log, :in_progress)).to eq(2)
end
it "returns 0 for invalid state" do
@ -85,6 +85,7 @@ RSpec.describe TasklistHelper do
end
describe "subsection link" do
let(:lettings_log) { create(:lettings_log, :completed) }
let(:subsection) { lettings_log.form.get_subsection("household_characteristics") }
let(:user) { build(:user) }

29
spec/models/validations/sales/setup_validations_spec.rb

@ -6,11 +6,7 @@ RSpec.describe Validations::Sales::SetupValidations do
let(:validator_class) { Class.new { include Validations::Sales::SetupValidations } }
describe "#validate_saledate" do
context "with saledate_next_collection_year_validation_enabled == true" do
before do
allow(FeatureToggle).to receive(:saledate_next_collection_year_validation_enabled?).and_return(true)
end
context "with sales_in_crossover_period == false" do
context "when saledate is blank" do
let(:record) { build(:sales_log, saledate: nil) }
@ -37,7 +33,7 @@ RSpec.describe Validations::Sales::SetupValidations do
it "adds error" do
setup_validator.validate_saledate(record)
expect(record.errors[:saledate]).to include("Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 31st March 2024")
expect(record.errors[:saledate]).to include("Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023")
end
end
@ -47,14 +43,19 @@ RSpec.describe Validations::Sales::SetupValidations do
it "adds error" do
setup_validator.validate_saledate(record)
expect(record.errors[:saledate]).to include("Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 31st March 2024")
expect(record.errors[:saledate]).to include("Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023")
end
end
end
context "with saledate_next_collection_year_validation_enabled == false" do
before do
allow(FeatureToggle).to receive(:saledate_next_collection_year_validation_enabled?).and_return(false)
context "with sales_in_crossover_period == true" do
around do |example|
Timecop.freeze(Time.zone.local(2024, 5, 1)) do
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
Singleton.__init__(FormHandler)
end
context "when saledate is blank" do
@ -68,7 +69,7 @@ RSpec.describe Validations::Sales::SetupValidations do
end
context "when saledate is in the 22/23 financial year" do
let(:record) { build(:sales_log, saledate: Time.zone.local(2023, 1, 1)) }
let(:record) { build(:sales_log, saledate: Time.zone.local(2024, 1, 1)) }
it "does not add an error" do
setup_validator.validate_saledate(record)
@ -78,12 +79,12 @@ RSpec.describe Validations::Sales::SetupValidations do
end
context "when saledate is before the 22/23 financial year" do
let(:record) { build(:sales_log, saledate: Time.zone.local(2020, 1, 1)) }
let(:record) { build(:sales_log, saledate: Time.zone.local(2020, 5, 1)) }
it "adds error" do
setup_validator.validate_saledate(record)
expect(record.errors[:saledate]).to include("Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023")
expect(record.errors[:saledate]).to include("Enter a date within the 23/24 or 24/25 financial years, which is between 1st April 2023 and 31st March 2025")
end
end
@ -93,7 +94,7 @@ RSpec.describe Validations::Sales::SetupValidations do
it "adds error" do
setup_validator.validate_saledate(record)
expect(record.errors[:saledate]).to include("Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023")
expect(record.errors[:saledate]).to include("Enter a date within the 23/24 or 24/25 financial years, which is between 1st April 2023 and 31st March 2025")
end
end
end

Loading…
Cancel
Save