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

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_email is 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'] is nil (no account on server side), this case will not do anything.
  • If the target_email and the current_email are the same or empty, this case will not do anything.

Parameters:

  • person (Ecoportal::API::V1::Person)

    the person we want to update, carrying the changes to be done.

  • dest_email (String, Proc)

    the email that we will move the other account to, when we free up target_email.

  • target_email (String) (defaults to: nil)

    the email that we want to free up from another account and bring to ours. If it's empty, the person.email will be used instead.

  • options (Hash) (defaults to: {})

    the options.

  • current_email (String)

    the email that the person's account is currently linked. As the current email should be associated with this person's account on server side, we use original_doc['email'].

  • context (String) (defaults to: 'Session')

    main core part of logs. Provides context to the logs.



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 ( # rubocop:disable Metrics/AbcSize
  person,
  dest_email:,
  target_email: nil,
  options:      {},
  context:      'Session'
)
  return false if options.dig(:exclude, :account)
  return false unless (     = person.)
  return false unless ( = person.original_doc['account']) # rubocop:disable Lint/UselessAssignment

  target_email  ||= person.email
     = person.original_doc['email']

  return false unless target_email != 
  return false if .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   != 
    return false if dest_email.to_s.strip.empty?
  end

   = ()
  person.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: , 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: , 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: , 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