Browse Source

Change bulk upload attributes

pull/2666/head
Manny Dinssa 2 years ago
parent
commit
d68de15128
  1. 6
      app/models/bulk_upload.rb
  2. 25
      app/services/bulk_upload/processor.rb
  3. 4
      config/locales/en.yml
  4. 5
      db/migrate/20240925095041_add_failed_to_bulk_uploads.rb
  5. 5
      db/migrate/20240927143916_add_processing_to_bulk_uploads.rb
  6. 6
      db/migrate/20241002163937_add_failure_reason_and_processing_to_bulk_uploads.rb
  7. 2
      db/schema.rb
  8. 4
      spec/models/bulk_upload_spec.rb

6
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?

25
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

4
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"

5
db/migrate/20240925095041_add_failed_to_bulk_uploads.rb

@ -1,5 +0,0 @@
class AddFailedToBulkUploads < ActiveRecord::Migration[7.0]
def change
add_column :bulk_uploads, :failed, :integer
end
end

5
db/migrate/20240927143916_add_processing_to_bulk_uploads.rb

@ -1,5 +0,0 @@
class AddProcessingToBulkUploads < ActiveRecord::Migration[7.0]
def change
add_column :bulk_uploads, :processing, :boolean, default: false
end
end

6
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

2
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

4
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)

Loading…
Cancel
Save