Sha256: 162a5a5f519050f7c424c351eb40ebeae8287ff9ebe7fb1612e7ba5dbe6034dd

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

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

    input do
      attribute :uuid, value: :uuid
      attribute :owner, type: ::Hash, contract: :is_present
    end

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

      Success account_owner_created: {
        user: contract[::User] & :is_persisted,
        account: contract[::Account] & :is_persisted
      }
    end

    def call(**input)
      rollback_on_failure {
        Given(input)
          .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.4.0 examples/business_processes/app/models/account/owner_creation.rb