From 7f355e933a6d32d17bde477b00cd8e4effb4b02a Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 20 Feb 2024 08:06:13 +0000 Subject: [PATCH] CLDC-3216 Validate BU headers (#2232) * Validate BU headers * Fix translation * Update link and validation order * Add anchor * Update link --- app/services/bulk_upload/lettings/validator.rb | 11 +++++++++++ .../bulk_upload/lettings/year2023/csv_parser.rb | 4 ++++ .../bulk_upload/lettings/year2024/csv_parser.rb | 4 ++++ app/services/bulk_upload/sales/validator.rb | 11 +++++++++++ .../bulk_upload/sales/year2023/csv_parser.rb | 4 ++++ .../bulk_upload/sales/year2024/csv_parser.rb | 4 ++++ config/locales/en.yml | 2 ++ .../services/bulk_upload/sales/validator_spec.rb | 16 ++++++++++++++++ 8 files changed, 56 insertions(+) diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index 1eb98f030..8c1d33fe0 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -1,10 +1,12 @@ class BulkUpload::Lettings::Validator include ActiveModel::Validations + include Rails.application.routes.url_helpers attr_reader :bulk_upload, :path validate :validate_file_not_empty validate :validate_field_numbers_count + validate :validate_missing_required_headers validate :validate_max_columns_count_if_no_headers validate :validate_correct_template @@ -175,6 +177,15 @@ private errors.add(:base, :wrong_template) if csv_parser.wrong_template_for_year? end + def validate_missing_required_headers + return if halt_validations? + + if csv_parser.missing_required_headers? + errors.add :base, I18n.t("activemodel.errors.models.bulk_upload/lettings/validator.attributes.base.no_headers", guidance_link: bulk_upload_lettings_log_url(id: "guidance", form: { year: bulk_upload.year }, host: ENV["APP_HOST"], anchor: "using-the-bulk-upload-template")) + halt_validations! + end + end + def halt_validations! @halt_validations = true end diff --git a/app/services/bulk_upload/lettings/year2023/csv_parser.rb b/app/services/bulk_upload/lettings/year2023/csv_parser.rb index 2b80c6f81..72458ecf8 100644 --- a/app/services/bulk_upload/lettings/year2023/csv_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/csv_parser.rb @@ -70,6 +70,10 @@ class BulkUpload::Lettings::Year2023::CsvParser false end + def missing_required_headers? + false + end + private def default_field_numbers diff --git a/app/services/bulk_upload/lettings/year2024/csv_parser.rb b/app/services/bulk_upload/lettings/year2024/csv_parser.rb index 1fb083c3e..061f3bbbd 100644 --- a/app/services/bulk_upload/lettings/year2024/csv_parser.rb +++ b/app/services/bulk_upload/lettings/year2024/csv_parser.rb @@ -70,6 +70,10 @@ class BulkUpload::Lettings::Year2024::CsvParser false end + def missing_required_headers? + !with_headers? + end + private def default_field_numbers diff --git a/app/services/bulk_upload/sales/validator.rb b/app/services/bulk_upload/sales/validator.rb index 978537124..91fa50096 100644 --- a/app/services/bulk_upload/sales/validator.rb +++ b/app/services/bulk_upload/sales/validator.rb @@ -1,10 +1,12 @@ class BulkUpload::Sales::Validator include ActiveModel::Validations + include Rails.application.routes.url_helpers attr_reader :bulk_upload, :path validate :validate_file_not_empty validate :validate_max_columns + validate :validate_missing_required_headers validate :validate_correct_template def initialize(bulk_upload:, path:) @@ -159,6 +161,15 @@ private errors.add(:base, :wrong_template) if csv_parser.wrong_template_for_year? end + def validate_missing_required_headers + return if halt_validations? + + if csv_parser.missing_required_headers? + errors.add :base, I18n.t("activemodel.errors.models.bulk_upload/sales/validator.attributes.base.no_headers", guidance_link: bulk_upload_sales_log_url(id: "guidance", form: { year: bulk_upload.year }, host: ENV["APP_HOST"], anchor: "using-the-bulk-upload-template")) + halt_validations! + end + end + def halt_validations! @halt_validations = true end diff --git a/app/services/bulk_upload/sales/year2023/csv_parser.rb b/app/services/bulk_upload/sales/year2023/csv_parser.rb index abbc51632..42b0764d9 100644 --- a/app/services/bulk_upload/sales/year2023/csv_parser.rb +++ b/app/services/bulk_upload/sales/year2023/csv_parser.rb @@ -55,6 +55,10 @@ class BulkUpload::Sales::Year2023::CsvParser false end + def missing_required_headers? + false + end + private def default_field_numbers diff --git a/app/services/bulk_upload/sales/year2024/csv_parser.rb b/app/services/bulk_upload/sales/year2024/csv_parser.rb index 81b7c8f61..e53f0ec28 100644 --- a/app/services/bulk_upload/sales/year2024/csv_parser.rb +++ b/app/services/bulk_upload/sales/year2024/csv_parser.rb @@ -55,6 +55,10 @@ class BulkUpload::Sales::Year2024::CsvParser false end + def missing_required_headers? + !with_headers? + end + private def default_field_numbers diff --git a/config/locales/en.yml b/config/locales/en.yml index 2fba138c4..90962e08d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -59,6 +59,7 @@ en: wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template" over_max_column_count: "Too many columns, please ensure you have used the correct template" wrong_template: "Incorrect start dates, please ensure you have used the correct template" + no_headers: "Your file does not contain the required header rows. Add or check the header rows and upload your file again. [Read more about using the template headers](%{guidance_link})." bulk_upload/sales/validator: attributes: base: @@ -66,6 +67,7 @@ en: wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template" over_max_column_count: "Too many columns, please ensure you have used the correct template" wrong_template: "Incorrect sale dates, please ensure you have used the correct template" + no_headers: "Your file does not contain the required header rows. Add or check the header rows and upload your file again. [Read more about using the template headers](%{guidance_link})." forms/bulk_upload_lettings/year: attributes: year: diff --git a/spec/services/bulk_upload/sales/validator_spec.rb b/spec/services/bulk_upload/sales/validator_spec.rb index dec7e5cef..3dceb80da 100644 --- a/spec/services/bulk_upload/sales/validator_spec.rb +++ b/spec/services/bulk_upload/sales/validator_spec.rb @@ -88,6 +88,22 @@ RSpec.describe BulkUpload::Sales::Validator do end end end + + context "when file is missing required headers" do + let(:bulk_upload) { create(:bulk_upload, user:, year: 2024) } + let(:log) { build(:sales_log, :completed, saledate: Time.zone.local(2024, 5, 5)) } + let(:file) { Tempfile.new } + let(:path) { file.path } + + before do + file.write(BulkUpload::SalesLogToCsv.new(log:, line_ending: "\r\n", col_offset: 0).to_2024_csv_row) + file.close + end + + it "is not valid" do + expect(validator).not_to be_valid + end + end end describe "#call" do