Sha256: 97e5a2e964285b0b3c65925a68481061f1612f250b5c2d6371bb897620a2fb67

Contents?: true

Size: 1.57 KB

Versions: 154

Compression:

Stored size: 1.57 KB

Contents

class FileImportProcessorJob < ApplicationJob
  queue_as :default

  def perform(file_import)
    model = CmAdmin::Model.find_by({name: file_import.associated_model_name})
    content = file_import.import_file.download
    importer = model.importer.class_name.classify.constantize.new(content: content)
    case model.importer.importer_type.to_s
    when 'csv_importer'
      run_csv_importer(importer, file_import)
    when 'custom_importer'
      run_custom_importer(importer, file_import)
    end
  end

  # All logic are from csv_importer gem
  def run_csv_importer(importer, file_import)
    if importer.valid_header?
      importer.run!
      if importer.report.success?
        file_import.update(status: :success, completed_at: DateTime.now)
      else
        identifier = importer.config.identifiers.first
        invalid_items_array = importer.report.invalid_rows.map { |row| [row.line_number, row.model.send(identifier), row.errors] }
        file_import.update(status: :failed, completed_at: DateTime.now, invalid_row_items: invalid_items_array)
      end
    else
      file_import.update(status: :failed, completed_at: DateTime.now, invalid_row_items: [[1, 'invalid_header', {invalid_header: importer.report.message}]])
    end
  end

  def run_custom_importer(importer, file_import)
    importer.run!
    invalid_items_array = importer.invalid_rows.map { |row| [row.line_number, row.identifier, row.errors] }
    status = importer.invalid_rows.empty? ? :success : :failed
    file_import.update(status: status, completed_at: DateTime.now, invalid_row_items: invalid_items_array)
  end
end

Version data entries

154 entries across 154 versions & 1 rubygems

Version Path
cm-admin-4.4.8 app/jobs/file_import_processor_job.rb
cm-admin-4.4.6 app/jobs/file_import_processor_job.rb
cm-admin-4.4.1 app/jobs/file_import_processor_job.rb
cm-admin-4.4.0 app/jobs/file_import_processor_job.rb
cm-admin-4.3.0 app/jobs/file_import_processor_job.rb
cm-admin-4.2.1 app/jobs/file_import_processor_job.rb
cm-admin-4.2.0 app/jobs/file_import_processor_job.rb
cm-admin-4.1.2 app/jobs/file_import_processor_job.rb
cm-admin-4.1.1 app/jobs/file_import_processor_job.rb
cm-admin-4.1.0 app/jobs/file_import_processor_job.rb
cm-admin-4.0.0 app/jobs/file_import_processor_job.rb
cm-admin-3.0.16 app/jobs/file_import_processor_job.rb
cm-admin-3.0.15 app/jobs/file_import_processor_job.rb
cm-admin-3.0.14 app/jobs/file_import_processor_job.rb
cm-admin-3.0.13 app/jobs/file_import_processor_job.rb
cm-admin-3.0.12 app/jobs/file_import_processor_job.rb
cm-admin-3.0.11 app/jobs/file_import_processor_job.rb
cm-admin-3.0.10 app/jobs/file_import_processor_job.rb
cm-admin-3.0.9 app/jobs/file_import_processor_job.rb
cm-admin-3.0.8 app/jobs/file_import_processor_job.rb