2021-01-03 14:25:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BulkCreateIntegrationService
|
2022-07-23 23:45:48 +05:30
|
|
|
include Integrations::BulkOperationHashes
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def initialize(integration, batch, association)
|
|
|
|
@integration = integration
|
|
|
|
@batch = batch
|
|
|
|
@association = association
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2022-07-23 23:45:48 +05:30
|
|
|
service_list = ServiceList.new(batch, integration_hash(:create), association).to_array
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Integration.transaction do
|
2021-01-03 14:25:43 +05:30
|
|
|
results = bulk_insert(*service_list)
|
|
|
|
|
|
|
|
if integration.data_fields_present?
|
2022-07-23 23:45:48 +05:30
|
|
|
data_list = DataList.new(results, data_fields_hash(:create), integration.data_fields.class).to_array
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
end
|