debian-mirror-gitlab/app/services/bulk_update_integration_service.rb

42 lines
951 B
Ruby
Raw Normal View History

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-10-27 15:23:28 +05:30
integration.data_fields.class.where(service_id: 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-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