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
@@ -0,0 +1,16 @@
1
+ # Helper class to create a custom `UseCase`
2
+ # @example Example of usage:
3
+ # class Custom::UseCase::Sync < Eco::API::Custom::UseCase
4
+ # name "custom-case"
5
+ # type :sync
6
+ #
7
+ # def main(entries, people, session, options, usecase)
8
+ # update = session.new_job("main", "update", :update, usecase)
9
+ # session.micro.with_each(entries, people, options) do |entry, person|
10
+ # # update some data in person using data from entry
11
+ # end
12
+ # end
13
+ #
14
+ # end
15
+ class Eco::API::Custom::UseCase < Eco::API::Common::Loaders::UseCase
16
+ end
@@ -13,6 +13,7 @@ module Eco
13
13
  @match = /.*/
14
14
  #RxValidId = /[a-f0-9]{24}/
15
15
 
16
+
16
17
  class Unclassified < Eco::API::Error
17
18
  @str_error = "Unclassified error message"
18
19
  @match = /.*/
@@ -2,12 +2,14 @@ module Eco
2
2
  module API
3
3
  class Error
4
4
  class Handlers < Eco::API::UseCases
5
+ autoloads_children_of "Eco::API::Common::Loaders::ErrorHandler"
6
+ autoload_namespace_ignore "Eco::API"
5
7
 
6
- def add(handler)
7
- raise "Expected Eco::API::Error::Handler object. Given: #{policy}" unless handler.is_a?(Eco::API::Error::Handler)
8
- super(handler)
8
+ def define(*args)
9
+ raise "Missusage. You should use 'on' method instead"
9
10
  end
10
11
 
12
+ # @param klass_err [Eco::API::Error] the specific error class that triggers this handler.
11
13
  def on(klass_err, type: :error_handler, retry: true, &block)
12
14
  Eco::API::Error.validate_err_class(klass_err)
13
15
  raise "Expected block but not given" unless block
@@ -16,6 +18,11 @@ module Eco
16
18
  end
17
19
  end
18
20
 
21
+ def add(handler)
22
+ raise "Expected Eco::API::Error::Handler object. Given: #{policy}" unless handler.is_a?(Eco::API::Error::Handler)
23
+ super(handler)
24
+ end
25
+
19
26
  end
20
27
  end
21
28
  end
@@ -0,0 +1,35 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases < Eco::API::Common::Session::BaseSession
4
+
5
+ def micro
6
+ self
7
+ end
8
+
9
+ end
10
+ end
11
+ end
12
+
13
+ require_relative 'microcases/account_excluded'
14
+ require_relative 'microcases/append_usergroups'
15
+ require_relative 'microcases/core_excluded'
16
+ require_relative 'microcases/fix_default_group'
17
+ require_relative 'microcases/fix_filter_tags'
18
+ require_relative 'microcases/people_cache'
19
+ require_relative 'microcases/people_load'
20
+ require_relative 'microcases/people_refresh'
21
+ require_relative 'microcases/people_search'
22
+ require_relative 'microcases/refresh_abilities'
23
+ require_relative 'microcases/set_account'
24
+ require_relative 'microcases/set_core_with_supervisor'
25
+ require_relative 'microcases/set_core'
26
+ require_relative 'microcases/refresh_default_tag'
27
+ require_relative 'microcases/set_supervisor'
28
+ require_relative 'microcases/strict_search'
29
+ require_relative 'microcases/s3upload_targets'
30
+ require_relative 'microcases/with_each'
31
+ require_relative 'microcases/with_each_leaver'
32
+ require_relative 'microcases/with_each_present'
33
+ require_relative 'microcases/with_each_starter'
34
+ require_relative 'microcases/with_each_subordinate'
35
+ require_relative 'microcases/with_supervisor'
@@ -0,0 +1,24 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
5
+ # @param options [Hash] the options.
6
+ # @return [Array<String>] the account parameters that should not be included.
7
+ def account_excluded(person, options)
8
+ [].tap do |account_excluded|
9
+ unless person.new?
10
+ if options.dig(:exclude, :policy_groups)
11
+ account_excluded.push("policy_group_ids")
12
+ end
13
+ if options.dig(:exclude, :default_tag)
14
+ account_excluded.push("default_tag")
15
+ end
16
+ if options.dig(:exclude, :login_providers)
17
+ account_excluded.push("login_provider_ids")
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # It preserves the usergroups of `person` and appends those defined in `policy_group_ids` of the `entry`
5
+ # @param entry [PersonEntry] the input entry with the data we should set on person.
6
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
7
+ # @param options [Hash] the options.
8
+ def append_usergroups(entry, person, options)
9
+ unless options.dig(:exclude, :account)
10
+ if person.account
11
+ person.account.policy_group_ids |= entry.policy_group_ids
12
+ micro.refresh_abilities(person, options)
13
+ end
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # @note by default `supervisor_id` is always excluded.
5
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
6
+ # @param options [Hash] the options.
7
+ # @return [Array<String>] the core parameters that should not be included.
8
+ def core_excluded(person, options)
9
+ ["supervisor_id"].tap do |core_excluded|
10
+ unless person.new?
11
+ exclusions = ["name", "external_id", "email", "filter_tags"].select do |attr|
12
+ options.dig(:exclude, attr.to_sym)
13
+ end
14
+ core_excluded.concat(exclusions)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # If defined, it sets the default usergroup, only when the `policy_group_ids` was not part of the input data.
5
+ # @param entry [PersonEntry] the input entry with the data we should set on person.
6
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
7
+ # @param options [Hash] the options.
8
+ def fix_default_group(entry, person, options)
9
+ unless options.dig(:exclude, :account)
10
+ unless options.dig(:exclude, :policy_groups) && !person.new?
11
+ end_pg_ids = person.account.policy_group_ids
12
+
13
+ if person.account_added? && __def_usergroup_id && !entry.policy_group_ids?
14
+ # on account creation, if missing policy_group_ids column in the input
15
+ # use default_usergroup, if it's defined
16
+ end_pg_ids = [__def_usergroup_id]
17
+ end
18
+
19
+ person.account.policy_group_ids = end_pg_ids
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def __def_usergroup_id
27
+ @def_usergroup_id ||= if session.config.people.default_usergroup?
28
+ session.policy_groups.to_id(session.config.people.default_usergroup)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,42 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Helper that makes sure the **custom `tags`** are preserved.
5
+ # @note
6
+ # - this feature is essential to preserve custom `tags` in users that have register tags.
7
+ # - for this to work out, it requires a `tagtree` to be defined.
8
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes already done.
9
+ # @param options [Hash] the options.
10
+ # @option options [Hash<Symbol, Object>] :filter_tags options around `filter_tags`.
11
+ # * `:preserve_custom` (Boolean) [true] indicates if original tags that are not in the tree should be added/preserved.
12
+ # * `:add_custom` (Boolean) [true] indicates if target tags that are not in the tree should be really added.
13
+ def fix_filter_tags(person, options)
14
+ if session.tagtree
15
+ unless !person.new? && (options.dig(:exclude, :core) || options.dig(:exclude, :filter_tags))
16
+ person.filter_tags = session.tagtree.user_tags(
17
+ initial: person.original_doc["filter_tags"] || [],
18
+ final: person.filter_tags,
19
+ preserve_custom: _fix_filter_tags_preserve_custom?(options),
20
+ add_custom: _fix_filter_tags_add_custom?(options)
21
+ )
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ # default `true`
29
+ def _fix_filter_tags_preserve_custom?(options)
30
+ key_defined = options.key?(:filter_tags) && options[:filter_tags].key?(:preserve_custom)
31
+ !key_defined || options.dig(:filter_tags, :preserve_custom)
32
+ end
33
+
34
+ # default `true`
35
+ def _fix_filter_tags_add_custom?(options)
36
+ key_defined = options.key?(:filter_tags) && options[:filter_tags].key?(:add_custom)
37
+ !key_defined || options.dig(:filter_tags, :add_custom)
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Helper to locally cache the people manager.
5
+ # @param filename [String] the name of the file where the data should be cached.
6
+ # @return [Eco::API::Organization::People] the `People` object with the data.
7
+ def people_cache(filename = enviro.config.people.cache)
8
+ logger.info("Going to get all the people via API")
9
+ people = session.batch.get_people
10
+ file = file_manager.save_json(people, filename, :timestamp)
11
+ logger.info("#{people.length} people loaded and saved locally to #{file}.")
12
+ Eco::API::Organization::People.new(people)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,59 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Helper to load `People` that works in different phases:
5
+ # 1. first tries to get the newest cached file that follows `filename` pattern
6
+ # - if not the newest, it tries to find the specific filename
7
+ # 2. if it succeeds to identif a cached file, it loads it
8
+ # - if it fails, it tries to get people from the server
9
+ # @note
10
+ # - `filename` will be relative to the working directory (the one of the session `enviro` set by the user).
11
+ # @param filename [String] the name of the file where the cached data is to be found.
12
+ # @param modifier [Array<Symbol>] modifiers to specify how this function should proceed:
13
+ # - `:newest` if it should try to find the newest file (pattern alike).
14
+ # - `:api` if it should try to get people from the server in case there's no cache.
15
+ # - `:file` if it is supposed to load people from a file.
16
+ # - `:save` if it is supposed to cache/save the data locally once obtained people from the server (`:api`)
17
+ # @return [Eco::API::Organization::People] the `People` object with the data.
18
+ def people_load(filename = enviro.config.people.cache, modifier: [:newest, :api])
19
+ modifier = [modifier].flatten
20
+ load_file = [:file, :newest].any? {|flag| modifier.include?(flag)}
21
+ people = case
22
+ when filename && load_file
23
+ if file = people_load_filename(filename, newest: modifier.include?(:newest))
24
+ file_manager.load_json(file).tap do |people|
25
+ logger.info("#{people&.length} people loaded from file #{file}") if people.is_a?(Array)
26
+ end
27
+ else
28
+ logger.error("could not find the file #{file_manager.dir.file(filename)}")
29
+ exit unless modifier.include?(:api)
30
+ people_load(modifier: modifier - [:newest, :file])
31
+ end
32
+ when modifier.include?(:api)
33
+ logger.info("Going to get all the people via API")
34
+ session.batch.get_people.tap do |people|
35
+ if modifier.include?(:save) && people && people.length > 0
36
+ file = file_manager.save_json(people, filename, :timestamp)
37
+ logger.info("#{people.length } people saved to file #{file}.")
38
+ end
39
+ end
40
+ end
41
+ Eco::API::Organization::People.new(people)
42
+ end
43
+
44
+ private
45
+
46
+ def people_load_filename(filename, newest: false)
47
+ if newest
48
+ # search input file based on pattern (in case the name has a timestamp)
49
+ file_manager.dir.newest_file(file: filename).tap do |file|
50
+ logger.info("previous file found: #{file}") if file
51
+ end
52
+ else
53
+ file_manager.dir.file(filename, should_exist: true)
54
+ end
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,31 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Helper to obtain all the elements of `people` anew from the _People Manager_.
5
+ # @note this helper is normally used to run consecutive usecases, where data needs refresh.
6
+ # @param people [Eco::API::Organization::People] the people that needs refresh.
7
+ # @param include_created [Boolean] include people created during this session? (will check `:create` batch jobs).
8
+ # @return [Eco::API::Organization::People] the `People` object with the data.
9
+ def people_refresh(people:, include_created: true)
10
+ ini = people.length
11
+ if include_created
12
+ session.job_groups.find_jobs(type: :create).map do |job|
13
+ people = people.merge(job.people)
14
+ end
15
+ end
16
+
17
+ created = people.length - ini
18
+ msg = "Going to refresh #{people.length} people with server data"
19
+ msg += " (including #{created} that were created)" if created > 0
20
+ logger.info(msg)
21
+ entries = session.batch.get_people(people, silent: true)
22
+
23
+ missing = people.length - entries.length
24
+ logger.error("Missed to obtain #{missing} people during the refresh") if missing > 0
25
+
26
+ Eco::API::Organization::People.new(status.people)
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,65 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Helper to search/obtain people from `data` against the server (_People Manager_).
5
+ # @note
6
+ # - this helper is normally used to **get partial** part of the people manager.
7
+ # - therefore, normally used with _**delta** input files_ (files with only the differences).
8
+ # @param data [Eco::API::Organization::People, Enumerable<Person>, Enumerable<Hash>] `People` to search against the server.
9
+ # @param options [Hash] the options.
10
+ # @param silent [Boolean] `false` if low level search messages should be shown.
11
+ # @return [Eco::API::Organization::People] the `People` object with the found persons.
12
+ def people_search(data, options: {}, silent: true)
13
+ session.logger.info("Going to api get #{data.length} entries...")
14
+ status = session.batch.search(data, silent: silent)
15
+ people = Eco::API::Organization::People.new(status.people)
16
+ session.logger.info("... could get #{people.length} people (out of #{data.length} entries)")
17
+
18
+ # get the supervisors of found people (current supervisors)
19
+ supers = people_search_prepare_supers_request(people)
20
+ if supers.length > 0
21
+ session.logger.info(" Going to api get #{supers.length} current supervisors...")
22
+ status = session.batch.search(supers, silent: silent)
23
+ people = people.merge(status.people, strict: micro.strict_search?(options))
24
+ end
25
+
26
+ # get the supervisors referred in the input data (future supervisors)
27
+ supers = people_search_prepare_supers_request(data, people)
28
+ if supers.length > 0
29
+ session.logger.info(" Going to api get #{supers.length} supervisors as per entries...")
30
+ status = session.batch.search(supers, silent: silent)
31
+ people = people.merge(status.people, strict: micro.strict_search?(options))
32
+ end
33
+
34
+ session.logger.info("Finally got #{people.length} people (out of #{data.length} entries)")
35
+ people
36
+ end
37
+
38
+ private
39
+
40
+ # Prepares a unique request with only the supervisor ids missing in `people`
41
+ def people_search_prepare_supers_request(data, people = data)
42
+ data.each_with_object([]) do |entry, request|
43
+ spr = {"id" => (sup_id = people_search_super_id(entry))}
44
+ unless !sup_id || request.include?(spr)
45
+ micro.with_supervisor(sup_id, people) do |supervisor|
46
+ request.push(spr) unless supervisor
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ # Gets the `supervisor_id` from `value`
53
+ def people_search_super_id(value)
54
+ sup_id = if value.respond_to?(:supervisor_id)
55
+ value.supervisor_id
56
+ elsif value.is_a?(Hash) && value.key("supervisor_id")
57
+ value["supervisor_id"]
58
+ end
59
+ sup_id = nil if sup_id.to_s.strip.empty?
60
+ sup_id
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,19 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # It sets the correct set of abilities based on the usergroups of `person`.
5
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
6
+ # @param options [Hash] the options.
7
+ def refresh_abilities(person, options)
8
+ unless options.dig(:exclude, :account)
9
+ if person.account
10
+ unless options.dig(:exclude, :abilities) && !person.new?
11
+ person.account.permissions_custom = session.new_preset(person)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # When the input data, or `entry`, does not provide the `default_tag`,
5
+ # it sets the `default_tag` of the user following some criteria
6
+ # @note it assumes `default_tag` has been already set to `person.account`
7
+ # @param entry [PersonEntry] the input entry with the data we should set on person.
8
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
9
+ # @param options [Hash] the options
10
+ def refresh_default_tag(entry, person, options)
11
+ if person.account
12
+ unless options.dig(:exclude, :account)
13
+ unless options.dig(:exclude, :filter_tags) || options.dig(:exclude, :default_tag) || entry&.default_tag?
14
+ if session.tagtree
15
+ person.account.default_tag = session.tagtree.default_tag(*person.filter_tags)
16
+ else
17
+ tags = person.filter_tags || []
18
+ person.account.default_tag = tags.first unless tags.length > 1
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ end
26
+ end
27
+ end