Sha256: e6c18c28e3782ff1d6d1d7a63fb85657a6b706059734b9a12be4d2a2e2980bd8
Contents?: true
Size: 1.25 KB
Versions: 3
Compression:
Stored size: 1.25 KB
Contents
# frozen_string_literal: true class User::Token class Creation < ::BCDD::Process input do attribute :user, contract: contract[::User] & :is_persisted attribute :executed_at, contract: contract[::Time], default: -> { ::Time.current } end output do Failure( token_already_exists: :empty_hash, token_creation_failed: :errors_by_attribute ) Success token_created: { token: contract[User::Token] & :is_persisted } end def call(**input) Given(input) .and_then(:validate_token_existence) .and_then(:create_token) .and_expose(:token_created, %i[token]) end private 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
3 entries across 3 versions & 1 rubygems