Class: Account::Inviter

Inherits:
BaseService show all
Defined in:
app/services/account/inviter.rb

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



8
9
10
# File 'app/services/account/inviter.rb', line 8

def 
  @account
end

#partyObject (readonly)

Returns the value of attribute party.



8
9
10
# File 'app/services/account/inviter.rb', line 8

def party
  @party
end

Class Method Details

.determine_login(email) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/account/inviter.rb', line 39

def self.(email)
   = email
  if Account.where(login: ).exists?
    name_part,domain = email.split('@')
     = name_part
    counter = 0
     = 
    while Account.where(login: ).exists?
      counter += 1
       = "#{}-#{counter}"
    end
  end
  
end

Instance Method Details

#destroy_existing_account!(party, existing_account, email) ⇒ Object (protected)



56
57
58
59
60
61
62
63
# File 'app/services/account/inviter.rb', line 56

def destroy_existing_account!(party, , email)
  ErrorReporting.warning(
    "[Account::Inviter] destroying existing account #{.id} on party #{party.id} before re-inviting",
    party_id: party.id, account_id: .id, account_email: .email
  )
  .destroy
  party.
end

#existing_account_result(existing_account) ⇒ Object (protected)



73
74
75
# File 'app/services/account/inviter.rb', line 73

def ()
  Result.new(invited: false, account: , messages: ["This customer already has an online account (#{.}). Use the existing account page to resend the invitation or reset their password."])
end

#login_in_use_result(login) ⇒ Object (protected)



69
70
71
# File 'app/services/account/inviter.rb', line 69

def ()
  Result.new(invited: false, messages: ["The login you requested #{} is already in use."])
end

#missing_email_result(party) ⇒ Object (protected)



65
66
67
# File 'app/services/account/inviter.rb', line 65

def missing_email_result(party)
  Result.new(invited: false, messages: ["No Email for party #{party.id}"])
end

#process(party:, email: nil, login: nil, skip_invitation_delivery: false, return_path: nil, force_reinvite: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/account/inviter.rb', line 10

def process(party:, email: nil, login: nil, skip_invitation_delivery: false,
            return_path: nil, force_reinvite: false)
  email ||= party.email

  return missing_email_result(party) unless email

  existing = Account.find_by(party_id: party.id)
  if existing
    if force_reinvite
      destroy_existing_account!(party, existing, email)
    else
      return (existing)
    end
  end

   =  || self.class.(email)
  return () if Account.where(login: ).exists?

   = Account.invite!(login: , email: email, name: party.full_name) do |a|
    a.party_id = party.id
    a.return_path_for_invite = return_path
    a.skip_invitation = true
    a.set_default_marketing_preferences(I18n.locale)
  end
  .deliver_invitation unless skip_invitation_delivery

  Result.new(invited: .persisted?, email: email, account: , messages: .errors.full_messages)
end