Sha256: d8e217860c20a3720357a450617d92339d0600f29d40eec13cb8c1a4a8261e27

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

class Account
  class OwnerCreation < ::BCDD::Process
    include BCDD::Result::RollbackOnFailure

    input do
      attribute :uuid, contract: :is_uuid, default: -> { ::SecureRandom.uuid }, normalize: -> { _1.strip.downcase }
      attribute :owner, type: ::Hash, validate: :is_present
    end

    output do
      Failure(
        invalid_owner: ::Hash,
        invalid_account: :errors_by_attribute,
      )

      user = contract[::User] & :is_persisted
      account = contract[::Account] & :is_persisted

      Success(account_owner_created: { account:, user: })
    end

    def call(**input)
      Given(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[account user])
    end

    private

    def create_owner(owner:, **)
      ::User::Creation.call(**owner).handle do |on|
        on.success { |output| Continue(user: output[:user]) }
        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_account, **account.errors.messages)
    end

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

      Continue()
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcdd-process-0.2.0 examples/business_processes/app/models/account/owner_creation.rb