Class: Eco::API::UseCases::Default::People::Utils::TransferAccountCase
- Inherits:
-
Common::Loaders::UseCase
- Object
- Loaders::Base
- Common::Loaders::CaseBase
- Common::Loaders::UseCase
- Eco::API::UseCases::Default::People::Utils::TransferAccountCase
- Defined in:
- lib/eco/api/usecases/default/people/utils/transfer_account_case.rb
Instance Method Summary collapse
-
#main(entries, _people, session, options, usecase) ⇒ Void
Usecase to actually transfer a user/account from one person to another person in the organization.
Methods inherited from Common::Loaders::UseCase
Methods included from Common::Loaders::UseCase::CliIdentify
Methods included from Common::Loaders::UseCase::TargetModel
Methods included from Common::Loaders::UseCase::Type
Methods inherited from Common::Loaders::CaseBase
#name, name_only_once!, original_name
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
csvshould contain a columndestination-idcontaining the personidorexternal_idof 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-peopleor use the-get-partialoption.
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.
* dummy email pattern: from name@domain.ltd --to--> demo+name.domain.ltd@ecoportal.co.nz
- **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_
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, , 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.account.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 = .dig(:include, :email) || !dst_person.account 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.account = 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.account = 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 [:simulate] units.each do |unit| puts unit.persons.map(&:external_id).join(" --> ") end end end |