Module: Eco::API::MicroCases::People::Macro::TakeEmail
- Included in:
- Eco::API::MicroCases::People::Macro
- Defined in:
- lib/eco/api/microcases/people/macro/take_email.rb
Instance Method Summary collapse
-
#take_email_from_account(person, dest_email:, target_email: nil, options: {}, context: 'Session') ⇒ Object
Frees up
target_emailfrom an account not present in this org.
Instance Method Details
#take_email_from_account(person, dest_email:, target_email: nil, options: {}, context: 'Session') ⇒ Object
Note:
- It does not do the final update to the server to the
target_email. You will need to do this part yourself. - You would call this function only when you got an error of
email already taken. - If the
target_emailis associated to a user in the same org, this will fail.
Frees up target_email from an account not present in this org.
Allows to force target_email on the current user's account.
- If the person does not have account, this case will not do anything.
- If
original_doc['account']isnil(no account on server side), this case will not do anything. - If the
target_emailand thecurrent_emailare the same or empty, this case will not do anything.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/eco/api/microcases/people/macro/take_email.rb', line 29 def take_email_from_account( # rubocop:disable Metrics/AbcSize person, dest_email:, target_email: nil, options: {}, context: 'Session' ) return false if .dig(:exclude, :account) return false unless (account = person.account) return false unless (had_account = person.original_doc['account']) # rubocop:disable Lint/UselessAssignment target_email ||= person.email account_email = person.original_doc['email'] return false unless target_email != account_email return false if account_email.to_s.strip.empty? return false if target_email.to_s.strip.empty? if dest_email.is_a?(String) return false unless target_email != dest_email return false unless dest_email != account_email return false if dest_email.to_s.strip.empty? end account_json = _take_email_account_json(account) person.email = account_email if (success = _take_email_remove_account!(person, context: context)) if (success = _take_email_acquire_account!(person, target_email, account: {}, context: context)) if (success = _take_email_email_free_up!(person, dest_email: dest_email, context: context)) if (success = _take_email_remove_account!(person, context: context)) # Bring back the original account if (success = _take_email_acquire_account!(person, account_email, account: account_json, context: context)) # rubocop:disable Style/SoleNestedConditional success = true person.email = target_email end end else # free up target email # restore reverted = false if reverted ||= _take_email_remove_account!(person, context: context) reverted ||= _take_email_acquire_account!(person, account_email, account: account_json, context: context) end unless reverted msg = "Could not revert back to the original account #{person.identify}" log(:debug) { msg } puts msg end success = false end else # aquire other account # restore unless _take_email_acquire_account!(person, account_email, account: account_json, context: context) msg = 'Could not bring back the original account that ' msg << "we want to update the email to '#{target_email}' #{person.identify}" log(:debug) { msg } puts msg end success = false end end success end |