diff --git a/app/services/bulk_upload/processor.rb b/app/services/bulk_upload/processor.rb index 4a87ffcc1..6975bab23 100644 --- a/app/services/bulk_upload/processor.rb +++ b/app/services/bulk_upload/processor.rb @@ -1,15 +1,22 @@ class BulkUpload::Processor + include CollectionTimeHelper attr_reader :bulk_upload - BLANK_TEMPLATE_ERRORS = [ - I18n.t("activemodel.errors.models.bulk_upload/lettings/validator.attributes.base.blank_file"), - I18n.t("validations.sales.2024.bulk_upload.blank_file"), - ].freeze + def blank_template_errors + [ + I18n.t("activemodel.errors.models.bulk_upload/lettings/validator.attributes.base.blank_file"), + I18n.t("validations.sales.#{current_collection_start_year}.bulk_upload.blank_file"), + I18n.t("validations.sales.#{previous_collection_start_year}.bulk_upload.blank_file"), + ].freeze + end - WRONG_TEMPLATE_ERRORS = [ - *I18n.t("activemodel.errors.models.bulk_upload/lettings/validator.attributes.base", default: {}).values, - *I18n.t("validations.sales.2024", default: {}).values, - ].freeze + def wrong_template_errors + [ + *I18n.t("activemodel.errors.models.bulk_upload/lettings/validator.attributes.base", default: {}).values, + *I18n.t("validations.sales.#{current_collection_start_year}.bulk_upload.wrong_template", default: {}).values, + *I18n.t("validations.sales.#{previous_collection_start_year}.bulk_upload.wrong_template", default: {}).values, + ].freeze + end def initialize(bulk_upload:) @bulk_upload = bulk_upload @@ -157,9 +164,9 @@ private end def handle_invalid_validator - if BLANK_TEMPLATE_ERRORS.any? { |error| validator.errors.full_messages.include?(error) } + if blank_template_errors.any? { |error| validator.errors.full_messages.include?(error) } @bulk_upload.update!(failure_reason: "blank_template") - elsif WRONG_TEMPLATE_ERRORS.any? { |error| validator.errors.full_messages.include?(error) } + elsif wrong_template_errors.any? { |error| validator.errors.full_messages.include?(error) } @bulk_upload.update!(failure_reason: "wrong_template") end diff --git a/app/services/bulk_upload/sales/validator.rb b/app/services/bulk_upload/sales/validator.rb index 070732a87..76fb6f1ae 100644 --- a/app/services/bulk_upload/sales/validator.rb +++ b/app/services/bulk_upload/sales/validator.rb @@ -164,20 +164,20 @@ private column_count = rows.map(&:size).max - errors.add(:base, I18n.t("validations.sales.#{@bulk_upload.year}.bulk_upload.over_max_column_count")) if column_count > csv_parser.class::MAX_COLUMNS + errors.add(:base, I18n.t("validations.sales.#{@bulk_upload.year}.bulk_upload.wrong_template.over_max_column_count")) if column_count > csv_parser.class::MAX_COLUMNS end def validate_correct_template return if halt_validations? - errors.add(:base, I18n.t("validations.sales.#{@bulk_upload.year}.bulk_upload.wrong_template")) if csv_parser.wrong_template_for_year? + errors.add(:base, I18n.t("validations.sales.#{@bulk_upload.year}.bulk_upload.wrong_template.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("validations.sales.#{@bulk_upload.year}.bulk_upload.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")) + errors.add :base, I18n.t("validations.sales.#{@bulk_upload.year}.bulk_upload.wrong_template.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")) end end @@ -185,7 +185,7 @@ private return if halt_validations? unless csv_parser.correct_field_count? - errors.add(:base, I18n.t("validations.sales.#{@bulk_upload.year}.bulk_upload.wrong_field_numbers_count")) + errors.add(:base, I18n.t("validations.sales.#{@bulk_upload.year}.bulk_upload.wrong_template.wrong_field_numbers_count")) halt_validations! end end diff --git a/config/locales/validations/sales/2023/bulk_upload.en.yml b/config/locales/validations/sales/2023/bulk_upload.en.yml index 7d8930db4..517f56858 100644 --- a/config/locales/validations/sales/2023/bulk_upload.en.yml +++ b/config/locales/validations/sales/2023/bulk_upload.en.yml @@ -3,15 +3,16 @@ en: sales: 2023: bulk_upload: - blank_file: "Template is blank - The template must be filled in for us to create the logs and check if data is correct." - over_max_column_count: "Too many columns, 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})." - wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template." - wrong_template: "Incorrect sale dates, please ensure you have used the correct template." not_answered: "You must answer %{question}" invalid_option: "Enter a valid value for %{question}" spreadsheet_dupe: "This is a duplicate of a log in your file." duplicate: "This is a duplicate log." + blank_file: "Template is blank - The template must be filled in for us to create the logs and check if data is correct." + wrong_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})." + 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." owning_organisation: not_answered: "The owning organisation code is incorrect." not_found: "The owning organisation code is incorrect." diff --git a/config/locales/validations/sales/2024/bulk_upload.en.yml b/config/locales/validations/sales/2024/bulk_upload.en.yml index a76dbb838..be466c4a2 100644 --- a/config/locales/validations/sales/2024/bulk_upload.en.yml +++ b/config/locales/validations/sales/2024/bulk_upload.en.yml @@ -3,15 +3,16 @@ en: sales: 2024: bulk_upload: - blank_file: "Template is blank - The template must be filled in for us to create the logs and check if data is correct." - over_max_column_count: "Too many columns, 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})." - wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template." - wrong_template: "Incorrect sale dates, please ensure you have used the correct template." not_answered: "You must answer %{question}" invalid_option: "Enter a valid value for %{question}" spreadsheet_dupe: "This is a duplicate of a log in your file." duplicate: "This is a duplicate log." + blank_file: "Template is blank - The template must be filled in for us to create the logs and check if data is correct." + wrong_template: + over_max_column_count: "Too many columns, 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})." + wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template." + wrong_template: "Incorrect sale dates, please ensure you have used the correct template." numeric: within_range: "%{field} must be between %{min} and %{max}." owning_organisation: diff --git a/spec/services/bulk_upload/sales/validator_spec.rb b/spec/services/bulk_upload/sales/validator_spec.rb index 149354bf1..c275ce681 100644 --- a/spec/services/bulk_upload/sales/validator_spec.rb +++ b/spec/services/bulk_upload/sales/validator_spec.rb @@ -54,7 +54,7 @@ RSpec.describe BulkUpload::Sales::Validator do it "is not valid" do expect(validator).not_to be_valid - expect(validator.errors["base"]).to eql([I18n.t("validations.sales.2024.bulk_upload.wrong_template")]) + expect(validator.errors["base"]).to eql([I18n.t("validations.sales.2024.bulk_upload.wrong_template.wrong_template")]) end end