Class: PartnerService::Staff::CreateFromOwner

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer
Defined in:
app/services/partner_service/staff/create_from_owner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(account) ⇒ CreateFromOwner

Initializes the service with an account and checks if the staff already exists.

Parameters:

  • account (Account)

    The account object to create the staff from.



16
17
18
19
20
21
# File 'app/services/partner_service/staff/create_from_owner.rb', line 16

def initialize()
  @account = 
  @existing_staff = Staff.find_by(email: .email)
  @result = { success: false, data: nil }
  @password = nil
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'app/services/partner_service/staff/create_from_owner.rb', line 12

def password
  @password
end

#resultStaff

Returns the existing staff if found by email.

Returns:

  • (Staff)

    Returns the existing staff if found by email.



12
13
14
# File 'app/services/partner_service/staff/create_from_owner.rb', line 12

def result
  @result
end

Instance Method Details

#callself

Main entry point for the service. Executes the logic to either handle an existing staff member or create a new one. Uses transactions to ensure atomicity in case of errors.

Returns:

  • (self)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/partner_service/staff/create_from_owner.rb', line 28

def call
  return handle_existing_staff if existing_staff.present?

  ActiveRecord::Base.transaction do
    build_new_staff
    assign_roles
    save_new_staff
    update_partner_portal_usage_and_sync
  rescue StandardError => e
    error.add(:base, e.message)
    @result[:success] = false
  end

  self
end

#dataStaff?

Retrieves the resulting data, which could be the newly created staff or the existing one.

Returns:

  • (Staff, nil)

    The created or existing staff, or nil if not successful.



52
53
54
# File 'app/services/partner_service/staff/create_from_owner.rb', line 52

def data
  @result[:data]
end

#error_messageString

Retrieves a simplified error message if any error occurred during the operation.

Returns:

  • (String)

    A simple error message.



58
59
60
# File 'app/services/partner_service/staff/create_from_owner.rb', line 58

def error_message
  error_message_simple
end

#success?Boolean

Checks if the operation was successful.

Returns:

  • (Boolean)

    True if the staff creation or existing staff handling was successful.



46
47
48
# File 'app/services/partner_service/staff/create_from_owner.rb', line 46

def success?
  @result[:success]
end