diff --git a/app/models/bulk_import.rb b/app/models/bulk_import.rb index e3e46d7b1c6..29327ab1111 100644 --- a/app/models/bulk_import.rb +++ b/app/models/bulk_import.rb @@ -48,6 +48,14 @@ class BulkImport < ApplicationRecord scope :archival_completed, -> { where(created_at: ..ARCHIVE_PERIOD.ago) } scope :confirmation_missed, -> { state_unconfirmed.where(created_at: ..CONFIRM_PERIOD.ago) } + def failure_count + processed_items - imported_items + end + + def processing_complete? + processed_items == total_items + end + def self.progress!(bulk_import_id, imported: false) # Use `increment_counter` so that the incrementation is done atomically in the database BulkImport.increment_counter(:processed_items, bulk_import_id) @@ -55,6 +63,6 @@ class BulkImport < ApplicationRecord # Since the incrementation has been done atomically, concurrent access to `bulk_import` is now bening bulk_import = BulkImport.find(bulk_import_id) - bulk_import.update!(state: :finished, finished_at: Time.now.utc) if bulk_import.processed_items == bulk_import.total_items + bulk_import.update!(state: :finished, finished_at: Time.now.utc) if bulk_import.processing_complete? end end diff --git a/app/services/bulk_import_service.rb b/app/services/bulk_import_service.rb index a361c7a3dac..8e0864a07f4 100644 --- a/app/services/bulk_import_service.rb +++ b/app/services/bulk_import_service.rb @@ -20,7 +20,7 @@ class BulkImportService < BaseService import_lists! end - @import.update!(state: :finished, finished_at: Time.now.utc) if @import.processed_items == @import.total_items + @import.update!(state: :finished, finished_at: Time.now.utc) if @import.processing_complete? rescue @import.update!(state: :finished, finished_at: Time.now.utc) diff --git a/app/views/settings/imports/index.html.haml b/app/views/settings/imports/index.html.haml index 55421991e13..e5195b74cc8 100644 --- a/app/views/settings/imports/index.html.haml +++ b/app/views/settings/imports/index.html.haml @@ -60,9 +60,6 @@ = l(import.created_at) %td - - num_failed = import.processed_items - import.imported_items - - if num_failed.positive? - - if import.state_finished? - = link_to num_failed, failures_settings_import_path(import, format: 'csv') - - else - = num_failed + - if import.failure_count.positive? + = link_to_if import.state_finished?, import.failure_count, failures_settings_import_path(import, format: :csv) do + = import.failure_count diff --git a/spec/models/bulk_import_spec.rb b/spec/models/bulk_import_spec.rb index a3bd01d2a85..4eb36aeb464 100644 --- a/spec/models/bulk_import_spec.rb +++ b/spec/models/bulk_import_spec.rb @@ -39,4 +39,28 @@ RSpec.describe BulkImport do end end end + + describe '#failure_count' do + subject { described_class.new(processed_items: 100, imported_items: 90).failure_count } + + it { is_expected.to eq(10) } + end + + describe '#processing_complete?' do + subject { Fabricate.build :bulk_import, processed_items:, total_items: } + + context 'when processed and total are the same' do + let(:processed_items) { 100 } + let(:total_items) { 100 } + + it { is_expected.to be_processing_complete } + end + + context 'when processed and total are not the same' do + let(:processed_items) { 100 } + let(:total_items) { 200 } + + it { is_expected.to_not be_processing_complete } + end + end end