Browse Source

CLDC-1911 Initialise missing forms using the new format (#1263)

* initialise missing forms using the new format, disable 14 days validation on non production

* Use only current and previous forms where needed

* refactor

* Refactor

* Remove startdate collection window validation from non production
pull/1282/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
a52b97b866
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      app/models/form.rb
  2. 19
      app/models/form_handler.rb
  3. 2
      app/models/forms/bulk_upload_lettings/year.rb
  4. 10
      app/models/validations/date_validations.rb
  5. 6
      config/initializers/feature_toggle.rb
  6. 41
      spec/models/form_handler_spec.rb

11
app/models/form.rb

@ -4,10 +4,10 @@ class Form
:setup_sections, :form_sections, :unresolved_log_redirect_page_id :setup_sections, :form_sections, :unresolved_log_redirect_page_id
def initialize(form_path, start_year = "", sections_in_form = [], type = "lettings") def initialize(form_path, start_year = "", sections_in_form = [], type = "lettings")
if type == "sales" if sales_or_start_year_after_2022?(type, start_year)
@setup_sections = [Form::Sales::Sections::Setup.new(nil, nil, self)] @setup_sections = type == "sales" ? [Form::Sales::Sections::Setup.new(nil, nil, self)] : [Form::Lettings::Sections::Setup.new(nil, nil, self)]
@form_sections = sections_in_form.map { |sec| sec.new(nil, nil, self) } @form_sections = sections_in_form.map { |sec| sec.new(nil, nil, self) }
@type = "sales" @type = type
@sections = setup_sections + form_sections @sections = setup_sections + form_sections
@subsections = sections.flat_map(&:subsections) @subsections = sections.flat_map(&:subsections)
@pages = subsections.flat_map(&:pages) @pages = subsections.flat_map(&:pages)
@ -20,6 +20,7 @@ class Form
"end_date" => end_date, "end_date" => end_date,
"sections" => sections, "sections" => sections,
} }
@unresolved_log_redirect_page_id = "tenancy_start_date" if type == "lettings"
else else
raise "No form definition file exists for given year".freeze unless File.exist?(form_path) raise "No form definition file exists for given year".freeze unless File.exist?(form_path)
@ -240,4 +241,8 @@ class Form
def valid_start_date_for_form?(start_date) def valid_start_date_for_form?(start_date)
start_date >= self.start_date && start_date <= end_date start_date >= self.start_date && start_date <= end_date
end end
def sales_or_start_year_after_2022?(type, start_year)
type == "sales" || (start_year && start_year.to_i > 2022)
end
end end

19
app/models/form_handler.rb

@ -28,8 +28,10 @@ class FormHandler
] ]
current_form = Form.new(nil, current_collection_start_year, sales_sections, "sales") current_form = Form.new(nil, current_collection_start_year, sales_sections, "sales")
previous_form = Form.new(nil, current_collection_start_year - 1, sales_sections, "sales") previous_form = Form.new(nil, current_collection_start_year - 1, sales_sections, "sales")
{ "current_sales" => current_form, {
"previous_sales" => previous_form } "current_sales" => current_form,
"previous_sales" => previous_form,
}
end end
def lettings_forms def lettings_forms
@ -42,6 +44,19 @@ class FormHandler
forms[form_to_set] = form if forms[form_to_set].blank? forms[form_to_set] = form if forms[form_to_set].blank?
end end
end end
lettings_sections = [
Form::Lettings::Sections::TenancyAndProperty,
Form::Lettings::Sections::Household,
Form::Lettings::Sections::RentAndCharges,
]
if forms["previous_lettings"].blank? && current_collection_start_year >= 2022
forms["previous_lettings"] = Form.new(nil, current_collection_start_year - 1, lettings_sections, "lettings")
end
forms["current_lettings"] = Form.new(nil, current_collection_start_year, lettings_sections, "lettings") if forms["current_lettings"].blank?
forms["next_lettings"] = Form.new(nil, current_collection_start_year + 1, lettings_sections, "lettings") if forms["next_lettings"].blank?
forms forms
end end

2
app/models/forms/bulk_upload_lettings/year.rb

@ -34,7 +34,7 @@ module Forms
private private
def possible_years def possible_years
FormHandler.instance.lettings_forms.values.map { |form| form.start_date.year }.sort.reverse [FormHandler.instance.lettings_forms["current_lettings"].start_date.year, FormHandler.instance.lettings_forms["previous_lettings"].start_date.year]
end end
end end
end end

10
app/models/validations/date_validations.rb

@ -39,7 +39,7 @@ module Validations::DateValidations
record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") record.errors.add :startdate, I18n.t("validations.date.outside_collection_window")
end end
if record.startdate < first_collection_start_date || record.startdate > second_collection_end_date if (record.startdate < first_collection_start_date || record.startdate > second_collection_end_date) && FeatureToggle.startdate_collection_window_validation_enabled?
record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") record.errors.add :startdate, I18n.t("validations.date.outside_collection_window")
end end
@ -69,19 +69,19 @@ module Validations::DateValidations
private private
def first_collection_start_date def first_collection_start_date
@first_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form.start_date }.compact.min @first_collection_start_date ||= FormHandler.instance.lettings_forms["previous_lettings"].start_date
end end
def first_collection_end_date def first_collection_end_date
@first_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form.end_date }.compact.min @first_collection_end_date ||= FormHandler.instance.lettings_forms["previous_lettings"].end_date
end end
def second_collection_start_date def second_collection_start_date
@second_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form.start_date }.compact.max @second_collection_start_date ||= FormHandler.instance.lettings_forms["current_lettings"].start_date
end end
def second_collection_end_date def second_collection_end_date
@second_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form.end_date }.compact.max @second_collection_end_date ||= FormHandler.instance.lettings_forms["current_lettings"].end_date
end end
def is_rsnvac_first_let?(record) def is_rsnvac_first_let?(record)

6
config/initializers/feature_toggle.rb

@ -1,6 +1,10 @@
class FeatureToggle class FeatureToggle
def self.startdate_two_week_validation_enabled? def self.startdate_two_week_validation_enabled?
true Rails.env.production? || Rails.env.test?
end
def self.startdate_collection_window_validation_enabled?
Rails.env.production? || Rails.env.test?
end end
def self.sales_log_enabled? def self.sales_log_enabled?

41
spec/models/form_handler_spec.rb

@ -168,5 +168,46 @@ RSpec.describe FormHandler do
end end
end end
end end
describe "lettings_forms" do
context "when current and previous forms are defined in JSON (current collection start year before 2023)" do
let(:now) { Time.utc(2022, 9, 20) }
it "creates a next_lettings form from ruby form objects" do
expect(form_handler.lettings_forms["previous_lettings"]).to be_present
expect(form_handler.lettings_forms["previous_lettings"].start_date.year).to eq(2021)
expect(form_handler.lettings_forms["current_lettings"]).to be_present
expect(form_handler.lettings_forms["current_lettings"].start_date.year).to eq(2022)
expect(form_handler.lettings_forms["next_lettings"]).to be_present
expect(form_handler.lettings_forms["next_lettings"].start_date.year).to eq(2023)
end
end
context "when only previous form is defined in JSON (current collection start year 2023)" do
let(:now) { Time.utc(2023, 9, 20) }
it "creates current_lettings and next_lettings forms from ruby form objects" do
expect(form_handler.lettings_forms["previous_lettings"]).to be_present
expect(form_handler.lettings_forms["previous_lettings"].start_date.year).to eq(2022)
expect(form_handler.lettings_forms["current_lettings"]).to be_present
expect(form_handler.lettings_forms["current_lettings"].start_date.year).to eq(2023)
expect(form_handler.lettings_forms["next_lettings"]).to be_present
expect(form_handler.lettings_forms["next_lettings"].start_date.year).to eq(2024)
end
end
context "when no form is defined in JSON (current collection start year 2024 onwards)" do
let(:now) { Time.utc(2024, 9, 20) }
it "creates previous_lettings, current_lettings and next_lettings forms from ruby form objects" do
expect(form_handler.lettings_forms["previous_lettings"]).to be_present
expect(form_handler.lettings_forms["previous_lettings"].start_date.year).to eq(2023)
expect(form_handler.lettings_forms["current_lettings"]).to be_present
expect(form_handler.lettings_forms["current_lettings"].start_date.year).to eq(2024)
expect(form_handler.lettings_forms["next_lettings"]).to be_present
expect(form_handler.lettings_forms["next_lettings"].start_date.year).to eq(2025)
end
end
end
# rubocop:enable RSpec/PredicateMatcher # rubocop:enable RSpec/PredicateMatcher
end end

Loading…
Cancel
Save