diff --git a/examples/service_objects/Rakefile b/examples/service_objects/Rakefile index d8638b5..18141fb 100644 --- a/examples/service_objects/Rakefile +++ b/examples/service_objects/Rakefile @@ -13,62 +13,24 @@ end desc 'creates an account and an owner user through BCDD::Result' task bcdd_result_transitions: %i[config] do - result = nil - - bench = Benchmark.measure do - result = Account::OwnerCreation.call( - owner: { - name: "\tJohn Doe \n", - email: ' JOHN.doe@email.com', - password: '123123123', - password_confirmation: '123123123' - } - ) - end - - p result - - puts "\nBenchmark: #{bench}" -end - -desc 'creates an account and an owner user directly through ActiveRecord' -task raw_active_record: %i[config] do - require_relative 'config' - - result = nil - - bench = Benchmark.measure do - email = 'john.doe@email.com' - - ActiveRecord::Base.transaction do - User.exists?(email:) and raise "User with email #{email} already exists" - - user = User.create!( - uuid: ::SecureRandom.uuid, - name: 'John Doe', - email:, - password: '123123123', - password_confirmation: '123123123' - ) - - executed_at = ::Time.current - - user.token.nil? or raise "User with email #{email} already has a 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 - ) - - account = Account.create!(uuid: ::SecureRandom.uuid) - - Account::Member.create!(account: account, user: user, role: :owner) - - result = { account: account, user: user } + result1 = Account::OwnerCreation.call( + owner: { + name: "\tJohn Doe \n", + email: ' JOHN.doe@email.com', + password: '123123123', + password_confirmation: '123123123' + } + ) + + puts result1.inspect + puts + + result2 = Account::OwnerCreation.call( + uuid: "", + owner: {} + ).on_failure(:invalid_input) do |output| + output[:input].errors.full_messages.each do |message| + puts message end end - - puts "\nBenchmark: #{bench}" end diff --git a/examples/service_objects/app/services/account/owner_creation.rb b/examples/service_objects/app/services/account/owner_creation.rb index 1be18f9..75e19c4 100644 --- a/examples/service_objects/app/services/account/owner_creation.rb +++ b/examples/service_objects/app/services/account/owner_creation.rb @@ -27,7 +27,7 @@ def call(attributes) def create_owner(owner:, **) ::User::Creation.call(owner).handle do |on| - on.success { |output| Continue(user: { record: output[:user], token: output[:token] }) } + on.success { |output| Continue(user: output[:user], token: output[:token]) } on.failure { |output| Failure(:invalid_owner, **output) } end end @@ -39,7 +39,7 @@ def create_account(uuid:, **) end def link_owner_to_account(account:, user:, **) - Member.create!(account:, user: user.fetch(:record), role: :owner) + Member.create!(account:, user:, role: :owner) Continue() end diff --git a/examples/service_objects/app/services/application_service.rb b/examples/service_objects/app/services/application_service.rb index 61d8b1b..a25d908 100644 --- a/examples/service_objects/app/services/application_service.rb +++ b/examples/service_objects/app/services/application_service.rb @@ -16,8 +16,8 @@ class << self def input=(klass) const_defined?(:Input, false) and raise ArgumentError, 'Attributes class already defined' - unless klass.is_a?(::Class) && klass < (ApplicationService::Input) - raise ArgumentError, 'must be a ApplicationService::Attributes subclass' + unless klass.is_a?(::Class) && klass < Input + raise ArgumentError, 'must be a ApplicationService::Input subclass' end const_set(:Input, klass) @@ -26,7 +26,7 @@ def input=(klass) def input(&block) return const_get(:Input, false) if const_defined?(:Input, false) - klass = ::Class.new(ApplicationService::Input) + klass = ::Class.new(Input) klass.class_eval(&block) self.input = klass @@ -52,7 +52,7 @@ def initialize(input) def call! ::BCDD::Result.transitions(name: self.class.name) do if input.invalid? - Failure(:invalid_input, **input.errors.messages) + Failure(:invalid_input, input: input) else call(input.attributes.deep_symbolize_keys) end