Class: AuctionFunCore::Operations::UserContext::RegistrationOperation

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

Overview

Operation class for create new users.

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/registration_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
33
34
35
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 22

def call(attributes)
  values = yield validate_contract(attributes)
  values_with_encrypt_password = yield encrypt_password(values)
  values_with_confirmation_attributes = yield confirmation_attributes(values_with_encrypt_password)

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

    yield publish_user_registration(@user.id)
    yield send_welcome_email(@user.id)
  end

  Success(@user)
end

#confirmation_attributes(attrs) ⇒ Hash

Adds confirmation parameters to the given attributes.

Parameters:

  • result (Hash)

    User valid contract attributes with password

Returns:

  • (Hash)

    Valid user database



63
64
65
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 63

def confirmation_attributes(attrs)
  Success({**attrs, email_confirmation_token: generate_email_token})
end

#encrypt_password(attrs) ⇒ Hash

Transforms the password attribute, encrypting it to be saved in the database.

Parameters:

  • result (Hash)

    User valid contract attributes

Returns:

  • (Hash)

    Valid user database



52
53
54
55
56
57
58
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 52

def encrypt_password(attrs)
  attributes = attrs.to_h.except(:password, :password_confirmation)

  Success(
    {**attributes, password_digest: BCrypt::Password.create(attrs[:password])}
  )
end

#persist(result) ⇒ ROM::Struct::User

Calls the user repository class to persist the attributes in the database.

Parameters:

  • result (Hash)

    User validated attributes

Returns:

  • (ROM::Struct::User)


70
71
72
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 70

def persist(result)
  Success(user_repository.create(result))
end

#publish_user_registration(user_id) ⇒ Dry::Monads::Result::Success

Triggers the publication of event users.registration.

Parameters:

  • user_id (Integer)

    User ID

Returns:

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


77
78
79
80
81
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 77

def publish_user_registration(user_id)
  user = user_repository.by_id!(user_id)

  Success(Application[:event].publish("users.registration", user.info))
end

#send_welcome_email(user_id) ⇒ Dry::Monads::Result::Success

Schedule the asynchronous sending of a welcome email.

Parameters:

  • user_id (Integer)

    User ID

Returns:

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


86
87
88
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 86

def send_welcome_email(user_id)
  Success(registration_mailer_job.perform_async(user_id))
end

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

Calls registration contract class to perform the validation of the informed attributes.

Parameters:

  • attrs (Hash)

    user attributes

Returns:

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


41
42
43
44
45
46
47
# File 'lib/auction_fun_core/operations/user_context/registration_operation.rb', line 41

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

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

  Success(contract.to_h)
end