Class: SpreeCmCommissioner::Cart::RemoveGuest

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

Instance Method Summary collapse

Instance Method Details

#call(order:, line_item:, guest_id: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/spree_cm_commissioner/cart/remove_guest.rb', line 6

def call(order:, line_item:, guest_id: nil)
  # 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
    remove_guest(order, line_item, guest_id) if guest_id.present?
    decrease_quantity(order, line_item) if should_decrease_quantity?(order, line_item)
    recalculate_cart(order, line_item)

    success(order: order, line_item: line_item)
  end
end