Class: AuctionFunCore::Operations::StaffContext::RegistrationOperation

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

Overview

Operation class for create new staff member.

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/staff_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



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

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

  staff_repository.transaction do |_t|
    @staff = yield persist(values_with_encrypt_password)

    yield publish_staff_registration(@staff.id)
  end

  Success(@staff)
end

#encrypt_password(attrs) ⇒ Hash

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

Parameters:

  • result (Hash)

    Staff valid contract attributes

Returns:

  • (Hash)

    Valid staff database



50
51
52
53
54
55
56
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 50

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

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

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

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

Parameters:

  • result (Hash)

    Staff validated attributes

Returns:

  • (ROM::Struct::Staff)


61
62
63
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 61

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

#publish_staff_registration(staff_id) ⇒ Dry::Monads::Result::Success

Triggers the publication of event staffs.registration.

Parameters:

  • staff_id (Integer)

    Staff ID

Returns:

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


68
69
70
71
72
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 68

def publish_staff_registration(staff_id)
  staff = staff_repository.by_id!(staff_id)

  Success(Application[:event].publish("staffs.registration", staff.info))
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)

    staff attributes

Returns:

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


39
40
41
42
43
44
45
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 39

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

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

  Success(contract.to_h)
end