2021-01-03 14:25:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BulkUpdateIntegrationService
|
|
|
|
def initialize(integration, batch)
|
|
|
|
@integration = integration
|
|
|
|
@batch = batch
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def execute
|
2021-06-08 01:23:25 +05:30
|
|
|
Integration.transaction do
|
2021-10-27 15:23:28 +05:30
|
|
|
Integration.where(id: batch_ids).update_all(integration_hash)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
if integration.data_fields_present?
|
2021-12-11 22:18:48 +05:30
|
|
|
integration.data_fields.class.where(data_fields_foreign_key => batch_ids).update_all(data_fields_hash)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :integration, :batch
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
# service_id or integration_id
|
|
|
|
def data_fields_foreign_key
|
|
|
|
integration.data_fields.class.reflections['integration'].foreign_key
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def integration_hash
|
|
|
|
integration.to_integration_hash.tap { |json| json['inherit_from_id'] = integration.inherit_from_id || integration.id }
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def data_fields_hash
|
|
|
|
integration.to_data_fields_hash
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
def batch_ids
|
|
|
|
@batch_ids ||=
|
|
|
|
if batch.is_a?(ActiveRecord::Relation)
|
|
|
|
batch.select(:id)
|
|
|
|
else
|
|
|
|
batch.map(&:id)
|
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|