Class: SpreeCmCommissioner::Orders::Find

Inherits:
Object
  • Object
show all
Defined in:
app/finders/spree_cm_commissioner/orders/find.rb

Constant Summary collapse

CURRENT_CART_STATES =
%w[cart address].freeze
CART_MAX_AGE_IN_MINUTES =

Default: 12 hours

ENV.fetch('CART_MAX_AGE_IN_MINUTES', '720').to_i

Instance Method Summary collapse

Instance Method Details

#cart_too_old?(order) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'app/finders/spree_cm_commissioner/orders/find.rb', line 50

def cart_too_old?(order)
  return false if order.nil?

  # Treat the cart as active based on its last update time, not its creation time.
  order.updated_at < CART_MAX_AGE_IN_MINUTES.minutes.ago
end

#execute(store:, user:, currency:, token: nil, state: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/finders/spree_cm_commissioner/orders/find.rb', line 10

def execute(store:, user:, currency:, token: nil, state: nil)
  state = Array(state).map(&:to_s)

  params = { store_id: store.id, currency: currency }
  params[:state] = state if state.present?

  order = if token.present?
            find_by_token(params, token)
          elsif user.present?
            find_by_user(params, user)
          end

  # Only enforce hold and age expiration when fetching current cart states ('cart' or 'address').
  # For other states (like 'payment,complete'), we want to return the order even if it's old or expired
  # because some pages on client still need some specific state of order to display.
  if state.intersect?(CURRENT_CART_STATES)
    return nil if hold_expired?(order)
    return nil if cart_too_old?(order)
  end

  order
end

#find_by_token(params, token) ⇒ Object



33
34
35
36
# File 'app/finders/spree_cm_commissioner/orders/find.rb', line 33

def find_by_token(params, token)
  params[:token] = token
  scope.find_by(params)
end

#find_by_user(params, user) ⇒ Object



38
39
40
41
# File 'app/finders/spree_cm_commissioner/orders/find.rb', line 38

def find_by_user(params, user)
  params[:user_id] = user.id
  scope.order(created_at: :desc).find_by(params)
end

#hold_expired?(order) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'app/finders/spree_cm_commissioner/orders/find.rb', line 43

def hold_expired?(order)
  return false if order.nil?

  # Check if the order has a hold_expires_at column and if it has already passed
  order.hold_expires_at.present? && order.hold_expires_at < Time.current
end