Module: CommandTower::Messaging::Endpoints::Verification

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

Constant Summary collapse

TRANSITIONS =
{
  "unverified" => %w[pending verified failed].freeze,
  "pending" => %w[verified failed unverified].freeze,
  "verified" => %w[unverified failed].freeze,
  "failed" => %w[pending unverified].freeze,
}.freeze

Class Method Summary collapse

Class Method Details

.apply!(record, to:) ⇒ Object



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

def apply!(record, to:)
  from = record.verification_state
  assert_transition!(from:, to:)
  record.verification_state = to.to_s
  case to.to_s
  when "verified"
    record.verified_at = Time.current
  when "unverified"
    record.verified_at = nil
  end
  record
end

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



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

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

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