2021-09-30 23:02:18 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
class EntitiesFinder
|
2022-06-21 17:19:12 +05:30
|
|
|
def initialize(user:, bulk_import: nil, params: {})
|
2021-09-30 23:02:18 +05:30
|
|
|
@user = user
|
|
|
|
@bulk_import = bulk_import
|
2022-06-21 17:19:12 +05:30
|
|
|
@params = params
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
::BulkImports::Entity
|
|
|
|
.preload(:failures) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
.by_user_id(user.id)
|
|
|
|
.then(&method(:filter_by_bulk_import))
|
|
|
|
.then(&method(:filter_by_status))
|
2022-06-21 17:19:12 +05:30
|
|
|
.then(&method(:sort))
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :user, :bulk_import, :status
|
|
|
|
|
|
|
|
def filter_by_bulk_import(entities)
|
|
|
|
return entities unless bulk_import
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
entities.by_bulk_import_id(bulk_import.id)
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_status(entities)
|
2022-06-21 17:19:12 +05:30
|
|
|
return entities unless ::BulkImports::Entity.all_human_statuses.include?(@params[:status])
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
entities.with_status(@params[:status])
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort(entities)
|
|
|
|
return entities unless @params[:sort]
|
|
|
|
|
|
|
|
entities.order_by_created_at(@params[:sort])
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|