eco-helpers 1.5.1 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +82 -2
  3. data/LICENSE +21 -0
  4. data/eco-helpers.gemspec +1 -1
  5. data/lib/eco/api.rb +2 -0
  6. data/lib/eco/api/common.rb +4 -0
  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 +3 -12
  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 +15 -25
  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 +24 -35
  26. data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +15 -25
  27. data/lib/eco/api/common/people/entries.rb +54 -24
  28. data/lib/eco/api/common/people/entry_factory.rb +10 -8
  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 +3 -2
  32. data/lib/eco/api/common/people/person_parser.rb +51 -18
  33. data/lib/eco/api/common/session/logger.rb +4 -0
  34. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +2 -0
  35. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +1 -1
  36. data/lib/eco/api/common/version_patches/exception.rb +22 -0
  37. data/lib/eco/api/custom.rb +13 -0
  38. data/lib/eco/api/custom/error_handler.rb +20 -0
  39. data/lib/eco/api/custom/namespace.rb +7 -0
  40. data/lib/eco/api/custom/parser.rb +50 -0
  41. data/lib/eco/api/custom/policy.rb +28 -0
  42. data/lib/eco/api/custom/use_case.rb +16 -0
  43. data/lib/eco/api/error.rb +1 -0
  44. data/lib/eco/api/error/handlers.rb +10 -3
  45. data/lib/eco/api/microcases.rb +17 -13
  46. data/lib/eco/api/microcases/account_excluded.rb +24 -0
  47. data/lib/eco/api/microcases/append_usergroups.rb +19 -0
  48. data/lib/eco/api/microcases/core_excluded.rb +4 -4
  49. data/lib/eco/api/microcases/{set_default_group.rb → fix_default_group.rb} +10 -9
  50. data/lib/eco/api/microcases/fix_filter_tags.rb +26 -6
  51. data/lib/eco/api/microcases/people_cache.rb +17 -0
  52. data/lib/eco/api/microcases/people_load.rb +59 -0
  53. data/lib/eco/api/microcases/people_refresh.rb +31 -0
  54. data/lib/eco/api/microcases/people_search.rb +65 -0
  55. data/lib/eco/api/microcases/refresh_abilities.rb +19 -0
  56. data/lib/eco/api/microcases/refresh_default_tag.rb +27 -0
  57. data/lib/eco/api/microcases/s3upload_targets.rb +39 -0
  58. data/lib/eco/api/microcases/set_account.rb +7 -19
  59. data/lib/eco/api/microcases/set_core.rb +5 -5
  60. data/lib/eco/api/microcases/set_core_with_supervisor.rb +23 -0
  61. data/lib/eco/api/microcases/set_supervisor.rb +17 -13
  62. data/lib/eco/api/microcases/strict_search.rb +12 -7
  63. data/lib/eco/api/microcases/with_each.rb +27 -0
  64. data/lib/eco/api/microcases/with_each_leaver.rb +24 -0
  65. data/lib/eco/api/microcases/with_each_present.rb +30 -0
  66. data/lib/eco/api/microcases/with_each_starter.rb +30 -0
  67. data/lib/eco/api/microcases/with_each_subordinate.rb +34 -0
  68. data/lib/eco/api/microcases/with_supervisor.rb +36 -0
  69. data/lib/eco/api/organization/people.rb +72 -35
  70. data/lib/eco/api/organization/presets_factory.rb +13 -4
  71. data/lib/eco/api/policies.rb +11 -7
  72. data/lib/eco/api/session.rb +54 -24
  73. data/lib/eco/api/session/batch.rb +1 -1
  74. data/lib/eco/api/session/batch/base_policy.rb +7 -6
  75. data/lib/eco/api/session/batch/errors.rb +28 -4
  76. data/lib/eco/api/session/batch/feedback.rb +7 -1
  77. data/lib/eco/api/session/batch/job.rb +40 -23
  78. data/lib/eco/api/session/batch/jobs.rb +9 -4
  79. data/lib/eco/api/session/batch/jobs_groups.rb +1 -1
  80. data/lib/eco/api/session/batch/request_stats.rb +91 -58
  81. data/lib/eco/api/session/batch/status.rb +35 -31
  82. data/lib/eco/api/session/config.rb +104 -42
  83. data/lib/eco/api/session/config/api.rb +17 -6
  84. data/lib/eco/api/session/config/logger.rb +2 -2
  85. data/lib/eco/api/session/config/post_launch.rb +1 -1
  86. data/lib/eco/api/session/config/workflow.rb +8 -7
  87. data/lib/eco/api/usecases.rb +47 -33
  88. data/lib/eco/api/usecases/backup/append_usergroups_case.rb +36 -0
  89. data/lib/eco/api/usecases/backup/create_case.rb +104 -0
  90. data/lib/eco/api/usecases/backup/create_details_case.rb +31 -0
  91. data/lib/eco/api/usecases/backup/create_details_with_supervisor_case.rb +48 -0
  92. data/lib/eco/api/usecases/backup/hris_case.rb +124 -0
  93. data/lib/eco/api/usecases/backup/set_default_tag_case.rb +49 -0
  94. data/lib/eco/api/usecases/backup/set_supervisor_case.rb +41 -0
  95. data/lib/eco/api/usecases/backup/transfer_account_case.rb +90 -0
  96. data/lib/eco/api/usecases/backup/update_case.rb +112 -0
  97. data/lib/eco/api/usecases/backup/update_details_case.rb +64 -0
  98. data/lib/eco/api/usecases/backup/upsert_case.rb +114 -0
  99. data/lib/eco/api/usecases/base_case.rb +2 -0
  100. data/lib/eco/api/usecases/base_io.rb +3 -3
  101. data/lib/eco/api/usecases/default_cases.rb +23 -53
  102. data/lib/eco/api/usecases/default_cases/append_usergroups_case.rb +10 -31
  103. data/lib/eco/api/usecases/default_cases/change_email_case.rb +23 -47
  104. data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +56 -43
  105. data/lib/eco/api/usecases/default_cases/create_case.rb +15 -101
  106. data/lib/eco/api/usecases/default_cases/create_details_case.rb +11 -26
  107. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +12 -43
  108. data/lib/eco/api/usecases/default_cases/delete_sync_case.rb +11 -0
  109. data/lib/eco/api/usecases/default_cases/delete_trans_case.rb +14 -0
  110. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +10 -21
  111. data/lib/eco/api/usecases/default_cases/hris_case.rb +23 -120
  112. data/lib/eco/api/usecases/default_cases/new_email_case.rb +10 -23
  113. data/lib/eco/api/usecases/default_cases/new_id_case.rb +11 -25
  114. data/lib/eco/api/usecases/default_cases/new_id_case0.rb +14 -0
  115. data/lib/eco/api/usecases/default_cases/org_data_convert_case.rb +83 -0
  116. data/lib/eco/api/usecases/default_cases/refresh_abilities_case.rb +30 -0
  117. data/lib/eco/api/usecases/default_cases/refresh_case.rb +7 -20
  118. data/lib/eco/api/usecases/default_cases/reinvite_sync_case.rb +11 -0
  119. data/lib/eco/api/usecases/default_cases/reinvite_trans_case.rb +17 -0
  120. data/lib/eco/api/usecases/default_cases/remove_account_sync_case.rb +11 -0
  121. data/lib/eco/api/usecases/default_cases/remove_account_trans_case.rb +17 -0
  122. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +9 -19
  123. data/lib/eco/api/usecases/default_cases/restore_db_case.rb +92 -0
  124. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +32 -40
  125. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +15 -33
  126. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +66 -57
  127. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +36 -44
  128. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +40 -55
  129. data/lib/eco/api/usecases/default_cases/transfer_account_case.rb +264 -84
  130. data/lib/eco/api/usecases/default_cases/update_case.rb +15 -109
  131. data/lib/eco/api/usecases/default_cases/update_details_case.rb +14 -61
  132. data/lib/eco/api/usecases/default_cases/upsert_case.rb +16 -111
  133. data/lib/eco/api/usecases/use_case_io.rb +9 -9
  134. data/lib/eco/cli/config.rb +10 -2
  135. data/lib/eco/cli/config/default.rb +2 -1
  136. data/lib/eco/cli/config/default/input_filters.rb +58 -0
  137. data/lib/eco/cli/config/default/options.rb +60 -25
  138. data/lib/eco/cli/config/default/people.rb +4 -4
  139. data/lib/eco/cli/config/default/people_filters.rb +108 -0
  140. data/lib/eco/cli/config/default/usecases.rb +69 -32
  141. data/lib/eco/cli/config/default/workflow.rb +37 -27
  142. data/lib/eco/cli/config/filters.rb +50 -0
  143. data/lib/eco/cli/config/filters/input_filters.rb +29 -0
  144. data/lib/eco/cli/config/filters/people_filters.rb +29 -0
  145. data/lib/eco/cli/config/help.rb +49 -0
  146. data/lib/eco/cli/config/options_set.rb +17 -1
  147. data/lib/eco/cli/config/use_cases.rb +79 -53
  148. data/lib/eco/cli/scripting.rb +10 -2
  149. data/lib/eco/cli/scripting/args_helpers.rb +25 -15
  150. data/lib/eco/cli/scripting/argument.rb +1 -0
  151. data/lib/eco/cli/scripting/arguments.rb +1 -1
  152. data/lib/eco/csv/table.rb +1 -1
  153. data/lib/eco/data/crypto/encryption.rb +3 -0
  154. data/lib/eco/language/match.rb +19 -9
  155. data/lib/eco/language/match_modifier.rb +13 -5
  156. data/lib/eco/language/models/collection.rb +77 -56
  157. data/lib/eco/language/models/parser_serializer.rb +39 -15
  158. data/lib/eco/version.rb +1 -1
  159. metadata +63 -18
  160. data/lib/eco/api/microcases/set_default_tag.rb +0 -23
  161. data/lib/eco/api/session/task.rb +0 -175
  162. data/lib/eco/api/usecases/default_case.rb +0 -19
  163. data/lib/eco/api/usecases/default_cases/delete_case.rb +0 -32
  164. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +0 -99
  165. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +0 -26
  166. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +0 -41
  167. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +0 -38
  168. data/lib/eco/api/usecases/microed_cases/hris_case.rb +0 -53
  169. data/lib/eco/api/usecases/microed_cases/update_case.rb +0 -33
  170. data/lib/eco/api/usecases/microed_cases/update_details_case.rb +0 -30
  171. data/lib/eco/api/usecases/microed_cases/upsert_case.rb +0 -36
  172. data/lib/eco/cli/config/default/filters.rb +0 -70
  173. data/lib/eco/cli/config/people_filters.rb +0 -38
@@ -0,0 +1,114 @@
1
+ module Eco
2
+ module API
3
+ class UseCases
4
+ class DefaultCases
5
+ class UpsertCase < DefaultCase
6
+
7
+ def process
8
+ @cases.define("upsert", type: :sync) do |entries, people, session, options, usecase|
9
+ creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account])
10
+ update = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account])
11
+ supers = session.job_group("post").new("supers", usecase: usecase, type: :update, sets: :core)
12
+
13
+ strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
14
+ pgs = session.policy_groups
15
+
16
+ if session.config.people.default_usergroup?
17
+ def_id = pgs.to_id(session.config.people.default_usergroup)
18
+ end
19
+
20
+ entries.each_with_index do |entry, i|
21
+ person = people.find(entry, strict: strict_search)
22
+ person = session.new_person if create = !person
23
+
24
+ unless options.dig(:exclude, :core) && !create
25
+ ini_tags = person.filter_tags || []
26
+
27
+ core_excluded = ["supervisor_id"]
28
+
29
+ unless create
30
+ core_attrs = ["name", "external_id", "email", "filter_tags"]
31
+ core_excluded += core_attrs.map.select {|attr| options.dig(:exclude, attr.to_sym)}
32
+ end
33
+
34
+ entry.set_core(person, exclude: core_excluded)
35
+ if session.tagtree && !options.dig(:exclude, :filter_tags)
36
+ person.filter_tags = session.tagtree.user_tags(
37
+ initial: ini_tags,
38
+ final: person.filter_tags,
39
+ preserve_custom: true,
40
+ add_custom: true
41
+ )
42
+ end
43
+ end
44
+
45
+ entry.set_details(person) unless options.dig(:exclude, :details)
46
+
47
+ unless options.dig(:exclude, :account)
48
+ add_account = !person.account
49
+ ini_pg_ids = person.account&.policy_group_ids || []
50
+
51
+ account_excluded = []
52
+ account_excluded.push("policy_group_ids") if options.dig(:exclude, :policy_groups) && !create
53
+
54
+ entry.set_account(person, exclude: account_excluded)
55
+
56
+ person.account.send_invites = options[:send_invites] if options.key?(:send_invites)
57
+
58
+ unless options.dig(:exclude, :policy_groups) && !create
59
+ end_pg_ids = person.account.policy_group_ids || []
60
+
61
+ if add_account && def_id && !entry.policy_group_ids?
62
+ # on account creation, if missing policy_group_ids column in the input
63
+ # use default_usergroup, if it's defined
64
+ end_pg_ids = [def_id]
65
+ end
66
+
67
+ # avoid false updates by preserving the original order
68
+ person.account.policy_group_ids = pgs.user_pg_ids(
69
+ initial: ini_pg_ids,
70
+ final: end_pg_ids
71
+ )
72
+ end
73
+
74
+ person.account.permissions_custom = session.new_preset(person) unless !create && options.dig(:exclude, :abilities)
75
+
76
+ unless options.dig(:exclude, :filter_tags) || entry.default_tag?
77
+ if session.tagtree
78
+ person.account.default_tag = session.tagtree.default_tag(*person.filter_tags)
79
+ else
80
+ tags = person.filter_tags || []
81
+ person.account.default_tag = tags.first unless tags.length > 1
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ creation.add(person) if create
88
+ update.add(person) unless create
89
+
90
+ # set supervisor
91
+ unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
92
+ if !(sup_id = entry.supervisor_id)
93
+ person.supervisor_id = nil
94
+ else
95
+ if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
96
+ person.supervisor_id = supervisor.id
97
+ else
98
+ # delay setting supervisor if does not exit
99
+ supers.add(person) do |person|
100
+ person.supervisor_id = sup_id
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ end
107
+ end
108
+ end
109
+
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -12,6 +12,8 @@ module Eco
12
12
  end
13
13
  end
14
14
 
15
+ extend Eco::API::Common::ClassHelpers
16
+
15
17
  @types = [:import, :filter, :transform, :sync, :error_handler, :export, :other]
16
18
 
17
19
  class << self
@@ -67,10 +67,10 @@ module Eco
67
67
  end
68
68
 
69
69
  # Helper to build a `Hash` of symbol keys or `Array` with params to do callbacks.
70
- def params(keyed: false)
70
+ def params(keyed: false, all: false)
71
71
  kargs = {}
72
- kargs.merge!(input: input) if input_required?
73
- kargs.merge!(people: people) if people_required?
72
+ kargs.merge!(input: input) if input_required? || all
73
+ kargs.merge!(people: people) if people_required? || all
74
74
  kargs.merge!(session: session, options: options)
75
75
  keyed ? kargs : kargs.values
76
76
  end
@@ -2,70 +2,40 @@ module Eco
2
2
  module API
3
3
  class UseCases
4
4
  class DefaultCases < Eco::API::UseCases
5
-
6
- def initialize(*args)
7
- super(*args)
8
- define_defaults
9
- end
10
-
11
- private
12
-
13
- def define_defaults
14
- ResetLandingPageCase.new(self).process
15
- ToCsvCase.new(self).process
16
- ToCsvDetailedCase.new(self).process
17
- CodesToTagsCase.new(self).process
18
- EmailAsIdCase.new(self).process
19
- NewIdCase.new(self).process
20
- NewEmailCase.new(self).process
21
- SwitchSupervisorCase.new(self).process
22
- SetSupervisorCase.new(self).process
23
- UpdateDetailsCase.new(self).process
24
- CreateDetailsCase.new(self).process
25
- CreateDetailsWithSupervisorCase.new(self).process
26
- ReinviteCase.new(self).process
27
- DeleteCase.new(self).process
28
- RemoveAccountCase.new(self).process
29
- TransferAccountCase.new(self).process
30
- RecoverDBCase.new(self).process
31
- RefreshPresets.new(self).process
32
- Refresh.new(self).process
33
- ChangeEMailCase.new(self).process
34
- SetDefaultTagCase.new(self).process
35
- CreateCase.new(self).process
36
- AppendUsergroupsCase.new(self).process
37
- UpdateCase.new(self).process
38
- UpsertCase.new(self).process
39
- HrisCase.new(self).process
40
- end
5
+ autoloads_children_of "Eco::API::Common::Loaders::UseCase"
6
+ autoload_namespace "Eco::API::UseCases::DefaultCases"
41
7
  end
42
8
  end
43
9
  end
44
10
  end
45
11
 
46
- require_relative 'default_cases/reset_landing_page_case'
47
- require_relative 'default_cases/to_csv_case'
48
- require_relative 'default_cases/to_csv_detailed_case'
12
+ require_relative 'default_cases/append_usergroups_case'
13
+ require_relative 'default_cases/change_email_case'
49
14
  require_relative 'default_cases/codes_to_tags_case'
15
+ require_relative 'default_cases/create_case'
16
+ require_relative 'default_cases/create_details_case'
17
+ require_relative 'default_cases/create_details_with_supervisor_case'
18
+ require_relative 'default_cases/delete_trans_case'
19
+ require_relative 'default_cases/delete_sync_case'
50
20
  require_relative 'default_cases/email_as_id_case'
21
+ require_relative 'default_cases/hris_case'
51
22
  require_relative 'default_cases/new_id_case'
52
23
  require_relative 'default_cases/new_email_case'
24
+ require_relative 'default_cases/org_data_convert_case'
25
+ require_relative 'default_cases/refresh_abilities_case'
26
+ require_relative 'default_cases/refresh_case'
27
+ require_relative 'default_cases/reinvite_trans_case'
28
+ require_relative 'default_cases/reinvite_sync_case'
29
+ require_relative 'default_cases/remove_account_trans_case'
30
+ require_relative 'default_cases/remove_account_sync_case'
31
+ require_relative 'default_cases/reset_landing_page_case'
32
+ require_relative 'default_cases/restore_db_case'
33
+ require_relative 'default_cases/set_default_tag_case'
53
34
  require_relative 'default_cases/set_supervisor_case'
54
35
  require_relative 'default_cases/switch_supervisor_case'
55
- require_relative 'default_cases/update_details_case'
56
- require_relative 'default_cases/create_details_case'
57
- require_relative 'default_cases/create_details_with_supervisor_case'
58
- require_relative 'default_cases/reinvite_case'
59
- require_relative 'default_cases/delete_case'
60
- require_relative 'default_cases/remove_account_case'
36
+ require_relative 'default_cases/to_csv_case'
37
+ require_relative 'default_cases/to_csv_detailed_case'
61
38
  require_relative 'default_cases/transfer_account_case'
62
- require_relative 'default_cases/refresh_case'
63
- require_relative 'default_cases/refresh_presets_case'
64
- require_relative 'default_cases/recover_db_case'
65
- require_relative 'default_cases/change_email_case'
66
- require_relative 'default_cases/set_default_tag_case'
67
- require_relative 'default_cases/create_case'
68
- require_relative 'default_cases/append_usergroups_case'
69
39
  require_relative 'default_cases/update_case'
40
+ require_relative 'default_cases/update_details_case'
70
41
  require_relative 'default_cases/upsert_case'
71
- require_relative 'default_cases/hris_case'
@@ -1,36 +1,15 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class AppendUsergroupsCase < DefaultCase
1
+ class Eco::API::UseCases::DefaultCases::AppendUsergroupsCase < Eco::API::Common::Loaders::UseCase
2
+ name "append-usergroups"
3
+ type :sync
6
4
 
7
- def process
8
- @cases.define("append-usergroups", type: :sync) do |entries, people, session, options, usecase|
9
- update = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account])
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
- pgs = session.policy_groups
13
-
14
- entries.each.with_index do |entry, i|
15
- if person = people.find(entry, strict: strict_search)
16
- if person.account
17
- ini_pg_ids = person.account&.policy_group_ids || []
18
- add = entry.internal_entry["policy_group_ids"].to_s.split("|").compact
19
- person.account.policy_group_ids = (ini_pg_ids | add).uniq
20
-
21
- person.account.permissions_custom = session.new_preset(person) unless options.dig(:exclude, :abilities)
22
- update.add(person)
23
- end
24
- else
25
- session.logger.error("Entry(#{i}) - this person does not exist: #{entry.to_s(:identify)}")
26
- end
27
- end
28
- end
29
-
30
- end
31
-
32
- end
33
- end
9
+ micro.with_each_present(entries, people, options, log_starter: true) do |entry, person|
10
+ update.add(person)
11
+ micro.append_usergroups(entry, person, options)
34
12
  end
35
13
  end
14
+
36
15
  end
@@ -1,52 +1,28 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class ChangeEMailCase < DefaultCase
6
-
7
- def process
8
- @cases.define("change-email", type: :sync) do |entries, people, session, options, usecase|
9
- remove = session.job_group("main").new("remove account", usecase: usecase, type: :update, sets: :account)
10
- change = session.job_group("main").new("change email", usecase: usecase, type: :update, sets: :core)
11
- add_account = session.job_group("post").new("add account", usecase: usecase, type: :update, sets: :account)
12
-
13
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
14
-
15
- entries.each.with_index do |entry, i|
16
- person = people.find(entry, strict: strict_search)
17
-
18
- if !person
19
- session.logger.error("Entry(#{i}) - this person does not exist: #{entry.to_s(:identify)}")
20
- else
21
- unless entry.email.to_s.strip.downcase == person.email.to_s.strip.downcase
22
-
23
- super_id = person.supervisor_id
24
- s_account_doc = person.account&.doc
25
- account_doc = s_account_doc ? JSON.parse(s_account_doc.to_json) : nil
26
-
27
- person.account = nil
28
- remove.add(person)
29
-
30
- new_email = entry.email
31
- change.add(person) do |person|
32
- person.email = new_email
33
- end
34
-
35
- add_account.add(person) do |person|
36
- if account_doc
37
- person.account = account_doc
38
- end
39
- person.supervisor_id = super_id
40
- end
41
- end
42
-
43
- end
44
- end
45
- end
46
- end
47
-
1
+ class Eco::API::UseCases::DefaultCases::ChangeEMailCase < Eco::API::Common::Loaders::UseCase
2
+ name "change-email"
3
+ type :sync
4
+
5
+ # Target people with non-up-to-date emails will be updated to the new email
6
+ def main(entries, people, session, options, usecase)
7
+ micro = session.micro
8
+ remove = session.new_job("main", "remove account", :update, usecase, :account)
9
+ change = session.new_job("main", "change email", :update, usecase, :core)
10
+ invite = session.new_job("post", "add account", :update, usecase, [:core, :account])
11
+
12
+ micro.with_each_present(entries, people, options, log_starter: true) do |entry, person|
13
+ if entry.email != person.email
14
+ super_id = person.supervisor_id
15
+ s_account_doc = person.account&.doc
16
+ account_doc = s_account_doc ? JSON.parse(s_account_doc.to_json) : nil
17
+
18
+ remove.add(person) {|person| person.account = nil }
19
+ change.add(person) {|person| person.email = entry.email}
20
+ invite.add(person) do |person|
21
+ person.account = account_doc if account_doc
22
+ person.supervisor_id = super_id
48
23
  end
49
24
  end
50
25
  end
51
26
  end
27
+
52
28
  end
@@ -1,47 +1,60 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class CodesToTagsCase < DefaultCase
6
-
7
- def process
8
- @cases.define("codes-to-tags-from", type: :other) do |session, options, usecase|
9
- raise "You have not defined a tagtree for the organisation" unless session.tagtree
10
-
11
- unless mapper = session.config.location_codes && session.config.locations_mapper
12
- raise "No filter_tags parser, nor location mappings are present"
13
- end
14
-
15
- to_tag = Proc.new do |code|
16
- if mapper.external?(code)
17
- mapper.to_internal(code)
18
- elsif session.tagtree.tag?(code)
19
- code.upcase
20
- else
21
- session.logger.warn("Unknown Location Code: '#{code}'")
22
- nil
23
- end
24
- end
25
-
26
- codes_column = options.dig(:other, :file, :codes_column)
27
- input_file = options.dig(:other, :file, :name)
28
- output_file = "mapped_#{File.basename(input_file).gsub(/\s/, "")}"
29
-
30
- csv_table = Eco::CSV.read(input_file)
31
-
32
- csv_table.add_column("ecoPortalTag").tap do |csv_table|
33
- csv_table.each do |row|
34
- row["ecoPortalTag"] = to_tag.call(row[codes_column])
35
- end
36
-
37
- File.open(output_file, "w") {|fd| fd << csv_table.to_csv}
38
- end
39
- exit
40
- end
41
- end
42
-
43
- end
1
+ class Eco::API::UseCases::DefaultCases::CodesToTagsCase < Eco::API::Common::Loaders::UseCase
2
+ name "codes-to-tags-from"
3
+ type :other
4
+
5
+ def codes_to_tags
6
+ raise "You have not defined a tagtree for the organisation" unless session.tagtree
7
+ csv_table = Eco::CSV.read(input_file)
8
+
9
+ csv_table.add_column("ecoPortalTag").tap do |csv_table|
10
+ csv_table.each do |row|
11
+ row["ecoPortalTag"] = to_tag(row[codes_column])
44
12
  end
13
+
14
+ File.open(output_file, "w") {|fd| fd << csv_table.to_csv}
45
15
  end
46
16
  end
17
+
18
+ def main(session, options, usecase)
19
+ @session = session
20
+ @options = options
21
+ codes_to_tags
22
+ exit
23
+ end
24
+
25
+ private
26
+
27
+ def to_tag(code)
28
+ if mapper.external?(code)
29
+ mapper.to_internal(code)
30
+ elsif @session.tagtree.tag?(code)
31
+ code.upcase
32
+ else
33
+ @session.logger.warn("Unknown Location Code: '#{code}'")
34
+ nil
35
+ end
36
+ end
37
+
38
+ def mapper
39
+ unless mapper ||= @session.config.location_codes && @session.config.locations_mapper
40
+ raise "No filter_tags parser, nor location mappings are present"
41
+ end
42
+ end
43
+
44
+ def codes_column
45
+ @codes_column ||= @options.dig(:other, :file, :codes_column)
46
+ end
47
+
48
+ def input_file
49
+ @input_file ||= @options.dig(:other, :file, :name)
50
+ end
51
+
52
+ def output_file
53
+ "mapped_#{clean_file_name(input_file)}"
54
+ end
55
+
56
+ def clean_file_name(filename)
57
+ File.basename(filename).gsub(/\s/, "")
58
+ end
59
+
47
60
  end