2020-09-29 11:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BulkUpdateIntegrationService
|
2022-05-30 05:09:35 -04:00
|
|
|
include Integrations::BulkOperationHashes
|
|
|
|
|
2020-09-29 11:10:08 -04:00
|
|
|
def initialize(integration, batch)
|
|
|
|
@integration = integration
|
|
|
|
@batch = batch
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def execute
|
2021-05-12 08:10:24 -04:00
|
|
|
Integration.transaction do
|
2022-05-30 05:09:35 -04:00
|
|
|
Integration.where(id: batch_ids).update_all(integration_hash(:update))
|
2020-09-29 11:10:08 -04:00
|
|
|
|
|
|
|
if integration.data_fields_present?
|
2022-05-30 05:09:35 -04:00
|
|
|
integration.data_fields.class.where(data_fields_foreign_key => batch_ids)
|
|
|
|
.update_all(
|
|
|
|
data_fields_hash(:update)
|
|
|
|
)
|
2020-09-29 11:10:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :integration, :batch
|
|
|
|
|
2021-11-09 07:12:15 -05:00
|
|
|
# service_id or integration_id
|
|
|
|
def data_fields_foreign_key
|
|
|
|
integration.data_fields.class.reflections['integration'].foreign_key
|
|
|
|
end
|
|
|
|
|
2021-07-28 14:10:23 -04:00
|
|
|
def batch_ids
|
|
|
|
@batch_ids ||=
|
|
|
|
if batch.is_a?(ActiveRecord::Relation)
|
|
|
|
batch.select(:id)
|
|
|
|
else
|
|
|
|
batch.map(&:id)
|
|
|
|
end
|
|
|
|
end
|
2020-09-29 11:10:08 -04:00
|
|
|
end
|