Class: AuctionFunCore::Operations::UserContext::PhoneConfirmationOperation

Inherits:
Base
  • Object
show all
Defined in:
lib/auction_fun_core/operations/user_context/phone_confirmation_operation.rb

Overview

Operation class for confirm users by token via phone.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(attributes, &block) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/auction_fun_core/operations/user_context/phone_confirmation_operation.rb', line 13

def self.call(attributes, &block)
  operation = new.call(attributes)

  return operation unless block

  Dry::Matcher::ResultMatcher.call(operation, &block)
end

Instance Method Details

#call(attributes) ⇒ Object

TODO:

Add custom doc



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/auction_fun_core/operations/user_context/phone_confirmation_operation.rb', line 22

def call(attributes)
  user = yield validate_contract(attributes)

  user_repository.transaction do |_t|
    user = yield persist(user.id)

    yield publish_user_confirmation(user.id, user.confirmed_at)
  end

  Success(user)
end

#persist(user_id, time = Time.current) ⇒ ROM::Struct::User

Calls the user repository class to update database fields.

Parameters:

  • user_id (Integer)

    User ID

  • time (Time) (defaults to: Time.current)

    Confirmation time

Returns:

  • (ROM::Struct::User)


50
51
52
53
54
# File 'lib/auction_fun_core/operations/user_context/phone_confirmation_operation.rb', line 50

def persist(user_id, time = Time.current)
  result = {phone_confirmation_token: nil, phone_confirmed_at: time, confirmed_at: time}

  Success(user_repository.update(user_id, result))
end

#publish_user_confirmation(user_id, time = Time.current) ⇒ Dry::Monads::Result::Success

Triggers the publication of event users.registration.

Parameters:

  • user_id (Integer)

    User ID

Returns:

  • (Dry::Monads::Result::Success)


59
60
61
62
63
# File 'lib/auction_fun_core/operations/user_context/phone_confirmation_operation.rb', line 59

def publish_user_confirmation(user_id, time = Time.current)
  Success(
    Application[:event].publish("users.confirmation", {user_id: user_id, time: time})
  )
end

#validate_contract(attrs) ⇒ Dry::Monads::Result::Success, Dry::Monads::Result::Failure

Calls the authentication contract class to perform the validation and authentication of the informed attributes.

Parameters:

  • attrs (Hash)

    user attributes

Returns:

  • (Dry::Monads::Result::Success, Dry::Monads::Result::Failure)


38
39
40
41
42
43
44
# File 'lib/auction_fun_core/operations/user_context/phone_confirmation_operation.rb', line 38

def validate_contract(attrs)
  contract = phone_confirmation_contract.call(attrs)

  return Failure(contract.errors.to_h) if contract.failure?

  Success(contract.context[:user])
end