Class: Account::Inviter
- Inherits:
-
BaseService
- Object
- BaseService
- Account::Inviter
- Defined in:
- app/services/account/inviter.rb
Overview
Service object: inviter.
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#party ⇒ Object
readonly
Returns the value of attribute party.
Attributes inherited from BaseService
Class Method Summary collapse
Instance Method Summary collapse
- #destroy_existing_account!(party, existing_account, _email) ⇒ Object protected
- #existing_account_result(existing_account) ⇒ Object protected
- #login_in_use_result(login) ⇒ Object protected
- #missing_email_result(party) ⇒ Object protected
- #process(party:, email: nil, login: nil, skip_invitation_delivery: false, return_path: nil, force_reinvite: false) ⇒ Object
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
10 11 12 |
# File 'app/services/account/inviter.rb', line 10 def account @account end |
#party ⇒ Object (readonly)
Returns the value of attribute party.
10 11 12 |
# File 'app/services/account/inviter.rb', line 10 def party @party end |
Class Method Details
.determine_login(email) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/services/account/inviter.rb', line 40 def self.determine_login(email) login = email if Account.where(login: login).exists? name_part, = email.split('@') base_login = name_part counter = 0 login = base_login while Account.where(login: login).exists? counter += 1 login = "#{base_login}-#{counter}" end end login end |
Instance Method Details
#destroy_existing_account!(party, existing_account, _email) ⇒ Object (protected)
57 58 59 60 61 62 63 64 |
# File 'app/services/account/inviter.rb', line 57 def destroy_existing_account!(party, existing_account, _email) ErrorReporting.warning( "[Account::Inviter] destroying existing account #{existing_account.id} on party #{party.id} before re-inviting", party_id: party.id, account_id: existing_account.id, account_email: existing_account.email ) existing_account.destroy party.reset_account end |
#existing_account_result(existing_account) ⇒ Object (protected)
74 75 76 |
# File 'app/services/account/inviter.rb', line 74 def existing_account_result(existing_account) Result.new(invited: false, account: existing_account, messages: ["This customer already has an online account (#{existing_account.login}). Use the existing account page to resend the invitation or reset their password."]) end |
#login_in_use_result(login) ⇒ Object (protected)
70 71 72 |
# File 'app/services/account/inviter.rb', line 70 def login_in_use_result(login) Result.new(invited: false, messages: ["The login you requested #{login} is already in use."]) end |
#missing_email_result(party) ⇒ Object (protected)
66 67 68 |
# File 'app/services/account/inviter.rb', line 66 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
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 38 |
# File 'app/services/account/inviter.rb', line 12 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 return existing_account_result(existing) unless force_reinvite destroy_existing_account!(party, existing, email) end login ||= self.class.determine_login(email) return login_in_use_result(login) if Account.where(login: login).exists? account = Account.invite!(login: 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 account.deliver_invitation unless skip_invitation_delivery Result.new(invited: account.persisted?, email: email, account: account, messages: account.errors.) end |