Class: UsageCredits::Fulfillment

Inherits:
ApplicationRecord show all
Defined in:
lib/usage_credits/models/fulfillment.rb

Overview

A Fulfillment represents a credit-giving action triggered by a purchase, including credit pack purchases and subscriptions. Some of this credit-giving actions are repeating in nature (i.e.: subscriptions), some are not (one-time purchases)

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/usage_credits/models/fulfillment.rb', line 48

def active?
  !stopped?
end

#calculate_next_fulfillmentObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/usage_credits/models/fulfillment.rb', line 52

def calculate_next_fulfillment
  return nil unless recurring?
  return nil if stopped?
  return nil if next_fulfillment_at.nil?

  # If next_fulfillment_at is in the past (e.g. due to missed fulfillments or errors),
  # we use current time as the base to avoid scheduling multiple rapid fulfillments.
  # This ensures smooth recovery from missed fulfillments by scheduling the next one
  # from the current time rather than the missed fulfillment time.
  base_time = next_fulfillment_at > Time.current ? next_fulfillment_at : Time.current

  base_time + UsageCredits::PeriodParser.parse_period(fulfillment_period)
end

#due_for_fulfillment?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/usage_credits/models/fulfillment.rb', line 32

def due_for_fulfillment?
  return false unless next_fulfillment_at.present?
  return false if stopped?
  return false if last_fulfilled_at.present? && next_fulfillment_at <= last_fulfilled_at

  next_fulfillment_at <= Time.current
end

#recurring?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/usage_credits/models/fulfillment.rb', line 40

def recurring?
  fulfillment_period.present?
end

#stopped?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/usage_credits/models/fulfillment.rb', line 44

def stopped?
  stops_at.present? && stops_at <= Time.current
end