2020-09-29 15:10:08 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BulkCreateIntegrationService
|
|
|
|
def initialize(integration, batch, association)
|
|
|
|
@integration = integration
|
|
|
|
@batch = batch
|
|
|
|
@association = association
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-06-30 06:07:17 +00:00
|
|
|
service_list = ServiceList.new(batch, integration_hash, association).to_array
|
2020-09-29 15:10:08 +00:00
|
|
|
|
2021-05-12 12:10:24 +00:00
|
|
|
Integration.transaction do
|
2020-09-29 15:10:08 +00:00
|
|
|
results = bulk_insert(*service_list)
|
|
|
|
|
|
|
|
if integration.data_fields_present?
|
|
|
|
data_list = DataList.new(results, data_fields_hash, integration.data_fields.class).to_array
|
|
|
|
|
|
|
|
bulk_insert(*data_list)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :integration, :batch, :association
|
|
|
|
|
|
|
|
def bulk_insert(klass, columns, values_array)
|
|
|
|
items_to_insert = values_array.map { |array| Hash[columns.zip(array)] }
|
|
|
|
|
|
|
|
klass.insert_all(items_to_insert, returning: [:id])
|
|
|
|
end
|
|
|
|
|
2021-06-30 06:07:17 +00:00
|
|
|
def integration_hash
|
2022-02-18 12:15:31 +00:00
|
|
|
integration.to_integration_hash.tap { |json| json['inherit_from_id'] = integration.inherit_from_id || integration.id }
|
2020-09-29 15:10:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def data_fields_hash
|
|
|
|
integration.to_data_fields_hash
|
|
|
|
end
|
|
|
|
end
|