Class: SpreeCmCommissioner::Cart::AddGuest

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/cart/add_guest.rb

Instance Method Summary collapse

Instance Method Details

#call(order:, line_item:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/spree_cm_commissioner/cart/add_guest.rb', line 6

def call(order:, line_item:)
  # Lock the line item (SELECT ... FOR UPDATE) so concurrent guest add/remove
  # on the same line item are serialized. with_lock reloads the record inside
  # the lock, so quantity/guests reflect the latest committed state before we
  # read-modify-write them. Without this, two simultaneous requests would
  # clobber each other's quantity update.
  line_item.with_lock do
    create_blank_guest(line_item)
    increase_quantity(line_item) if should_increase_quantity?(line_item)
    recalculate_cart(order, line_item)
  end

  success(order: order, line_item: line_item)
rescue ActiveRecord::RecordInvalid => e
  # Convert ONLY the out-of-stock failure into a failure result. The stock validator
  # tags its error with the type :selected_quantity_not_available (see
  # Spree::Stock::AvailabilityValidator), so we match on that type rather than on any
  # RecordInvalid. The raise rolls back the lock transaction (so the blank guest is
  # not persisted); returning it as a failure lets the caller render the overridden
  # availability message instead of a 500.
  #
  # Anything else a different line item validation, or an invalid order/shipment
  # raised by recalculate_cart is re-raised so genuine backend errors are not
  # masked behind an empty quantity error.
  raise unless e.record == line_item && e.record.errors.of_kind?(:quantity, :selected_quantity_not_available)

  failure(e.record)
end