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
20
21
22
23
24
25
26
27
28
29
# File 'app/services/spree_cm_commissioner/cart/remove_guest.rb', line 6

def call(order:, line_item:, guest_id: nil)
  # Release holds BEFORE mutating, using the quantities the hold was placed against
  # (Release reads live line items). Only release when the quantity will actually
  # decrease. The gate projects the pending guest removal (we evaluate before it
  # happens), unlike should_decrease_quantity? which assumes the guest is already
  # gone. Guard on success and abort if it fails.
  if will_decrease_quantity?(order, line_item, guest_id)
    release = order.release_order_holds(reason: :cart_changed)
    return release unless release.success?
  end

  # 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