Class: SpreeCmCommissioner::Imports::Orders::Create

Inherits:
Base
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/imports/orders/create.rb

Constant Summary collapse

BATCH_SIZE =
ENV.fetch('IMPORT_ORDERS_BATCH_SIZE', '50').to_i

Instance Attribute Summary collapse

Attributes inherited from Base

#fail_row_numbers, #import_order_id

Instance Method Summary collapse

Methods inherited from Base

#call, #cleaned_value, #fetch_content, #import_order, #record_failure, #save_fail_rows, #update_import_status_when_finish, #update_import_status_when_start

Constructor Details

#initialize(import_order_id:, import_by_user_id:) ⇒ Create

Returns a new instance of Create.



11
12
13
14
# File 'app/services/spree_cm_commissioner/imports/orders/create.rb', line 11

def initialize(import_order_id:, import_by_user_id:)
  super(import_order_id: import_order_id)
  @import_by_user_id = import_by_user_id
end

Instance Attribute Details

#import_by_user_idObject (readonly)

Returns the value of attribute import_by_user_id.



9
10
11
# File 'app/services/spree_cm_commissioner/imports/orders/create.rb', line 9

def import_by_user_id
  @import_by_user_id
end

Instance Method Details

#import_ordersObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/spree_cm_commissioner/imports/orders/create.rb', line 16

def import_orders
  # Processes CSV orders in a memory-efficient streaming manner to handle large files (5k+ rows).
  # Batches rows into groups of 100 for reduced DB transaction overhead and atomicity per batch.
  # Uses CSV.foreach to stream from disk, avoiding memory spikes. Row indices start at 2 (post-header).
  file_path = fetch_content
  batch = []

  begin
    CSV.foreach(file_path, headers: true).with_index(2) do |row, index|
      batch << [row.to_hash.symbolize_keys, index]

      # Process batch when it reaches the configured size
      if batch.size >= BATCH_SIZE
        process_batch(batch)
        batch = []
      end
    end

    process_batch(batch) unless batch.empty?
  ensure
    FileUtils.rm_f(file_path)
  end
end

#process_batch(batch) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/spree_cm_commissioner/imports/orders/create.rb', line 40

def process_batch(batch)
  # Processes a batch of orders with each row in its own transaction for error isolation.
  # Failures record but don't affect other rows, allowing partial batch success.
  batch.each do |order_data, index|
    ActiveRecord::Base.transaction do
      process_order(order_data, index)
    rescue StandardError => e
      CmAppLogger.error(
        label: "#{self.class.name}::process_batch",
        data: { message: e.message, row: index }
      )
      record_failure(index)
    end
  end
end

#process_order(order_data, index) ⇒ Object



56
57
58
59
60
# File 'app/services/spree_cm_commissioner/imports/orders/create.rb', line 56

def process_order(order_data, index)
  params = build_order_params(order_data)
  order  = create_order(params, index)
  record_failure(index) if order && !complete_order(order)
end