Class: SpreeCmCommissioner::OrderImporter::MultiGuest

Inherits:
BaseInteractor
  • Object
show all
Defined in:
app/interactors/spree_cm_commissioner/order_importer/multi_guest.rb

Instance Method Summary collapse

Instance Method Details

#callObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/interactors/spree_cm_commissioner/order_importer/multi_guest.rb', line 6

def call
  return context.fail!(message: 'variant_id_is_required') if params[:variant_id].blank?
  return context.fail!(message: 'email_or_phone_is_required') if params[:order_email].blank? && params[:order_phone_number].blank?

  context.order = construct_order

  begin
    # Wraps order save and inventory update in a transaction to ensure atomicity.
    # If either the order save or inventory update fails, both operations will be rolled back.
    # This prevents partial updates that could lead to data inconsistency.
    context.order.unstock_inventory! { context.order.save! }
  rescue StandardError => e
    context.fail!(message: "Failed to save order: #{e.message}")
  end
end

#construct_orderObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/interactors/spree_cm_commissioner/order_importer/multi_guest.rb', line 22

def construct_order
  Spree::Order.new(
    email: params[:order_email],
    phone_number: params[:order_phone_number],
    completed_at: Date.current,
    state: 'complete',
    payment_state: 'paid',
    line_items_attributes: [
      {
        quantity: quantity.to_i,
        variant_id: params[:variant_id]
      }
    ]
  )
end