Sha256: 29056e7791637a084e1485f078d1b87d29add63fe9e583183ff3994c5bfd92ce

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

class Account
  class OwnerCreation
    include BCDD::Context.mixin
    include BCDD::Result::RollbackOnFailure

    def call(**input)
      BCDD::Result.event_logs(name: self.class.name) do
        Given(input)
          .and_then(:normalize_input)
          .and_then(:validate_input)
          .then { |result|
            rollback_on_failure {
              result
                .and_then(:create_owner)
                .and_then(:create_account)
                .and_then(:link_owner_to_account)
            }
          }.and_expose(:account_owner_created, %i[user account])
      end
    end

    private

    def normalize_input(**options)
      uuid = String(options.fetch(:uuid) { ::SecureRandom.uuid }).strip.downcase

      Continue(uuid:)
    end

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

      err[:uuid] << 'must be an UUID' unless uuid.match?(/\A[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}\z/i)
      err[:owner] << 'must be a Hash' unless owner.is_a?(::Hash)

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

    def create_owner(owner:, **)
      ::User::Creation.new.call(**owner).handle do |on|
        on.success { |output| Continue(user: { record: output[:user], token: output[:token] }) }
        on.failure { |output| Failure(:invalid_owner, **output) }
      end
    end

    def create_account(uuid:, **)
      ::RuntimeBreaker.try_to_interrupt(env: 'BREAK_ACCOUNT_CREATION')

      account = ::Account.create(uuid:)

      account.persisted? ? Continue(account:) : Failure(:invalid_record, **account.errors.messages)
    end

    def link_owner_to_account(account:, user:, **)
      Member.create!(account:, user: user.fetch(:record), role: :owner)

      Continue()
    end
  end
end

Version data entries

4 entries across 2 versions & 1 rubygems

Version Path
bcdd-result-1.1.0 examples/multiple_listeners/app/models/account/owner_creation.rb
bcdd-result-1.1.0 examples/single_listener/app/models/account/owner_creation.rb
bcdd-result-1.0.0 examples/multiple_listeners/app/models/account/owner_creation.rb
bcdd-result-1.0.0 examples/single_listener/app/models/account/owner_creation.rb