From 753e1649ac55e102e1b3a077723d8989b2d578ab Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:08:03 +0100 Subject: [PATCH] Allow 4 digit years for start date/sale date --- .../lettings/year2023/csv_parser.rb | 6 ++++-- .../lettings/year2023/row_parser.rb | 19 ++++++------------- .../lettings/year2024/csv_parser.rb | 6 ++++-- .../lettings/year2024/row_parser.rb | 19 ++++++------------- .../bulk_upload/sales/year2023/csv_parser.rb | 6 ++++-- .../bulk_upload/sales/year2023/row_parser.rb | 7 ++++--- .../bulk_upload/sales/year2024/csv_parser.rb | 6 ++++-- .../bulk_upload/sales/year2024/row_parser.rb | 7 ++++--- config/locales/en.yml | 4 ++-- .../lettings/year2023/row_parser_spec.rb | 18 ++++++++++++++---- .../lettings/year2024/row_parser_spec.rb | 18 ++++++++++++++---- .../sales/year2023/row_parser_spec.rb | 15 +++++++++++++-- .../sales/year2024/row_parser_spec.rb | 16 +++++++++++++--- 13 files changed, 92 insertions(+), 55 deletions(-) diff --git a/app/services/bulk_upload/lettings/year2023/csv_parser.rb b/app/services/bulk_upload/lettings/year2023/csv_parser.rb index 64e27108f..96be2256d 100644 --- a/app/services/bulk_upload/lettings/year2023/csv_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/csv_parser.rb @@ -109,9 +109,11 @@ private def first_record_start_date if with_headers? - Date.new(row_parsers.first.field_9.to_i + 2000, row_parsers.first.field_8.to_i, row_parsers.first.field_7.to_i) + year = row_parsers.first.field_9.to_s.strip.length.between?(1, 2) ? row_parsers.first.field_9.to_i + 2000 : row_parsers.first.field_9.to_i + Date.new(year, row_parsers.first.field_8.to_i, row_parsers.first.field_7.to_i) else - Date.new(rows.first[97].to_i + 2000, rows.first[96].to_i, rows.first[95].to_i) + year = rows.first[97].to_s.strip.length.between?(1, 2) ? rows.first[97].to_i + 2000 : rows.first[97].to_i + Date.new(year, rows.first[96].to_i, rows.first[95].to_i) end end end diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 8c435ad24..cc68cabeb 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -323,8 +323,8 @@ class BulkUpload::Lettings::Year2023::RowParser category: :setup, }, format: { - with: /\A\d{2}\z/, - message: I18n.t("validations.setup.startdate.year_not_two_digits"), + with: /\A(\d{2}|\d{4})\z/, + message: I18n.t("validations.setup.startdate.year_not_two_or_four_digits"), category: :setup, unless: -> { field_9.blank? }, }, @@ -618,14 +618,6 @@ private end end - def start_date - return if field_7.blank? || field_8.blank? || field_9.blank? - - Date.parse("20#{field_9.to_s.rjust(2, '0')}-#{field_8}-#{field_7}") - rescue StandardError - nil - end - def validate_no_and_dont_know_disabled_needs_conjunction if field_87 == 1 && field_88 == 1 errors.add(:field_87, I18n.t("validations.household.housingneeds.no_and_dont_know_disabled_needs_conjunction")) @@ -736,9 +728,9 @@ private end def validate_relevant_collection_window - return if start_date.blank? || bulk_upload.form.blank? + return if startdate.blank? || bulk_upload.form.blank? - unless bulk_upload.form.valid_start_date_for_form?(start_date) + unless bulk_upload.form.valid_start_date_for_form?(startdate) errors.add(:field_7, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) errors.add(:field_8, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) errors.add(:field_9, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) @@ -1388,7 +1380,8 @@ private end def startdate - Date.new(field_9 + 2000, field_8, field_7) if field_9.present? && field_8.present? && field_7.present? + year = field_9.to_s.strip.length.between?(1, 2) ? field_9 + 2000 : field_9 + Date.new(year, field_8, field_7) if field_9.present? && field_8.present? && field_7.present? rescue Date::Error Date.new end diff --git a/app/services/bulk_upload/lettings/year2024/csv_parser.rb b/app/services/bulk_upload/lettings/year2024/csv_parser.rb index d8d430755..22caeab02 100644 --- a/app/services/bulk_upload/lettings/year2024/csv_parser.rb +++ b/app/services/bulk_upload/lettings/year2024/csv_parser.rb @@ -112,9 +112,11 @@ private def first_record_start_date if with_headers? - Date.new(row_parsers.first.field_10.to_i + 2000, row_parsers.first.field_9.to_i, row_parsers.first.field_8.to_i) + year = row_parsers.first.field_10.to_s.strip.length.between?(1, 2) ? row_parsers.first.field_10.to_i + 2000 : row_parsers.first.field_10.to_i + Date.new(year, row_parsers.first.field_9.to_i, row_parsers.first.field_8.to_i) else - Date.new(rows.first[9].to_i + 2000, rows.first[8].to_i, rows.first[7].to_i) + year = rows.first[9].to_s.strip.length.between?(1, 2) ? rows.first[9].to_i + 2000 : rows.first[9].to_i + Date.new(year, rows.first[8].to_i, rows.first[7].to_i) end end end diff --git a/app/services/bulk_upload/lettings/year2024/row_parser.rb b/app/services/bulk_upload/lettings/year2024/row_parser.rb index a7eb96c61..4fc612e57 100644 --- a/app/services/bulk_upload/lettings/year2024/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2024/row_parser.rb @@ -324,8 +324,8 @@ class BulkUpload::Lettings::Year2024::RowParser category: :setup, }, format: { - with: /\A\d{2}\z/, - message: I18n.t("validations.setup.startdate.year_not_two_digits"), + with: /\A(\d{2}|\d{4})\z/, + message: I18n.t("validations.setup.startdate.year_not_two_or_four_digits"), category: :setup, unless: -> { field_10.blank? }, }, @@ -686,14 +686,6 @@ private end end - def start_date - return if field_8.blank? || field_9.blank? || field_10.blank? - - Date.parse("20#{field_10.to_s.rjust(2, '0')}-#{field_9}-#{field_8}") - rescue StandardError - nil - end - def validate_no_and_dont_know_disabled_needs_conjunction if field_83 == 1 && field_84 == 1 errors.add(:field_83, I18n.t("validations.household.housingneeds.no_and_dont_know_disabled_needs_conjunction")) @@ -790,9 +782,9 @@ private end def validate_relevant_collection_window - return if start_date.blank? || bulk_upload.form.blank? + return if startdate.blank? || bulk_upload.form.blank? - unless bulk_upload.form.valid_start_date_for_form?(start_date) + unless bulk_upload.form.valid_start_date_for_form?(startdate) errors.add(:field_8, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) errors.add(:field_9, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) errors.add(:field_10, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) @@ -1394,7 +1386,8 @@ private end def startdate - Date.new(field_10 + 2000, field_9, field_8) if field_10.present? && field_9.present? && field_8.present? + year = field_10.to_s.strip.length.between?(1, 2) ? field_10 + 2000 : field_10 + Date.new(year, field_9, field_8) if field_10.present? && field_9.present? && field_8.present? rescue Date::Error Date.new end diff --git a/app/services/bulk_upload/sales/year2023/csv_parser.rb b/app/services/bulk_upload/sales/year2023/csv_parser.rb index ec7346f92..85d455d01 100644 --- a/app/services/bulk_upload/sales/year2023/csv_parser.rb +++ b/app/services/bulk_upload/sales/year2023/csv_parser.rb @@ -111,9 +111,11 @@ private def first_record_start_date if with_headers? - Date.new(row_parsers.first.field_5.to_i + 2000, row_parsers.first.field_4.to_i, row_parsers.first.field_3.to_i) + year = row_parsers.first.field_5.to_s.strip.length.between?(1, 2) ? row_parsers.first.field_5.to_i + 2000 : row_parsers.first.field_5.to_i + Date.new(year, row_parsers.first.field_4.to_i, row_parsers.first.field_3.to_i) else - Date.new(rows.first[3].to_i + 2000, rows.first[2].to_i, rows.first[1].to_i) + year = rows.first[3].to_s.strip.length.between?(1, 2) ? rows.first[3].to_i + 2000 : rows.first[3].to_i + Date.new(year, rows.first[2].to_i, rows.first[1].to_i) end end end diff --git a/app/services/bulk_upload/sales/year2023/row_parser.rb b/app/services/bulk_upload/sales/year2023/row_parser.rb index 6a18512aa..66cf65a52 100644 --- a/app/services/bulk_upload/sales/year2023/row_parser.rb +++ b/app/services/bulk_upload/sales/year2023/row_parser.rb @@ -328,8 +328,8 @@ class BulkUpload::Sales::Year2023::RowParser category: :setup, }, format: { - with: /\A\d{2}\z/, - message: I18n.t("validations.setup.saledate.year_not_two_digits"), + with: /\A(\d{2}|\d{4})\z/, + message: I18n.t("validations.setup.saledate.year_not_two_or_four_digits"), category: :setup, if: proc { field_5.present? }, }, on: :after_log @@ -954,7 +954,8 @@ private end def saledate - Date.new(field_5 + 2000, field_4, field_3) if field_5.present? && field_4.present? && field_3.present? + year = field_5.to_s.strip.length.between?(1, 2) ? field_5 + 2000 : field_5 + Date.new(year, field_4, field_3) if field_5.present? && field_4.present? && field_3.present? rescue Date::Error Date.new end diff --git a/app/services/bulk_upload/sales/year2024/csv_parser.rb b/app/services/bulk_upload/sales/year2024/csv_parser.rb index 9ba99c19b..4a3cb7ac9 100644 --- a/app/services/bulk_upload/sales/year2024/csv_parser.rb +++ b/app/services/bulk_upload/sales/year2024/csv_parser.rb @@ -114,9 +114,11 @@ private def first_record_start_date if with_headers? - Date.new(row_parsers.first.field_6.to_i + 2000, row_parsers.first.field_5.to_i, row_parsers.first.field_4.to_i) + year = row_parsers.first.field_6.to_s.strip.length.between?(1, 2) ? row_parsers.first.field_6.to_i + 2000 : row_parsers.first.field_6.to_i + Date.new(year, row_parsers.first.field_5.to_i, row_parsers.first.field_4.to_i) else - Date.new(rows.first[5].to_i + 2000, rows.first[4].to_i, rows.first[3].to_i) + year = rows.first[5].to_s.strip.length.between?(1, 2) ? rows.first[5].to_i + 2000 : rows.first[5].to_i + Date.new(year, rows.first[4].to_i, rows.first[3].to_i) end end end diff --git a/app/services/bulk_upload/sales/year2024/row_parser.rb b/app/services/bulk_upload/sales/year2024/row_parser.rb index fbc99ba02..667a9b4e0 100644 --- a/app/services/bulk_upload/sales/year2024/row_parser.rb +++ b/app/services/bulk_upload/sales/year2024/row_parser.rb @@ -320,8 +320,8 @@ class BulkUpload::Sales::Year2024::RowParser category: :setup, }, format: { - with: /\A\d{2}\z/, - message: I18n.t("validations.setup.saledate.year_not_two_digits"), + with: /\A(\d{2}|\d{4})\z/, + message: I18n.t("validations.setup.saledate.year_not_two_or_four_digits"), category: :setup, if: proc { field_6.present? }, }, on: :after_log @@ -994,7 +994,8 @@ private end def saledate - Date.new(field_6 + 2000, field_5, field_4) if field_6.present? && field_5.present? && field_4.present? + year = field_6.to_s.strip.length.between?(1, 2) ? field_6 + 2000 : field_6 + Date.new(year, field_5, field_4) if field_6.present? && field_5.present? && field_4.present? rescue Date::Error Date.new end diff --git a/config/locales/en.yml b/config/locales/en.yml index 85be0e677..27c5e0673 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -281,7 +281,7 @@ en: intermediate_rent_product_name: blank: "Enter name of other intermediate rent product." saledate: - year_not_two_digits: "Sale completion year must be 2 digits." + year_not_two_or_four_digits: "Sale completion year must be 2 or 4 digits." type: percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}% for this shared ownership type." @@ -294,7 +294,7 @@ en: before_scheme_end_date: "The tenancy start date must be before the end date for this supported housing scheme." after_void_date: "Enter a tenancy start date that is after the void date." after_major_repair_date: "Enter a tenancy start date that is after the major repair date." - year_not_two_digits: "Tenancy start year must be 2 digits." + year_not_two_or_four_digits: "Tenancy start year must be 2 or 4 digits." ten_years_after_void_date: "Enter a tenancy start date that is no more than 10 years after the void date." ten_years_after_mrc_date: "Enter a tenancy start date that is no more than 10 years after the major repairs completion date." invalid_merged_organisations_start_date: diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index 6d3feffa9..58551e8d9 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -1476,11 +1476,21 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end context "when field_9 is 4 digits instead of 2" do - let(:attributes) { { bulk_upload:, field_9: "2022" } } + let(:attributes) { setup_section_params.merge({ bulk_upload:, field_9: "2023", field_8: "12", field_7: "1" }) } + + it "correctly sets the date" do + parser.valid? + expect(parser.errors[:field_9]).to be_empty + expect(parser.log.startdate).to eq(Date.new(2023, 12, 1)) + end + end + + context "when field_9 is not 4 or 2 digits" do + let(:attributes) { { bulk_upload:, field_9: "202" } } it "returns an error" do parser.valid? - expect(parser.errors[:field_9]).to include("Tenancy start year must be 2 digits.") + expect(parser.errors[:field_9]).to include("Tenancy start year must be 2 or 4 digits.") end end @@ -2824,12 +2834,12 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end end - describe "#start_date" do + describe "#startdate" do context "when year of 9 is passed to represent 2009" do let(:attributes) { { bulk_upload:, field_7: "1", field_8: "1", field_9: "9" } } it "uses the year 2009" do - expect(parser.send(:start_date)).to eql(Date.new(2009, 1, 1)) + expect(parser.send(:startdate)).to eql(Date.new(2009, 1, 1)) end end end diff --git a/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb index 2e4c82635..dbe43bb0d 100644 --- a/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb @@ -1303,11 +1303,21 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do end context "when field_10 is 4 digits instead of 2" do - let(:attributes) { { bulk_upload:, field_10: "2023" } } + let(:attributes) { setup_section_params.merge({ bulk_upload:, field_10: "2024", field_9: "4", field_8: "5" }) } + + it "correctly sets the date" do + parser.valid? + expect(parser.errors[:field_10]).to be_empty + expect(parser.log.startdate).to eq(Time.zone.local(2024, 4, 5)) + end + end + + context "when field_10 is not 4 or 2 digits" do + let(:attributes) { { bulk_upload:, field_10: "204" } } it "returns an error" do parser.valid? - expect(parser.errors[:field_10]).to include("Tenancy start year must be 2 digits.") + expect(parser.errors[:field_10]).to include("Tenancy start year must be 2 or 4 digits.") end end @@ -2945,12 +2955,12 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do end end - describe "#start_date" do + describe "#startdate" do context "when year of 9 is passed to represent 2009" do let(:attributes) { { bulk_upload:, field_8: "1", field_9: "1", field_10: "9" } } it "uses the year 2009" do - expect(parser.send(:start_date)).to eql(Date.new(2009, 1, 1)) + expect(parser.send(:startdate)).to eql(Date.new(2009, 1, 1)) end end end diff --git a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb index d5b6aa718..69627eccd 100644 --- a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb @@ -594,10 +594,21 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do end context "when field 5 is 4 digits instead of 2" do - let(:attributes) { setup_section_params.merge({ bulk_upload:, field_5: "2022" }) } + let(:attributes) { setup_section_params.merge({ bulk_upload:, field_5: "2023", field_4: "4", field_3: "3" }) } + + it "correctly sets the date" do + parser.valid? + expect(parser.errors.where(:field_5, category: :setup)).to be_empty + expect(parser.log.saledate).to eq(Time.zone.local(2023, 4, 3)) + end + end + + context "when field 5 is not 2 or 4 digits" do + let(:attributes) { setup_section_params.merge({ bulk_upload:, field_5: "202" }) } it "returns a setup error" do - expect(parser.errors.where(:field_5, category: :setup).map(&:message)).to include("Sale completion year must be 2 digits.") + parser.valid? + expect(parser.errors.where(:field_5, category: :setup).map(&:message)).to include("Sale completion year must be 2 or 4 digits.") end end diff --git a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb index 3c4e71247..bb12a83a4 100644 --- a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb @@ -721,12 +721,22 @@ RSpec.describe BulkUpload::Sales::Year2024::RowParser do end end - context "when field 5 is 4 digits instead of 2" do - let(:attributes) { setup_section_params.merge({ bulk_upload:, field_6: "2023" }) } + context "when field 6 is 4 digits instead of 2" do + let(:attributes) { setup_section_params.merge({ bulk_upload:, field_6: "2024" }) } + + it "correctly sets the date" do + parser.valid? + expect(parser.errors.where(:field_6, category: :setup)).to be_empty + expect(parser.log.saledate).to eq(Time.zone.local(2024, 5, 1)) + end + end + + context "when field 5 is not 2 or 4 digits" do + let(:attributes) { setup_section_params.merge({ bulk_upload:, field_6: "202" }) } it "returns a setup error" do parser.valid? - expect(parser.errors.where(:field_6, category: :setup).map(&:message)).to include("Sale completion year must be 2 digits.") + expect(parser.errors.where(:field_6, category: :setup).map(&:message)).to include("Sale completion year must be 2 or 4 digits.") end end