Class: SpreeCmCommissioner::InventoryHolds::Convert

Inherits:
Object
  • Object
show all
Extended by:
ServiceModuleThrowable
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/inventory_holds/convert.rb

Constant Summary collapse

HoldExpiredError =
Class.new(StandardError)

Instance Method Summary collapse

Methods included from ServiceModuleThrowable

call!

Instance Method Details

#call(order:) ⇒ Object



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
34
35
# File 'app/services/spree_cm_commissioner/inventory_holds/convert.rb', line 9

def call(order:)
  hold = InventoryHold.find_by(order: order)
  return success(order) if hold.nil?
  return success(order) if hold.converted?
  return failure(nil, hold_expired_message) if hold.finalized?

  InventoryHold.transaction do
    hold.lock!

    # Finalized (converted or released) between the check above and acquiring this lock —
    # e.g. ReleaseJob won the race, or a concurrent completion attempt for the same order
    # converted it first. Strict: fail either way rather than silently treating "someone
    # else already converted it" as success — a losing duplicate request should surface as
    # a failure, not a false success for work it didn't do.
    raise HoldExpiredError, hold_expired_message if hold.finalized?

    hold.update!(status: :converted)
    convert_hold_redis!(order)
    enqueue_sync_inventory(order)
  end

  success(order)
rescue StandardError => e
  error = { error_type: e.class.name.demodulize, order_id: order.id, message: e.message }
  CmAppLogger.error(label: "#{self.class.name}#call failed", data: error)
  failure(nil, e.message)
end