Browse Source

Validate that correct template for the year is used for sales (with headers)

pull/1665/head
Kat 3 years ago
parent
commit
4bcdbd2575
  1. 7
      app/services/bulk_upload/sales/validator.rb
  2. 4
      app/services/bulk_upload/sales/year2022/csv_parser.rb
  3. 8
      app/services/bulk_upload/sales/year2023/csv_parser.rb
  4. 1
      config/locales/en.yml
  5. 44
      spec/services/bulk_upload/sales/validator_spec.rb

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

@ -5,6 +5,7 @@ class BulkUpload::Sales::Validator
validate :validate_file_not_empty validate :validate_file_not_empty
validate :validate_max_columns validate :validate_max_columns
validate :validate_correct_template
def initialize(bulk_upload:, path:) def initialize(bulk_upload:, path:)
@bulk_upload = bulk_upload @bulk_upload = bulk_upload
@ -133,6 +134,12 @@ private
errors.add(:base, :over_max_column_count) if column_count > csv_parser.class::MAX_COLUMNS errors.add(:base, :over_max_column_count) if column_count > csv_parser.class::MAX_COLUMNS
end end
def validate_correct_template
return if halt_validations?
errors.add(:base, :wrong_template) unless csv_parser.correct_template_for_year?
end
def halt_validations! def halt_validations!
@halt_validations = true @halt_validations = true
end end

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

@ -43,6 +43,10 @@ class BulkUpload::Sales::Year2022::CsvParser
cols[headers.find_index(field) + col_offset] cols[headers.find_index(field) + col_offset]
end end
def correct_template_for_year?
true
end
private private
def headers def headers

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

@ -46,6 +46,10 @@ class BulkUpload::Sales::Year2023::CsvParser
cols[field_numbers.find_index(field) + col_offset] cols[field_numbers.find_index(field) + col_offset]
end end
def correct_template_for_year?
!with_headers? || (with_headers? && has_field_in_header?(135))
end
private private
def default_field_numbers def default_field_numbers
@ -87,4 +91,8 @@ private
@normalised_string @normalised_string
end end
def has_field_in_header?(field)
rows[rows.find_index { |row| row[0].match(/field number/i) }].any? { |cell| cell&.match?(/#{field}/i) }
end
end end

1
config/locales/en.yml

@ -55,6 +55,7 @@ en:
base: base:
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 fields, please ensure you have used the correct template"
forms/bulk_upload_lettings/year: forms/bulk_upload_lettings/year:
attributes: attributes:
year: year:

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

@ -27,7 +27,49 @@ RSpec.describe BulkUpload::Sales::Validator do
end end
end end
context "when incorrect headers" context "when trying to upload 2022 data for 2023 bulk upload" do
let(:bulk_upload) { create(:bulk_upload, user:, year: 2023) }
context "with a valid csv" do
let(:path) { file_fixture("2022_23_sales_bulk_upload.csv") }
it "is not valid" do
expect(validator).not_to be_valid
end
end
context "with unix line endings" do
let(:fixture_path) { file_fixture("2022_23_sales_bulk_upload.csv") }
let(:file) { Tempfile.new }
let(:path) { file.path }
before do
string = File.read(fixture_path)
string.gsub!("\r\n", "\n")
file.write(string)
file.rewind
end
it "is not valid" do
expect(validator).not_to be_valid
end
end
context "without headers" do
let(:log) { build(:sales_log, :completed) }
let(:file) { Tempfile.new }
let(:path) { file.path }
before do
file.write(BulkUpload::SalesLogToCsv.new(log:, line_ending: "\r\n", col_offset: 0).to_2022_csv_row)
file.close
end
xit "is not valid" do
expect(validator).not_to be_valid
end
end
end
end end
describe "#call" do describe "#call" do

Loading…
Cancel
Save