Module: CommandTower::Messaging::Endpoints::Lifecycle

Defined in:
app/services/command_tower/messaging/endpoints/lifecycle.rb

Constant Summary collapse

TRANSITIONS =
{
  "active" => %w[revoked invalid retired].freeze,
  "revoked" => %w[retired].freeze,
  "invalid" => %w[retired].freeze,
  "retired" => [].freeze,
}.freeze

Class Method Summary collapse

Class Method Details

.apply!(record, to:, revoked_at: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'app/services/command_tower/messaging/endpoints/lifecycle.rb', line 24

def apply!(record, to:, revoked_at: nil)
  from = record.lifecycle_state
  assert_transition!(from:, to:)
  record.lifecycle_state = to.to_s
  if to.to_s == "revoked"
    record.revoked_at = revoked_at || Time.current
  end
  record.derive_uniqueness_columns!
  record
end

.assert_transition!(from:, to:) ⇒ Object



16
17
18
19
20
21
22
# File 'app/services/command_tower/messaging/endpoints/lifecycle.rb', line 16

def assert_transition!(from:, to:)
  allowed = TRANSITIONS.fetch(from.to_s) { [] }
  return if allowed.include?(to.to_s)

  raise InvalidTransitionError,
        "illegal lifecycle transition: #{from.inspect}#{to.inspect}"
end