diff --git a/app/models/bulk_upload.rb b/app/models/bulk_upload.rb index 5a6971d10..dc862529e 100644 --- a/app/models/bulk_upload.rb +++ b/app/models/bulk_upload.rb @@ -1,7 +1,7 @@ class BulkUpload < ApplicationRecord enum log_type: { lettings: "lettings", sales: "sales" } enum rent_type_fix_status: { not_applied: "not_applied", applied: "applied", not_needed: "not_needed" } - enum failed: { blank_template: 1, wrong_template: 2 } + enum failure_reason: { blank_template: "blank_template", wrong_template: "wrong_template" } belongs_to :user @@ -39,8 +39,8 @@ class BulkUpload < ApplicationRecord def status return :processing if processing - return :blank_template if failed == "blank_template" - return :wrong_template if failed == "wrong_template" + return :blank_template if failure_reason == "blank_template" + return :wrong_template if failure_reason == "wrong_template" if logs.visible.exists? return :errors_fixed_in_service if completed? && bulk_upload_errors.any? diff --git a/app/services/bulk_upload/processor.rb b/app/services/bulk_upload/processor.rb index fa10b42e3..9425cf503 100644 --- a/app/services/bulk_upload/processor.rb +++ b/app/services/bulk_upload/processor.rb @@ -1,6 +1,22 @@ class BulkUpload::Processor attr_reader :bulk_upload + BLANK_TEMPLATE_ERRORS = [ + I18n.t('activemodel.errors.models.bulk_upload/lettings/validator.attributes.base.blank_file'), + I18n.t('activemodel.errors.models.bulk_upload/sales/validator.attributes.base.blank_file') + ].freeze + + WRONG_TEMPLATE_ERRORS = [ + I18n.t('activemodel.errors.models.bulk_upload/lettings/validator.attributes.base.wrong_field_numbers_count'), + I18n.t('activemodel.errors.models.bulk_upload/lettings/validator.attributes.base.over_max_column_count'), + I18n.t('activemodel.errors.models.bulk_upload/lettings/validator.attributes.base.wrong_template'), + I18n.t('activemodel.errors.models.bulk_upload/lettings/validator.attributes.base.no_headers'), + I18n.t('activemodel.errors.models.bulk_upload/sales/validator.attributes.base.wrong_field_numbers_count'), + I18n.t('activemodel.errors.models.bulk_upload/sales/validator.attributes.base.over_max_column_count'), + I18n.t('activemodel.errors.models.bulk_upload/sales/validator.attributes.base.wrong_template'), + I18n.t('activemodel.errors.models.bulk_upload/sales/validator.attributes.base.no_headers') + ].freeze + def initialize(bulk_upload:) @bulk_upload = bulk_upload end @@ -147,11 +163,12 @@ private end def handle_invalid_validator - if validator.errors.full_messages.include?("Template is blank - The template must be filled in for us to create the logs and check if data is correct.") - @bulk_upload.update!(failed: 1) - elsif validator.errors.full_messages.include?("Incorrect number of fields, please ensure you have used the correct template") - @bulk_upload.update!(failed: 2) + 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) } + @bulk_upload.update!(failure_reason: "wrong_template") end + send_failure_mail(errors: validator.errors.full_messages) end end diff --git a/config/locales/en.yml b/config/locales/en.yml index c666746da..1103d284d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -63,7 +63,7 @@ en: bulk_upload/lettings/validator: attributes: base: - blank_file: Template is blank - The template must be filled in for us to create the logs and check if data is correct. + blank_file: "Template is blank - The template must be filled in for us to create the logs and check if data is correct." 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" @@ -71,7 +71,7 @@ en: bulk_upload/sales/validator: attributes: base: - blank_file: Template is blank - The template must be filled in for us to create the logs and check if data is correct. + blank_file: "Template is blank - The template must be filled in for us to create the logs and check if data is correct." 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" diff --git a/db/migrate/20240925095041_add_failed_to_bulk_uploads.rb b/db/migrate/20240925095041_add_failed_to_bulk_uploads.rb deleted file mode 100644 index 82cc219bd..000000000 --- a/db/migrate/20240925095041_add_failed_to_bulk_uploads.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddFailedToBulkUploads < ActiveRecord::Migration[7.0] - def change - add_column :bulk_uploads, :failed, :integer - end -end diff --git a/db/migrate/20240927143916_add_processing_to_bulk_uploads.rb b/db/migrate/20240927143916_add_processing_to_bulk_uploads.rb deleted file mode 100644 index 51e6e9d4a..000000000 --- a/db/migrate/20240927143916_add_processing_to_bulk_uploads.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddProcessingToBulkUploads < ActiveRecord::Migration[7.0] - def change - add_column :bulk_uploads, :processing, :boolean, default: false - end -end diff --git a/db/migrate/20241002163937_add_failure_reason_and_processing_to_bulk_uploads.rb b/db/migrate/20241002163937_add_failure_reason_and_processing_to_bulk_uploads.rb new file mode 100644 index 000000000..727c22ec8 --- /dev/null +++ b/db/migrate/20241002163937_add_failure_reason_and_processing_to_bulk_uploads.rb @@ -0,0 +1,6 @@ +class AddFailureReasonAndProcessingToBulkUploads < ActiveRecord::Migration[7.0] + def change + add_column :bulk_uploads, :failure_reason, :string + add_column :bulk_uploads, :processing, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 18863e319..cb36e6303 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -46,6 +46,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_09_27_143916) do t.integer "moved_user_id" t.integer "failed" t.boolean "processing", default: false + t.string "failure_reason" + t.boolean "processing" t.index ["identifier"], name: "index_bulk_uploads_on_identifier", unique: true t.index ["user_id"], name: "index_bulk_uploads_on_user_id" end diff --git a/spec/models/bulk_upload_spec.rb b/spec/models/bulk_upload_spec.rb index 3f834695e..af2547d7a 100644 --- a/spec/models/bulk_upload_spec.rb +++ b/spec/models/bulk_upload_spec.rb @@ -173,7 +173,7 @@ RSpec.describe BulkUpload, type: :model do describe "#status" do context "when the bulk upload was uploaded with a blank template" do - let(:bulk_upload) { create(:bulk_upload, failed: 1) } + let(:bulk_upload) { create(:bulk_upload, failure_reason: "blank_template") } it "returns the correct status" do expect(bulk_upload.status).to eq(:blank_template) @@ -181,7 +181,7 @@ RSpec.describe BulkUpload, type: :model do end context "when the bulk upload was uploaded with the wrong template" do - let(:bulk_upload) { create(:bulk_upload, failed: 2) } + let(:bulk_upload) { create(:bulk_upload, failure_reason: "wrong_template") } it "returns the correct status" do expect(bulk_upload.status).to eq(:wrong_template)