Browse Source

CLDC-3216 Validate BU headers (#2232)

* Validate BU headers

* Fix translation

* Update link and validation order

* Add anchor

* Update link
pull/2253/head v0.4.18
kosiakkatrina 10 months ago committed by GitHub
parent
commit
7f355e933a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      app/services/bulk_upload/lettings/validator.rb
  2. 4
      app/services/bulk_upload/lettings/year2023/csv_parser.rb
  3. 4
      app/services/bulk_upload/lettings/year2024/csv_parser.rb
  4. 11
      app/services/bulk_upload/sales/validator.rb
  5. 4
      app/services/bulk_upload/sales/year2023/csv_parser.rb
  6. 4
      app/services/bulk_upload/sales/year2024/csv_parser.rb
  7. 2
      config/locales/en.yml
  8. 16
      spec/services/bulk_upload/sales/validator_spec.rb

11
app/services/bulk_upload/lettings/validator.rb

@ -1,10 +1,12 @@
class BulkUpload::Lettings::Validator class BulkUpload::Lettings::Validator
include ActiveModel::Validations include ActiveModel::Validations
include Rails.application.routes.url_helpers
attr_reader :bulk_upload, :path attr_reader :bulk_upload, :path
validate :validate_file_not_empty validate :validate_file_not_empty
validate :validate_field_numbers_count validate :validate_field_numbers_count
validate :validate_missing_required_headers
validate :validate_max_columns_count_if_no_headers validate :validate_max_columns_count_if_no_headers
validate :validate_correct_template validate :validate_correct_template
@ -175,6 +177,15 @@ private
errors.add(:base, :wrong_template) if csv_parser.wrong_template_for_year? errors.add(:base, :wrong_template) if csv_parser.wrong_template_for_year?
end 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! def halt_validations!
@halt_validations = true @halt_validations = true
end end

4
app/services/bulk_upload/lettings/year2023/csv_parser.rb

@ -70,6 +70,10 @@ class BulkUpload::Lettings::Year2023::CsvParser
false false
end end
def missing_required_headers?
false
end
private private
def default_field_numbers def default_field_numbers

4
app/services/bulk_upload/lettings/year2024/csv_parser.rb

@ -70,6 +70,10 @@ class BulkUpload::Lettings::Year2024::CsvParser
false false
end end
def missing_required_headers?
!with_headers?
end
private private
def default_field_numbers def default_field_numbers

11
app/services/bulk_upload/sales/validator.rb

@ -1,10 +1,12 @@
class BulkUpload::Sales::Validator class BulkUpload::Sales::Validator
include ActiveModel::Validations include ActiveModel::Validations
include Rails.application.routes.url_helpers
attr_reader :bulk_upload, :path attr_reader :bulk_upload, :path
validate :validate_file_not_empty validate :validate_file_not_empty
validate :validate_max_columns validate :validate_max_columns
validate :validate_missing_required_headers
validate :validate_correct_template validate :validate_correct_template
def initialize(bulk_upload:, path:) def initialize(bulk_upload:, path:)
@ -159,6 +161,15 @@ private
errors.add(:base, :wrong_template) if csv_parser.wrong_template_for_year? errors.add(:base, :wrong_template) if csv_parser.wrong_template_for_year?
end 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! def halt_validations!
@halt_validations = true @halt_validations = true
end end

4
app/services/bulk_upload/sales/year2023/csv_parser.rb

@ -55,6 +55,10 @@ class BulkUpload::Sales::Year2023::CsvParser
false false
end end
def missing_required_headers?
false
end
private private
def default_field_numbers def default_field_numbers

4
app/services/bulk_upload/sales/year2024/csv_parser.rb

@ -55,6 +55,10 @@ class BulkUpload::Sales::Year2024::CsvParser
false false
end end
def missing_required_headers?
!with_headers?
end
private private
def default_field_numbers def default_field_numbers

2
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" 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" 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" 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: bulk_upload/sales/validator:
attributes: attributes:
base: base:
@ -66,6 +67,7 @@ en:
wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template" 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" 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" 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: forms/bulk_upload_lettings/year:
attributes: attributes:
year: year:

16
spec/services/bulk_upload/sales/validator_spec.rb

@ -88,6 +88,22 @@ RSpec.describe BulkUpload::Sales::Validator do
end end
end 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 end
describe "#call" do describe "#call" do

Loading…
Cancel
Save