eco-helpers 1.4.0 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +150 -2
  3. data/LICENSE +21 -0
  4. data/eco-helpers.gemspec +11 -10
  5. data/lib/eco/api.rb +3 -0
  6. data/lib/eco/api/common.rb +5 -1
  7. data/lib/eco/api/common/base_loader.rb +54 -0
  8. data/lib/eco/api/common/class_auto_loader.rb +109 -0
  9. data/lib/eco/api/common/class_helpers.rb +33 -0
  10. data/lib/eco/api/common/class_hierarchy.rb +1 -1
  11. data/lib/eco/api/common/class_meta_basics.rb +16 -0
  12. data/lib/eco/api/common/loaders.rb +13 -0
  13. data/lib/eco/api/common/loaders/error_handler.rb +41 -0
  14. data/lib/eco/api/common/loaders/parser.rb +127 -0
  15. data/lib/eco/api/common/loaders/policy.rb +25 -0
  16. data/lib/eco/api/common/loaders/use_case.rb +40 -0
  17. data/lib/eco/api/common/people/default_parsers.rb +5 -10
  18. data/lib/eco/api/common/people/default_parsers/boolean_parser.rb +13 -23
  19. data/lib/eco/api/common/people/default_parsers/csv_parser.rb +20 -35
  20. data/lib/eco/api/common/people/default_parsers/date_parser.rb +15 -26
  21. data/lib/eco/api/common/people/default_parsers/freemium_parser.rb +20 -0
  22. data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +26 -0
  23. data/lib/eco/api/common/people/default_parsers/multi_parser.rb +15 -27
  24. data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +14 -19
  25. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +31 -0
  26. data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +15 -24
  27. data/lib/eco/api/common/people/entries.rb +54 -24
  28. data/lib/eco/api/common/people/entry_factory.rb +18 -15
  29. data/lib/eco/api/common/people/person_attribute_parser.rb +29 -12
  30. data/lib/eco/api/common/people/person_entry.rb +308 -216
  31. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +5 -2
  32. data/lib/eco/api/common/people/person_parser.rb +52 -19
  33. data/lib/eco/api/common/session/base_session.rb +3 -6
  34. data/lib/eco/api/common/session/environment.rb +2 -23
  35. data/lib/eco/api/common/session/logger.rb +4 -0
  36. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +2 -0
  37. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +9 -1
  38. data/lib/eco/api/common/version_patches/exception.rb +24 -2
  39. data/lib/eco/api/custom.rb +13 -0
  40. data/lib/eco/api/custom/error_handler.rb +20 -0
  41. data/lib/eco/api/custom/namespace.rb +7 -0
  42. data/lib/eco/api/custom/parser.rb +50 -0
  43. data/lib/eco/api/custom/policy.rb +28 -0
  44. data/lib/eco/api/custom/use_case.rb +16 -0
  45. data/lib/eco/api/error.rb +1 -0
  46. data/lib/eco/api/error/handlers.rb +10 -3
  47. data/lib/eco/api/microcases.rb +35 -0
  48. data/lib/eco/api/microcases/account_excluded.rb +24 -0
  49. data/lib/eco/api/microcases/append_usergroups.rb +19 -0
  50. data/lib/eco/api/microcases/core_excluded.rb +20 -0
  51. data/lib/eco/api/microcases/fix_default_group.rb +34 -0
  52. data/lib/eco/api/microcases/fix_filter_tags.rb +42 -0
  53. data/lib/eco/api/microcases/people_cache.rb +17 -0
  54. data/lib/eco/api/microcases/people_load.rb +59 -0
  55. data/lib/eco/api/microcases/people_refresh.rb +31 -0
  56. data/lib/eco/api/microcases/people_search.rb +65 -0
  57. data/lib/eco/api/microcases/refresh_abilities.rb +19 -0
  58. data/lib/eco/api/microcases/refresh_default_tag.rb +27 -0
  59. data/lib/eco/api/microcases/s3upload_targets.rb +39 -0
  60. data/lib/eco/api/microcases/set_account.rb +20 -0
  61. data/lib/eco/api/microcases/set_core.rb +18 -0
  62. data/lib/eco/api/microcases/set_core_with_supervisor.rb +23 -0
  63. data/lib/eco/api/microcases/set_supervisor.rb +30 -0
  64. data/lib/eco/api/microcases/strict_search.rb +19 -0
  65. data/lib/eco/api/microcases/with_each.rb +27 -0
  66. data/lib/eco/api/microcases/with_each_leaver.rb +24 -0
  67. data/lib/eco/api/microcases/with_each_present.rb +30 -0
  68. data/lib/eco/api/microcases/with_each_starter.rb +30 -0
  69. data/lib/eco/api/microcases/with_each_subordinate.rb +34 -0
  70. data/lib/eco/api/microcases/with_supervisor.rb +36 -0
  71. data/lib/eco/api/organization/people.rb +72 -35
  72. data/lib/eco/api/organization/presets_factory.rb +13 -4
  73. data/lib/eco/api/organization/presets_reference.json +9 -1
  74. data/lib/eco/api/organization/presets_values.json +4 -1
  75. data/lib/eco/api/policies.rb +11 -7
  76. data/lib/eco/api/session.rb +62 -29
  77. data/lib/eco/api/session/batch.rb +2 -45
  78. data/lib/eco/api/session/batch/base_policy.rb +7 -6
  79. data/lib/eco/api/session/batch/errors.rb +28 -4
  80. data/lib/eco/api/session/batch/feedback.rb +7 -1
  81. data/lib/eco/api/session/batch/job.rb +40 -23
  82. data/lib/eco/api/session/batch/jobs.rb +9 -4
  83. data/lib/eco/api/session/batch/jobs_groups.rb +1 -1
  84. data/lib/eco/api/session/batch/request_stats.rb +91 -58
  85. data/lib/eco/api/session/batch/status.rb +35 -31
  86. data/lib/eco/api/session/config.rb +106 -44
  87. data/lib/eco/api/session/config/api.rb +132 -7
  88. data/lib/eco/api/session/config/apis.rb +24 -25
  89. data/lib/eco/api/session/config/logger.rb +2 -2
  90. data/lib/eco/api/session/config/post_launch.rb +1 -1
  91. data/lib/eco/api/session/config/workflow.rb +8 -7
  92. data/lib/eco/api/usecases.rb +47 -33
  93. data/lib/eco/api/usecases/backup/append_usergroups_case.rb +36 -0
  94. data/lib/eco/api/usecases/backup/create_case.rb +104 -0
  95. data/lib/eco/api/usecases/backup/create_details_case.rb +31 -0
  96. data/lib/eco/api/usecases/backup/create_details_with_supervisor_case.rb +48 -0
  97. data/lib/eco/api/usecases/backup/hris_case.rb +124 -0
  98. data/lib/eco/api/usecases/backup/set_default_tag_case.rb +49 -0
  99. data/lib/eco/api/usecases/backup/set_supervisor_case.rb +41 -0
  100. data/lib/eco/api/usecases/backup/transfer_account_case.rb +90 -0
  101. data/lib/eco/api/usecases/backup/update_case.rb +112 -0
  102. data/lib/eco/api/usecases/backup/update_details_case.rb +64 -0
  103. data/lib/eco/api/usecases/backup/upsert_case.rb +114 -0
  104. data/lib/eco/api/usecases/base_case.rb +2 -0
  105. data/lib/eco/api/usecases/base_io.rb +3 -3
  106. data/lib/eco/api/usecases/default_cases.rb +23 -53
  107. data/lib/eco/api/usecases/default_cases/append_usergroups_case.rb +10 -31
  108. data/lib/eco/api/usecases/default_cases/change_email_case.rb +23 -47
  109. data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +56 -43
  110. data/lib/eco/api/usecases/default_cases/create_case.rb +15 -101
  111. data/lib/eco/api/usecases/default_cases/create_details_case.rb +11 -26
  112. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +12 -43
  113. data/lib/eco/api/usecases/default_cases/delete_sync_case.rb +11 -0
  114. data/lib/eco/api/usecases/default_cases/delete_trans_case.rb +14 -0
  115. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +10 -21
  116. data/lib/eco/api/usecases/default_cases/hris_case.rb +23 -120
  117. data/lib/eco/api/usecases/default_cases/new_email_case.rb +10 -23
  118. data/lib/eco/api/usecases/default_cases/new_id_case.rb +11 -25
  119. data/lib/eco/api/usecases/default_cases/new_id_case0.rb +14 -0
  120. data/lib/eco/api/usecases/default_cases/org_data_convert_case.rb +83 -0
  121. data/lib/eco/api/usecases/default_cases/refresh_abilities_case.rb +30 -0
  122. data/lib/eco/api/usecases/default_cases/refresh_case.rb +7 -20
  123. data/lib/eco/api/usecases/default_cases/reinvite_sync_case.rb +11 -0
  124. data/lib/eco/api/usecases/default_cases/reinvite_trans_case.rb +17 -0
  125. data/lib/eco/api/usecases/default_cases/remove_account_sync_case.rb +11 -0
  126. data/lib/eco/api/usecases/default_cases/remove_account_trans_case.rb +17 -0
  127. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +9 -19
  128. data/lib/eco/api/usecases/default_cases/restore_db_case.rb +92 -0
  129. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +32 -40
  130. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +15 -33
  131. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +66 -57
  132. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +36 -44
  133. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +40 -55
  134. data/lib/eco/api/usecases/default_cases/transfer_account_case.rb +264 -84
  135. data/lib/eco/api/usecases/default_cases/update_case.rb +15 -109
  136. data/lib/eco/api/usecases/default_cases/update_details_case.rb +14 -61
  137. data/lib/eco/api/usecases/default_cases/upsert_case.rb +16 -111
  138. data/lib/eco/api/usecases/use_case_io.rb +9 -9
  139. data/lib/eco/cli/config.rb +10 -2
  140. data/lib/eco/cli/config/default.rb +2 -1
  141. data/lib/eco/cli/config/default/input_filters.rb +58 -0
  142. data/lib/eco/cli/config/default/options.rb +60 -25
  143. data/lib/eco/cli/config/default/people.rb +4 -4
  144. data/lib/eco/cli/config/default/people_filters.rb +108 -0
  145. data/lib/eco/cli/config/default/usecases.rb +69 -32
  146. data/lib/eco/cli/config/default/workflow.rb +37 -27
  147. data/lib/eco/cli/config/filters.rb +50 -0
  148. data/lib/eco/cli/config/filters/input_filters.rb +29 -0
  149. data/lib/eco/cli/config/filters/people_filters.rb +29 -0
  150. data/lib/eco/cli/config/help.rb +49 -0
  151. data/lib/eco/cli/config/options_set.rb +17 -1
  152. data/lib/eco/cli/config/use_cases.rb +79 -53
  153. data/lib/eco/cli/scripting.rb +10 -2
  154. data/lib/eco/cli/scripting/args_helpers.rb +25 -15
  155. data/lib/eco/cli/scripting/argument.rb +1 -0
  156. data/lib/eco/cli/scripting/arguments.rb +1 -1
  157. data/lib/eco/csv.rb +8 -3
  158. data/lib/eco/csv/table.rb +1 -1
  159. data/lib/eco/data/crypto/encryption.rb +3 -0
  160. data/lib/eco/data/files/helpers.rb +6 -1
  161. data/lib/eco/language/match.rb +19 -9
  162. data/lib/eco/language/match_modifier.rb +13 -5
  163. data/lib/eco/language/models/collection.rb +77 -56
  164. data/lib/eco/language/models/parser_serializer.rb +39 -15
  165. data/lib/eco/version.rb +1 -1
  166. metadata +149 -63
  167. data/lib/eco/api/session/task.rb +0 -175
  168. data/lib/eco/api/usecases/default_case.rb +0 -19
  169. data/lib/eco/api/usecases/default_cases/delete_case.rb +0 -32
  170. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +0 -98
  171. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +0 -26
  172. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +0 -41
  173. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +0 -38
  174. data/lib/eco/cli/config/default/filters.rb +0 -70
  175. data/lib/eco/cli/config/people_filters.rb +0 -38
@@ -1,41 +1,23 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class SetSupervisorCase < DefaultCase
1
+ class Eco::API::UseCases::DefaultCases::SetSupervisorCase < Eco::API::Common::Loaders::UseCase
2
+ name "set-supervisor"
3
+ type :sync
6
4
 
7
- def process
8
- @cases.define("set-supervisor", type: :sync) do |entries, people, session, options, usecase|
9
- job = session.job_group("main").new("update", usecase: usecase, type: :update, sets: :core)
5
+ def main(entries, people, session, options, usecase)
6
+ micro = session.micro
7
+ update = session.new_job("main", "update", :update, usecase)
10
8
 
11
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
12
-
13
- entries.each.with_index do |entry, i|
14
- person = people.find(entry, strict: strict_search)
15
-
16
- if !person
17
- session.logger.error("Entry(#{i}) - this person does not exist: #{entry.to_s(:identify)}")
18
- else
19
- sup_id = entry.supervisor_id
20
- supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
21
-
22
- if !supervisor
23
- if entry.supervisor_id
24
- msg = "Entry(#{i}) - supervisor id #{entry.supervisor_id} does not exist for person: #{entry.to_s(:identify)}"
25
- session.logger.warn(msg)
26
- end
27
- else
28
- # set internal id of the supervisor (detect if actually changed)
29
- person.supervisor_id = supervisor.id
30
- job.add(person)
31
- end
32
- end
33
- end
34
- end
9
+ micro.with_each_present(entries, people, options, log_starter: true) do |entry, person|
10
+ micro.with_supervisor(entry.supervisor_id, people) do |supervisor|
11
+ if supervisor
12
+ update.add(person)
13
+ person.supervisor_id = supervisor.id unless options.dig(:exclude, :supervisor)
14
+ else
15
+ unless !entry.supervisor_id
16
+ session.logger.warn("Supervisor '#{entry.supervisor_id}' does not exist. Entry: #{entry.to_s(:identify)}")
35
17
  end
36
-
37
18
  end
38
19
  end
39
20
  end
40
21
  end
22
+
41
23
  end
@@ -1,61 +1,70 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class SwitchSupervisorCase < DefaultCase
6
-
7
- def process
8
- @cases.define("switch-supervisor", type: :transform) do |people, session, options, usecase|
9
-
10
- unless old_id = options.dig(:super, :old)
11
- msg = "You haven't specified the original supervisor. Aborting..."
12
- session.logger.error(msg)
13
- exit(1)
14
- end
15
-
16
- # we could be setting the supervisor to nil
17
- unless options[:super].key?(:new)
18
- msg = "You haven't specified the new supervisor. Aborting..."
19
- session.logger.error(msg)
20
- exit(1)
21
- end
22
-
23
- new_id = options.dig(:super, :new)
24
-
25
- unless old_sup = people.person(id: old_id, external_id: old_id, email: old_id)
26
- msg = "Couldn't find any user with that id (old-super): '#{old_id}'. Aborting..."
27
- session.logger.error(msg)
28
- exit(1)
29
- end
30
-
31
- unless new_sup = people.person(id: new_id, external_id: new_id, email: new_id)
32
- msg = "Couldn't find any user with that id (new-super): '#{new_id}'. Aborting..."
33
- session.logger.error(msg)
34
- exit(1)
35
- end
36
-
37
- people = people.supervisor_id(old_sup.id)
38
- unless people.length > 0
39
- msg = "There are no people with supervisor #{old_sup.external_id} (#{old_sup.name} - #{old_sup.email}). Aborting..."
40
- session.logger.error(msg)
41
- exit(1)
42
- end
43
-
44
- session.logger.info("Going to change supervisor '#{old_sup.name}' (#{old_sup.external_id}) to '#{new_sup.name}' (#{new_sup.external_id})")
45
-
46
- # create batch queue
47
- supers = session.job_group("main").new("update", usecase: usecase, type: :update, sets: :core)
48
-
49
- people.each.with_index do |person, i|
50
- person.supervisor_id = new_sup.id
51
- supers.add(person)
52
- end
53
-
54
- end
55
- end
56
-
57
- end
1
+ class Eco::API::UseCases::DefaultCases::SwitchSupervisorCase < Eco::API::Common::Loaders::UseCase
2
+ name "switch-supervisor"
3
+ type :transform
4
+
5
+ def main(people, session, options, usecase)
6
+ micro = session.micro
7
+ supers = session.new_job("main", "supers", :update, usecase, :core)
8
+
9
+ old_sup, new_sup = get_supers(people, session, options).tap do |supers|
10
+ inform(*supers, session.logger)
11
+ end
12
+
13
+ micro.with_each_subordinate(old_sup, people) do |subordinate|
14
+ subordinate.supervisor_id = new_sup&.id
15
+ supers.add(subordinate)
16
+ end.tap do |subordinates|
17
+ unless subordinates.length > 0
18
+ sup_str = "#{old_sup.external_id} (#{old_sup.name} - #{old_sup.email})"
19
+ session.logger.error("There are no subordinates for supervisor #{sup_str}. Aborting...")
20
+ exit(1)
58
21
  end
59
22
  end
60
23
  end
24
+
25
+ private
26
+
27
+ def get_supers(people, session, options)
28
+ micro = session.micro
29
+ old_id, new_id = get_super_ids(options)
30
+ old_sup, new_sup = [nil, nil]
31
+
32
+ micro.with_supervisor(old_id, people) do |supervisor|
33
+ unless old_sup = supervisor
34
+ session.logger.error("Couldn't find any user with that id (old-super): '#{old_id}'. Aborting...")
35
+ exit(1)
36
+ end
37
+ end
38
+
39
+ micro.with_supervisor(new_id, people) do |supervisor|
40
+ unless new_sup = supervisor
41
+ session.logger.error("Couldn't find any user with that id (new-super): '#{new_id}'. Aborting...")
42
+ exit(1)
43
+ end
44
+ end
45
+
46
+ [old_sup, new_sup]
47
+ end
48
+
49
+ def get_super_ids(options)
50
+ unless old_id = options.dig(:super, :old)
51
+ session.logger.error("You haven't specified the original supervisor. Aborting...")
52
+ exit(1)
53
+ end
54
+
55
+ # we could be setting the supervisor to nil
56
+ unless options[:super].key?(:new)
57
+ session.logger.error("You haven't specified the new supervisor. Aborting...")
58
+ exit(1)
59
+ end
60
+
61
+ [old_id, options.dig(:super, :new)]
62
+ end
63
+
64
+ def inform(old_sup, new_sup, logger)
65
+ msg = "Switching supervisor '#{old_sup.name}' (#{old_sup.external_id}) "
66
+ msg += "to '#{new_sup&.name}' (#{new_sup&.external_id})"
67
+ logger.info(msg)
68
+ end
69
+
61
70
  end
@@ -1,53 +1,45 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class ToCsvCase < DefaultCase
1
+ class Eco::API::UseCases::DefaultCases::ToCsvCase < Eco::API::Common::Loaders::UseCase
2
+ name "to-csv"
3
+ type :export
6
4
 
7
- def process
8
- @cases.define("to-csv", type: :export) do |people, session, options, usecase|
9
- unless people && !people.empty?
10
- session.logger.warn("No source people to create the file... aborting!")
11
- next false
12
- end
13
-
14
- unless file = options[:file] || options.dig(:export, :file, :name)
15
- session.logger.error("Destination file not specified")
16
- next false
17
- end
5
+ def main(people, session, options, usecase)
6
+ unless people && !people.empty?
7
+ session.logger.warn("No source people to create the file... aborting!")
8
+ return false
9
+ end
18
10
 
19
- session.logger.info("going to create file: #{file}")
20
- CSV.open(file, "w") do |csv|
21
- deps = {"supervisor_id" => {people: people}}
22
- entry = session.new_entry(people.first, dependencies: deps)
23
- header = entry.to_hash.keys
11
+ unless file = options[:file] || options.dig(:export, :file, :name)
12
+ session.logger.error("Destination file not specified")
13
+ return false
14
+ end
24
15
 
25
- if options.dig(:nice_header) || options.dig(:export, :options, :nice_header)
26
- name_maps = session.schema.fields_by_alt_id.transform_values do |fld|
27
- fld.name
28
- end.merge({
29
- "policy_group_ids" => "User Group(s)",
30
- "email" => "Email",
31
- "name" => "Name",
32
- "supervisor_id" => "Manager ID",
33
- "filter_tags" => "Locations",
34
- "default_tag" => "Default Location",
35
- "id" => "ecoPortal ID"
36
- })
37
- header = header.map {|name| name_maps[name] ? name_maps[name] : name}
38
- end
16
+ session.logger.info("going to create file: #{file}")
17
+ CSV.open(file, "w") do |csv|
18
+ deps = {"supervisor_id" => {people: people}}
19
+ entry = session.new_entry(people.first, dependencies: deps)
20
+ header = entry.external_entry.keys
39
21
 
40
- csv << header
41
- people.each do |person|
42
- csv << session.new_entry(person, dependencies: deps).to_hash.values
43
- end
44
- end
45
- exit(0)
46
- end
47
- end
22
+ if options.dig(:nice_header) || options.dig(:export, :options, :nice_header)
23
+ name_maps = session.schema.fields_by_alt_id.transform_values do |fld|
24
+ fld.name
25
+ end.merge({
26
+ "policy_group_ids" => "User Group(s)",
27
+ "email" => "Email",
28
+ "name" => "Name",
29
+ "supervisor_id" => "Manager ID",
30
+ "filter_tags" => "Locations",
31
+ "default_tag" => "Default Location",
32
+ "id" => "ecoPortal ID"
33
+ })
34
+ header = header.map {|name| name_maps[name] ? name_maps[name] : name}
35
+ end
48
36
 
49
- end
37
+ csv << header
38
+ people.each do |person|
39
+ csv << session.new_entry(person, dependencies: deps).external_entry.values
50
40
  end
51
41
  end
42
+ exit(0)
52
43
  end
44
+
53
45
  end
@@ -1,68 +1,53 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class ToCsvDetailedCase < DefaultCase
6
-
7
- def process
8
- @cases.define("to-csv-detailed", type: :export) do |people, session, options, usecase|
9
- unless people && !people.empty?
10
- session.logger.warn("No source people to create the file... aborting!")
11
- next false
12
- end
13
-
14
- unless file = options[:file] || options.dig(:export, :file, :name)
15
- session.logger.error("Destination file not specified")
16
- next false
17
- end
18
-
19
- login_providers = session.login_providers
20
-
21
- abilities = session.new_preset([]).keys
1
+ class Eco::API::UseCases::DefaultCases::ToCsvDetailedCase < Eco::API::Common::Loaders::UseCase
2
+ name "to-csv-detailed"
3
+ type :export
4
+
5
+ def main(people, session, options, usecase)
6
+ unless people && !people.empty?
7
+ session.logger.warn("No source people to create the file... aborting!")
8
+ return false
9
+ end
22
10
 
23
- session.logger.info("going to create file: #{file}")
24
- CSV.open(file, "w") do |csv|
25
- deps = {"supervisor_id" => {people: people}}
26
- header = session.new_entry(people.first, dependencies: deps).to_hash.keys
27
- header += ["Subordinates"]
28
- header += ["Supervisor Name"]
29
- header += abilities
30
- header += ["Login Methods"] if login_providers.any_optional?
31
- header += ["Landing Page"]
11
+ unless file = options[:file] || options.dig(:export, :file, :name)
12
+ session.logger.error("Destination file not specified")
13
+ return false
14
+ end
32
15
 
33
- csv << header
34
- people.each do |person|
35
- data = session.new_entry(person, dependencies: deps).to_hash.values
16
+ login_providers = session.login_providers
17
+ abilities = session.presets_factory.keys
36
18
 
37
- data.push(person.subordinates)
38
- super_id = person.supervisor_id
39
- if supervisor = people.person(id: super_id, external_id: super_id)
40
- data.push(supervisor.name)
41
- else
42
- data.push("")
43
- end
19
+ session.logger.info("going to create file: #{file}")
20
+ CSV.open(file, "w") do |csv|
21
+ deps = {"supervisor_id" => {people: people}}
44
22
 
45
- person_abilities = (person.account && person.account.permissions_custom) || {}
46
- data += abilities.map {|key| person_abilities[key] || "no access"}
23
+ person_model = people.first
24
+ person_entry = session.new_entry(person_model, dependencies: deps)
25
+ header = person_entry.external_entry.keys
26
+ header += ["Subordinates"]
27
+ header += ["Supervisor Name"]
28
+ header += abilities
29
+ header += ["Landing Page"]
47
30
 
48
- if login_providers.any_optional?
49
- logins = (person.account && person.account.login_provider_ids) || []
50
- data.push(login_providers.to_name(logins).join("|"))
51
- end
31
+ csv << header
32
+ people.each do |person|
33
+ data = session.new_entry(person, dependencies: deps).external_entry.values
52
34
 
53
- if person.account && landing_id = person.account.landing_page_id
54
- data.push(landing_id)
55
- end
35
+ data.push(person.subordinates)
36
+ session.micro.with_supervisor(person.supervisor_id, people) do |supervisor|
37
+ data.push supervisor ? supervisor.name : ""
38
+ end
56
39
 
57
- csv << data
58
- end
59
- end
60
- exit(0)
61
- end
62
- end
40
+ person_abilities = (person.account && person.account.permissions_custom) || {}
41
+ data += abilities.map {|key| person_abilities[key] || "no access"}
63
42
 
43
+ if person.account && landing_id = person.account.landing_page_id
44
+ data.push(landing_id)
64
45
  end
46
+
47
+ csv << data
65
48
  end
66
49
  end
50
+ exit(0)
67
51
  end
52
+
68
53
  end
@@ -1,90 +1,270 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class TransferAccountCase < DefaultCase
6
-
7
- def process
8
- @cases.define("transfer-account", type: :sync) do |entries, people, session, options, usecase|
9
- remove_account = session.job_group("main").new("remove account", usecase: usecase, type: :update, sets: :account)
10
- add_account = session.job_group("post").new("add account", usecase: usecase, type: :update, sets: :account)
11
-
12
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
13
-
14
- done = []; pending = []
15
- pairs = entries.each_with_object([]) do |source|
16
- entry_hash = source.internal_entry
17
- unless entry_hash.key?("destination-id")
18
- session.logger.error("You haven't defined a column 'destination-id' to whom the account should be transferred")
19
- exit(1)
20
- end
21
- if peer_id = entry_hash["destination-id"]
22
- if peer = entries.entry(id: peer_id, external_id: peer_id)
23
- if done.include?(peer)
24
- session.logger.error("You paired '#{peer_id}' twice. A person can only receive account from 1 user")
25
- exit(1)
26
- end
27
- pending.delete(source)
28
- pending.delete(peer)
29
- done.push(source).push(pair)
30
- [source, peer]
31
- else
32
- pending.push(source)
33
- nil
34
- end
35
- else
36
- pending.push(source)
37
- end
38
- end.compact
39
-
40
- # Data input integrity check
41
- unless pending.empty?
42
- msg = "You haven't defined a pair for the following ids:"
43
- msg += pending.each_with_object("") do |entry, str|
44
- str << "\n#{entry.id || entry.external_id}"
45
- end
46
- session.logger.error(msg)
47
- exit(1)
48
- end
49
-
50
- pairs.each do |pair|
51
- src_entry, dst_entry = pair
52
- unless src_person = people.find(src_entry, strict: strict_search)
53
- session.logger.error("Entry(#{i}) - this person does not exist: #{src_entry.to_s(:identify)}")
54
- exit(1)
55
- end
56
-
57
- unless dst_person = people.find(dst_entry, strict: strict_search)
58
- session.logger.error("Entry(#{i}) - this person does not exist: #{dst_person.to_s(:identify)}")
59
- exit(1)
60
- end
61
-
62
- unless account_doc = src_person.account&.doc
63
- session.logger.error("You are trying to move account from a person that doesn't have: #{src_person.id | src_person.external_id}")
64
- exit(1)
65
- end
66
-
67
- if dst_person.email.to_s.strip.empty?
68
- session.logger.error("A person you are trying to add account doesn't have email: #{dst_person.id | dst_person.external_id}")
69
- exit(1)
70
- end
71
-
72
- src_person.account = nil
73
- remove_account.add(src_person)
74
- add_account.add(dst_person) do |person|
75
- # only if we got to remove the account of the original person
76
- if account_doc && src_person.as_update == {}
77
- person.account = account_doc
78
- end
79
- end
80
-
81
- end
82
-
83
- end
84
- end
1
+ class Eco::API::UseCases::DefaultCases::TransferAccountCase < Eco::API::Common::Loaders::UseCase
2
+ name "transfer-account"
3
+ type :sync
85
4
 
5
+ # Usecase to **actually transfer a user/account** from one person to another person in the organization.
6
+ # * **invocation command**: `-transfer-account-from`
7
+ #
8
+ # These are the steps and jobs it does:
9
+ # 1. **pair** person entries (note: the `destination-id` entry could not be present, it will add it in such a case).
10
+ # 2. **retrieve** from server persons that were not included in `people`.
11
+ # 3. **validation**
12
+ # - a person should only receive account from just one user
13
+ # - a person should only give account to just one user
14
+ # - every account giver should have account
15
+ # 4. **create jobs**
16
+ # - **move** giver's and receiver's **accounts** to dummy email addresses.
17
+ # * dummy email pattern: from name@domain.ltd --to--> demo+name.domain.ltd@ecoportal.co.nz
18
+ # - **free up** giver's and receiver's **accounts**.
19
+ # - **switch** email: set receiver's email to that of giver's dummy address.
20
+ # * to ensure account transfer, as we moved accounts to dummy emails, those dummy addresses should be used
21
+ # - **invite** receivers: adds account to the destination person in the dummy email.
22
+ # * actual user/account transfer to the person/receiver
23
+ # * **no notification** will be recived by the user, because of the dummy address at this stage
24
+ # - **restore** email: sets the receiver email from the dummy address to the final email.
25
+ # * the final email inbox will receive a **notification** of _email change_
26
+ # @note
27
+ # - the `csv` should contain a column `destination-id` containing the person `id` or `external_id` of the person that will be receiving the account.
28
+ # - when running this case, it is recommended to use the option `-skip-batch-policies`
29
+ # - it is highly recommended to either refresh the cache with `-get-people` or use the `-get-partial` option.
30
+ # @param entries [Eco::API::Common::People::Entries] the input entries with the data.
31
+ # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
32
+ # @param session [Eco::API::Session] the current session where the usecase kicks in.
33
+ # @param options [Hash] the options that modify the case behaviour or bring some dependencies.
34
+ # @option options [Hash<Symbol, Object>] :include things that should be included.
35
+ # * `:email` (Boolean) [false] if the `email` should be transferred as well (**command option**: `-include-email`)
36
+ # @option options [Hash<Symbol, Object>] :skip things that should be excluded.
37
+ # * `:api_policies` (Boolean) [false] if the `api policies` should be skipped (**command option**: `-skip-api-policies`)
38
+ # @return [Void]
39
+ def main(entries, people, session, options, usecase)
40
+ micro = session.micro
41
+ move = session.new_job("main", "move email accounts", :update, usecase, :account)
42
+ free = session.new_job("main", "free up accounts", :update, usecase, :account)
43
+ switch = session.new_job("main", "switch email", :update, usecase, :core)
44
+ invite = session.new_job("post", "invite receivers", :update, usecase, :account)
45
+ restore = session.new_job("post", "restore email", :update, usecase, :core)
46
+
47
+ with_each_person_pair(entries, people, session, options) do |src_person, dst_person|
48
+ # capture actual initial information
49
+ src_doc = src_person.account.doc
50
+ src_email = src_person.email
51
+ src_dummy = dummy_email(src_person)
52
+ dst_email = dst_person.email
53
+ dst_dummy = dummy_email(dst_person)
54
+ copy_src_email = options.dig(:include, :email) || !dst_person.account
55
+ dst_end_email = copy_src_email ? src_email : dst_email
56
+
57
+ # account email renamings are necessary to avoid uncertainty and ensure no email taken error
58
+ move.add(dst_person) {|dst| dst.email = dst_dummy}
59
+ move.add(src_person) {|src| src.email = src_dummy}
60
+ # free accounts up!
61
+ free.add([dst_person, src_person]) {|person| person.account = nil}
62
+ # to effectivelly transfer the user/account, email should be the same during invite
63
+ # otherwise the account doesn't actually get transferred but just copied
64
+ switch.add(dst_person) {|dst| dst.email = src_dummy}
65
+ # do the actual transfer of account
66
+ invite.add(dst_person) {|dst| dst.account = src_doc}
67
+ # recover the original email, if the receiver had account
68
+ restore.add(dst_person) {|dst| dst.email = dst_end_email}
69
+ end.tap do |units|
70
+ if options[:simulate]
71
+ units.each {|unit| puts unit.persons.map(&:external_id).join(" --> ")}
72
+ end
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ # if the person has account and an email different to demo+__@ecoportal.co.nz
79
+ # it transforms email to demo+__@ecoportal.co.nz
80
+ def dummy_email(person)
81
+ return nil unless email = person.email
82
+ return email if email.start_with?("demo") && email.end_with?("@ecoportal.co.nz")
83
+ return email unless person.account
84
+ "demo+#{email.split("@").join(".")}@ecoportal.co.nz"
85
+ end
86
+
87
+ def with_each_person_pair(entries, people, session, options)
88
+ units = paired_entries(entries, session) do |unpaired_entries|
89
+ report_missing_peer!(unpaired_entries, session.logger)
90
+ end.map do |entry_pair|
91
+ person_pair = to_persons(entry_pair, people, session, options)
92
+ new_unit(*entry_pair, *person_pair)
93
+ end
94
+ sort_units(units).tap do |units|
95
+ # check that there are no repeated sources or destinations, that sources have account
96
+ validate_pairs!(units, session.logger)
97
+ units.each do |unit|
98
+ yield(*unit.persons) if block_given?
99
+ end
100
+ end
101
+ end
102
+
103
+ # account givers that are receivers should give first
104
+ # exchangers are placed at the beginning
105
+ def sort_units(units)
106
+ base = units.dup
107
+ # cases where two persons exchange account
108
+ exchangers = base.select do |source|
109
+ base.any? {|dest| source.reversed_unit?(dest)}
110
+ end
111
+ exchangers | base.sort
112
+ end
113
+
114
+ # Entry helpers
115
+ def to_persons(paired_entry, people, session, options)
116
+ micro = session.micro
117
+ [].tap do |persons|
118
+ micro.with_each(paired_entry, people, options) do |entry, person|
119
+ next persons.push(person) unless person.new?
120
+ if person = session.batch.search([entry], silent: true).people.first
121
+ persons.push(person)
122
+ people << person
123
+ next
86
124
  end
125
+ session.logger.error("This person does not exist: #{entry.to_s(:identify)}")
126
+ exit(1)
127
+ end
128
+ end.tap do |persons|
129
+ yield(*persons) if block_given?
130
+ end
131
+ end
132
+
133
+ def paired_entries(entries, session)
134
+ expect_destination_id!(entries, session.logger)
135
+ missing_peer = []
136
+ entries.each_with_object([]) do |source, out|
137
+ if peer = entry_peer(source, entries)
138
+ out.push([source, peer])
139
+ else
140
+ missing_peer.push(source)
141
+ end
142
+ end.tap do |paired_entries|
143
+ yield(missing_peer) if block_given?
144
+ end
145
+ end
146
+
147
+ def entry_peer(entry, entries)
148
+ return nil unless peer_id = entry_peer_id(entry)
149
+ entries.entry(id: peer_id, external_id: peer_id) || decouple_peer(entry)
150
+ end
151
+
152
+ def decouple_peer(entry)
153
+ entry.new({
154
+ "id" => entry_peer_id(entry),
155
+ "external_id" => entry_peer_id(entry),
156
+ "email" => entry_peer_id(entry),
157
+ "idx" => entry.idx
158
+ })
159
+ end
160
+
161
+ def entry_peer_id(entry)
162
+ dest_id = entry.final_entry["destination-id"]
163
+ return dest_id unless dest_id.to_s.strip.empty?
164
+ end
165
+
166
+ def entry_peer_id?(entry)
167
+ entry.final_entry.key?("destination-id")
168
+ end
169
+
170
+ # Unit type helpers
171
+ def unit_type
172
+ @unit_type ||= Struct.new(:src_entry, :dst_entry, :src_person, :dst_person) do
173
+ def persons
174
+ [src_person, dst_person]
87
175
  end
176
+ def reversed_unit?(u2)
177
+ (src_person == u2.dst_person) && (dst_person == u2.src_person)
178
+ end
179
+ # givers go first
180
+ def <=>(other)
181
+ return -1 if src_person == other.dst_person
182
+ return 1 if dst_person == other.src_person
183
+ 0
184
+ end
185
+ end
186
+ end
187
+
188
+ def new_unit(e1, e2, p1, p2)
189
+ unit_type.new(e1, e2, p1, p2)
190
+ end
191
+
192
+ # Units helpers
193
+ def find_repeated(units, &block)
194
+ units.group_by(&block).select do |k,v|
195
+ v.count > 1
196
+ end.map {|k, v| v.first}
197
+ end
198
+
199
+ def uniq_array(ary, &block)
200
+ ary.each_with_object([]) do |e, uniq|
201
+ uniq.push(e) unless uniq.any? {|chosen| block.call(chosen, e)}
202
+ end
203
+ end
204
+
205
+ def validate_pairs!(units, logger)
206
+ missing_account = []
207
+ src_repeated = find_repeated(units) do |unit|
208
+ unit.src_person.tap do |src_person|
209
+ missing_account << (src_person.account ? nil : str_person_entry(unit.src_person, unit.src_entry))
210
+ end
211
+ end.each_with_object([]) do |unit, lines|
212
+ lines << str_person_entry(unit.src_person, unit.src_entry)
213
+ end
214
+ dst_repeated = find_repeated(units) do
215
+ |unit| unit.dst_person
216
+ end.each_with_object([]) do |unit, lines|
217
+ lines << str_person_entry(unit.dst_person, unit.dst_entry, unit.src_entry.idx)
218
+ end
219
+ missing_account.compact!
220
+ unless [missing_account, src_repeated, dst_repeated].all?(&:empty?)
221
+ report_missing_account(missing_account.join("\n"), logger) unless missing_account.empty?
222
+
223
+ msg = Proc.new do |spot|
224
+ "Transfers should be a 1.to.1 relation. The following #{spot} entries are repeated in the csv:"
225
+ end
226
+ report_repeated(src_repeated.join("\n"), logger, msg["SOURCE"]) unless src_repeated.empty?
227
+ report_repeated(dst_repeated.join("\n"), logger, msg["DESTINATION"]) unless dst_repeated.empty?
228
+ exit(1)
88
229
  end
89
230
  end
231
+
232
+ # Messaging & Validation
233
+ def report_missing_peer!(unpaired_entries, logger)
234
+ unless unpaired_entries.empty?
235
+ msg = "The following rows are missing 'destination-id':\n\n"
236
+ msg += unpaired_entries.map {|entry| str_missing_peer(entry)}.join("\n")
237
+ logger.error(msg)
238
+ exit(1)
239
+ end
240
+ end
241
+
242
+ def str_missing_peer(entry)
243
+ "Source #{entry.to_s(:identify)} is missing 'destination-id'."
244
+ end
245
+
246
+ def str_person_entry(person, entry, row = nil)
247
+ str_row = row ? "(actual row: #{row}) " : nil
248
+ "#{str_row}id: '#{person.id || person.external_id}' email:#{person.email} -> Entry #{entry.to_s(:identify)}"
249
+ end
250
+
251
+ def expect_destination_id!(entries, logger)
252
+ unless entries.length > 0
253
+ logger.error("Your csv is empty")
254
+ exit(1)
255
+ end
256
+ unless entry_peer_id?(entries.first)
257
+ logger.error("You haven't defined a column 'destination-id' to whom the account should be transferred")
258
+ exit(1)
259
+ end
260
+ end
261
+
262
+ def report_missing_account(str, logger)
263
+ logger.error("The following source people do not have account:\n#{str}")
264
+ end
265
+
266
+ def report_repeated(str, logger, msg = "The following entries are repeated")
267
+ logger.error("#{msg}\n#{str}")
268
+ end
269
+
90
270
  end