Class: Eco::API::UseCases::Default::People::Utils::TransferAccountCase

Inherits:
Common::Loaders::UseCase show all
Defined in:
lib/eco/api/usecases/default/people/utils/transfer_account_case.rb

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods inherited from Common::Loaders::UseCase

cli, cli!, #cli_apply!, #initialize, type, #type

Methods inherited from Common::Loaders::CaseBase

#name, name_only_once!

Methods inherited from Common::Loaders::Base

<=>, created_at, #initialize, set_created_at!

Methods included from Common::ClassHelpers

#class_resolver, #descendants, #descendants?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant

Methods included from Language::AuxiliarLogger

#log

Constructor Details

This class inherits a constructor from Eco::API::Common::Loaders::UseCase

Instance Method Details

#main(entries, _people, session, options, usecase) ⇒ Void

Note:
  • the csv should contain a column destination-id containing the person id or external_id of the person that will be receiving the account.
  • when running this case, it is recommended to use the option -skip-batch-policies
  • it is highly recommended to either refresh the cache with -get-people or use the -get-partial option.

Usecase to actually transfer a user/account from one person to another person in the organization.

  • invocation command: -transfer-account-from

These are the steps and jobs it does:

  1. pair person entries (note: the destination-id entry could not be present, it will add it in such a case).
  2. retrieve from server persons that were not included in people.
  3. validation
    • a person should only receive account from just one user
    • a person should only give account to just one user
    • every account giver should have account
  4. create jobs
    • move giver's and receiver's accounts to dummy email addresses.
    • free up giver's and receiver's accounts.
    • switch email: set receiver's email to that of giver's dummy address.
      • to ensure account transfer, as we moved accounts to dummy emails, those dummy addresses should be used
    • invite receivers: adds account to the destination person in the dummy email.
      • actual user/account transfer to the person/receiver
      • no notification will be recived by the user, because of the dummy address at this stage
    • restore email: sets the receiver email from the dummy address to the final email.
      • the final email inbox will receive a notification of email change

Parameters:

Options Hash (options):

  • :include (Hash<Symbol, Object>)

    things that should be included.

    • :email (Boolean) [false] if the email should be transferred as well (command option: -include-email)
  • :skip (Hash<Symbol, Object>)

    things that should be excluded.

    • :api_policies (Boolean) [false] if the api policies should be skipped (command option: -skip-api-policies)

Returns:

  • (Void)


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
# File 'lib/eco/api/usecases/default/people/utils/transfer_account_case.rb', line 42

def main(entries, _people, session, options, usecase) # rubocop:disable Metrics/AbcSize
  move     = session.new_job("main", "move email accounts", :update, usecase, :account)
  free     = session.new_job("main", "free up accounts",    :update, usecase, :account)
  switch   = session.new_job("main", "switch email",        :update, usecase, :core)
  invite   = session.new_job("post", "invite receivers",    :update, usecase, :account)
  restore  = session.new_job("post", "restore email",       :update, usecase, :core)

  with_each_person_pair(entries) do |src_person, dst_person|
    # capture actual initial information
    src_doc        = src_person..doc
    src_email      = src_person.email
    src_dummy      = dummy_email(src_person)
    dst_email      = dst_person.email
    dst_dummy      = dummy_email(dst_person)
    copy_src_email = options.dig(:include, :email) || !dst_person.
    dst_end_email  = copy_src_email ? src_email : dst_email

    # account email renamings are necessary to avoid uncertainty and ensure no email taken error
    move.add(dst_person) {|dst| dst.email = dst_dummy}
    move.add(src_person) {|src| src.email = src_dummy}
    # free accounts up!
    free.add([dst_person, src_person]) {|person| person. = nil}
    # to effectivelly transfer the user/account, email should be the same during invite
    # otherwise the account doesn't actually get transferred but just copied
    switch.add(dst_person)  {|dst| dst.email = src_dummy}
    # do the actual transfer of account
    invite.add(dst_person)  {|dst| dst. = src_doc}
    # recover the original email, if the receiver had account
    restore.add(dst_person) {|dst| dst.email = dst_end_email}
  end.tap do |units|
    next unless options[:simulate]

    units.each do |unit|
      puts unit.persons.map(&:external_id).join(" --> ")
    end
  end
end