Sha256: 1cb608c16feed1b2cd5c7d521c5dc7de17e42de1bbcee1586f3e8c53ac48ccd0

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

class User::Token
  class Creation
    include Solid::Output.mixin

    def call(**input)
      Solid::Result.event_logs(name: self.class.name) do
        Given(input)
          .and_then(:normalize_input)
          .and_then(:validate_input)
          .and_then(:validate_token_existence)
          .and_then(:create_token)
          .and_expose(:token_created, %i[token])
      end
    end

    private

    def normalize_input(**options)
      Continue(executed_at: options.fetch(:executed_at) { ::Time.current })
    end

    def validate_input(user:, executed_at:)
      err = ::Hash.new { |hash, key| hash[key] = [] }

      err[:user] << 'must be a User' unless user.is_a?(::User)
      err[:user] << 'must be persisted' unless user.try(:persisted?)
      err[:executed_at] << 'must be a time' unless executed_at.is_a?(::Time)

      err.empty? ? Continue() : Failure(:invalid_user, **err)
    end

    def validate_token_existence(user:, **)
      user.token.nil? ? Continue() : Failure(:token_already_exists)
    end

    def create_token(user:, executed_at:, **)
      ::RuntimeBreaker.try_to_interrupt(env: 'BREAK_USER_TOKEN_CREATION')

      token = user.create_token(
        access_token: ::SecureRandom.hex(24),
        refresh_token: ::SecureRandom.hex(24),
        access_token_expires_at: executed_at + 15.days,
        refresh_token_expires_at: executed_at + 30.days
      )

      token.persisted? ? Continue(token:) : Failure(:token_creation_failed, **token.errors.messages)
    end
  end
end

Version data entries

2 entries across 1 versions & 1 rubygems

Version Path
solid-result-2.0.0 examples/multiple_listeners/app/models/user/token/creation.rb
solid-result-2.0.0 examples/single_listener/app/models/user/token/creation.rb