From a0b708b21eb75b1e58aadac4c93ab16c6a04475b Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Mon, 19 Jun 2023 15:07:17 +0100 Subject: [PATCH] check bulk upload template against date (#1681) --- .../bulk_upload/lettings/validator.rb | 10 ++++-- .../lettings/year2022/csv_parser.rb | 11 ++++++- .../lettings/year2022/row_parser.rb | 12 +++---- .../bulk_upload/sales/year2022/csv_parser.rb | 11 ++++++- .../bulk_upload/sales/year2022/row_parser.rb | 12 +++---- .../bulk_upload/lettings/validator_spec.rb | 4 ++- .../lettings/year2022/csv_parser_spec.rb | 28 +++++++++++++++++ .../sales/year2022/csv_parser_spec.rb | 31 +++++++++++++++++++ 8 files changed, 102 insertions(+), 17 deletions(-) diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index 47600978b..14c31b6d7 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -150,13 +150,19 @@ private def validate_field_numbers_count return if halt_validations? - errors.add(:base, :wrong_field_numbers_count) unless csv_parser.correct_field_count? + unless csv_parser.correct_field_count? + errors.add(:base, :wrong_field_numbers_count) + halt_validations! + end end def validate_max_columns_count_if_no_headers return if halt_validations? - errors.add(:base, :over_max_column_count) if csv_parser.too_many_columns? + if csv_parser.too_many_columns? + errors.add(:base, :over_max_column_count) + halt_validations! + end end def validate_correct_template diff --git a/app/services/bulk_upload/lettings/year2022/csv_parser.rb b/app/services/bulk_upload/lettings/year2022/csv_parser.rb index f5bba15bf..f21e92a24 100644 --- a/app/services/bulk_upload/lettings/year2022/csv_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/csv_parser.rb @@ -3,6 +3,7 @@ require "csv" class BulkUpload::Lettings::Year2022::CsvParser FIELDS = 134 MAX_COLUMNS = 135 + FORM_YEAR = 2022 attr_reader :path @@ -62,11 +63,19 @@ class BulkUpload::Lettings::Year2022::CsvParser end def wrong_template_for_year? - false + !(first_record_start_date >= form.start_date && first_record_start_date <= form.end_date) end private + def form + @form ||= FormHandler.instance.lettings_form_for_start_year(FORM_YEAR) + end + + def first_record_start_date + @first_record_start_date ||= row_parsers.first.startdate || Date.new + end + def default_field_numbers ("field_1".."field_#{FIELDS}").to_a end diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index c6251c81f..83ed20457 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -461,6 +461,12 @@ class BulkUpload::Lettings::Year2022::RowParser end end + def startdate + Date.new(field_98 + 2000, field_97, field_96) if field_98.present? && field_97.present? && field_96.present? + rescue Date::Error + Date.new + end + private def validate_declaration_acceptance @@ -983,12 +989,6 @@ private } end - def startdate - Date.new(field_98 + 2000, field_97, field_96) if field_98.present? && field_97.present? && field_96.present? - rescue Date::Error - Date.new - end - def renttype case field_1 when 1, 2, 3, 4 diff --git a/app/services/bulk_upload/sales/year2022/csv_parser.rb b/app/services/bulk_upload/sales/year2022/csv_parser.rb index 4bae79d33..939ff5689 100644 --- a/app/services/bulk_upload/sales/year2022/csv_parser.rb +++ b/app/services/bulk_upload/sales/year2022/csv_parser.rb @@ -2,6 +2,7 @@ require "csv" class BulkUpload::Sales::Year2022::CsvParser MAX_COLUMNS = 126 + FORM_YEAR = 2022 attr_reader :path @@ -44,11 +45,19 @@ class BulkUpload::Sales::Year2022::CsvParser end def wrong_template_for_year? - false + !(first_record_sale_date >= form.start_date && first_record_sale_date <= form.end_date) end private + def form + @form ||= FormHandler.instance.sales_form_for_start_year(FORM_YEAR) + end + + def first_record_sale_date + @first_record_sale_date ||= row_parsers.first.saledate || Date.new + end + def headers @headers ||= ("field_1".."field_125").to_a end diff --git a/app/services/bulk_upload/sales/year2022/row_parser.rb b/app/services/bulk_upload/sales/year2022/row_parser.rb index 1ac6258fd..40860c0d0 100644 --- a/app/services/bulk_upload/sales/year2022/row_parser.rb +++ b/app/services/bulk_upload/sales/year2022/row_parser.rb @@ -453,6 +453,12 @@ class BulkUpload::Sales::Year2022::RowParser end end + def saledate + Date.new(field_4 + 2000, field_3, field_2) if field_2.present? && field_3.present? && field_4.present? + rescue Date::Error + Date.new + end + private def validate_data_protection_answered @@ -762,12 +768,6 @@ private end end - def saledate - Date.new(field_4 + 2000, field_3, field_2) if field_2.present? && field_3.present? && field_4.present? - rescue Date::Error - Date.new - end - def hodate Date.new(field_61 + 2000, field_60, field_59) if field_59.present? && field_60.present? && field_61.present? rescue Date::Error diff --git a/spec/services/bulk_upload/lettings/validator_spec.rb b/spec/services/bulk_upload/lettings/validator_spec.rb index 27d018229..9f202e2f9 100644 --- a/spec/services/bulk_upload/lettings/validator_spec.rb +++ b/spec/services/bulk_upload/lettings/validator_spec.rb @@ -36,7 +36,9 @@ RSpec.describe BulkUpload::Lettings::Validator do context "and doesn't have too many columns" do before do - file.write(("a" * 135).chars.join(",")) + file.write(("a" * 95).chars.join(",")) + file.write(",1,10,22,") + file.write(("a" * 37).chars.join(",")) file.write("\n") file.rewind end diff --git a/spec/services/bulk_upload/lettings/year2022/csv_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/csv_parser_spec.rb index 5ae063567..15d20e9b2 100644 --- a/spec/services/bulk_upload/lettings/year2022/csv_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/csv_parser_spec.rb @@ -187,4 +187,32 @@ RSpec.describe BulkUpload::Lettings::Year2022::CsvParser do expect(service.row_parsers[0].field_12.to_i).to eq(35) end end + + describe "#wrong_template_for_year?" do + context "when 23/24 file with 23/24 data" do + let(:log) { build(:lettings_log, :completed, startdate: Date.new(2023, 10, 1)) } + + before do + file.write(BulkUpload::LettingsLogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.rewind + end + + it "returns true" do + expect(service).to be_wrong_template_for_year + end + end + + context "when 22/23 file with 22/23 data" do + let(:log) { build(:lettings_log, :completed, startdate: Date.new(2022, 10, 1)) } + + before do + file.write(BulkUpload::LettingsLogToCsv.new(log:, col_offset: 0).to_2022_csv_row) + file.rewind + end + + it "returns false" do + expect(service).not_to be_wrong_template_for_year + end + end + end end diff --git a/spec/services/bulk_upload/sales/year2022/csv_parser_spec.rb b/spec/services/bulk_upload/sales/year2022/csv_parser_spec.rb index 76504b974..c33f6cf2f 100644 --- a/spec/services/bulk_upload/sales/year2022/csv_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2022/csv_parser_spec.rb @@ -116,4 +116,35 @@ RSpec.describe BulkUpload::Sales::Year2022::CsvParser do expect(service.column_for_field("field_125")).to eql("DU") end end + + describe "#wrong_template_for_year?" do + let(:file) { Tempfile.new } + let(:path) { file.path } + + context "when 23/24 file with 23/24 data" do + let(:log) { build(:sales_log, :completed, saledate: Date.new(2023, 10, 1)) } + + before do + file.write(BulkUpload::SalesLogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.rewind + end + + it "returns true" do + expect(service).to be_wrong_template_for_year + end + end + + context "when 22/23 file with 22/23 data" do + let(:log) { build(:sales_log, :completed, saledate: Date.new(2022, 10, 1)) } + + before do + file.write(BulkUpload::SalesLogToCsv.new(log:, col_offset: 0).to_2022_csv_row) + file.rewind + end + + it "returns false" do + expect(service).not_to be_wrong_template_for_year + end + end + end end