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
@@ -1,18 +1,18 @@
1
1
  module Eco
2
2
  module API
3
3
  class MicroCases
4
- #self.define("set_core") do |entry, person, session, options, people, supers|
5
-
4
+ # Sets all the core details, but the supervisor.
5
+ # @note `supervisor_id` requires a special treatment, and therefore is always excluded.
6
6
  # @param entry [PersonEntry] the input entry with the data we should set on person.
7
- # @param person [Person] the person we want to update, carrying the changes to be done.
7
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
8
8
  # @param options [Hash] the options
9
- def set_account(entry, person, options, people, supers_job)
9
+ def set_core(entry, person, options)
10
10
  unless options.dig(:exclude, :core) && !person.new?
11
11
  entry.set_core(person, exclude: micro.core_excluded(person, options))
12
12
  micro.fix_filter_tags(person, options)
13
- micro.set_supervisor(entry, person, options, people, supers_job)
14
13
  end
15
14
  end
15
+
16
16
  end
17
17
  end
18
18
  end
@@ -0,0 +1,23 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Sets all the core details, but the supervisor.
5
+ # @note `supervisor_id` requires a special treatment, and therefore is always excluded.
6
+ # @param entry [PersonEntry] the input entry with the data we should set on person.
7
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
8
+ # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
9
+ # @param supers_job [Eco::API::Session::Batch::Job] the job that will run the supers .
10
+ # @param options [Hash] the options.
11
+ def set_core_with_supervisor(entry, person, people, supers_job, options)
12
+ unless options.dig(:exclude, :core) && !person.new?
13
+ micro.set_core(entry, person, options)
14
+ micro.set_supervisor(entry.supervisor_id, person, people, options) do |unkown_id|
15
+ # delay setting supervisor if does not exit
16
+ supers_job.add(person) {|person| person.supervisor_id = unkown_id}
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -1,26 +1,30 @@
1
1
  module Eco
2
2
  module API
3
3
  class MicroCases
4
- #self.define("set_supervisor") do |entry, person, session, options, people, supers_job|
5
-
6
- def set_supervisor(entry, person, options, people, supers_job)
7
- # set supervisor
4
+ # Special snippet to decide if the `supervisor_id` is set now or in a later batch job `supers_job`.
5
+ # @note delaying the setting of a `supervisor_id` can save errors when the supervisor still does not exit.
6
+ # @param sup_id [nil, String] the **supervisor id** we should set on the `person`.
7
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
8
+ # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
9
+ # @param options [Hash] the options.
10
+ # @yield [supervisor_id] callback when the supervisor_id is **unknown** (not `nil` nor any one's in `people`).
11
+ # @yieldparam supervisor_id [String] the **unknown** `supervisor_id`.
12
+ def set_supervisor(sup_id, person, people, options)
8
13
  unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
9
- if !(sup_id = entry.supervisor_id)
10
- person.supervisor_id = nil
11
- else
12
- if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
14
+ micro.with_supervisor(sup_id, people) do |supervisor|
15
+ if !sup_id
16
+ person.supervisor_id = nil
17
+ elsif supervisor
13
18
  person.supervisor_id = supervisor.id
19
+ elsif !block_given?
20
+ person.supervisor_id = sup_id
14
21
  else
15
- # delay setting supervisor if does not exit
16
- supers_job.add(person) do |person|
17
- person.supervisor_id = sup_id
18
- end
22
+ yield(sup_id) if block_given?
19
23
  end
20
24
  end
21
25
  end
22
26
  end
23
-
27
+
24
28
  end
25
29
  end
26
30
  end
@@ -1,14 +1,19 @@
1
1
  module Eco
2
2
  module API
3
3
  class MicroCases
4
- #self.define("strict_search") do |session, options|
5
-
6
- # @param options [Hash] the options
7
- def strict_search(options)
8
- strict_config = session.config.people.strict_search?
9
- strict_option = !options[:search]&.key?(:strict) || options.dig(:search, :strict)
10
- strict_config && strict_option
4
+ # When trying to find an `person` with given a source `entry`, it states if such a search should be `strict` or `soft`.
5
+ # @note
6
+ # - `strict` searches ignore the email when the source `entry` has an `external_id` specified.
7
+ # - see related command line options `-search-strict` and `-search-soft`
8
+ # @param options [Hash] the options.
9
+ # @return [Boolean] `true` if the search should be `strict` only, and `false` otherwise.
10
+ def strict_search?(options)
11
+ strict_config = session.config.people.strict_search?
12
+ strict_option = options.dig(:search, :strict)
13
+ soft_option = options.dig(:search, :soft) && !strict_option
14
+ (strict_config || strict_option) && !soft_option
11
15
  end
16
+
12
17
  end
13
18
  end
14
19
  end
@@ -0,0 +1,27 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Finds each **entry** of `entries` in `people` and runs a block.
5
+ # @note
6
+ # - it also links to `person.entry` the input data entry.
7
+ # @param entries [Eco::API::Common::People::Entries] the input entries with the data.
8
+ # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
9
+ # @param options [Hash] the options.
10
+ # @yield [entry, person] gives each entry, and the paired person thereof (new or existing).
11
+ # @yieldparam entry [PersonEntry] the input entry with the data we should set on person.
12
+ # @yieldparam person [Ecoportal::API::V1::Person] the found person that matches `entry`, or a new person otherwise.
13
+ # @return [Eco::API::Organization::People] all the people, including new and existing ones.
14
+ def with_each(entries, people, options)
15
+ entries.map do |entry|
16
+ unless person = people.find(entry, strict: micro.strict_search?(options))
17
+ person = session.new_person
18
+ end
19
+ person.entry = entry
20
+ yield(entry, person) if block_given?
21
+ person
22
+ end.yield_self {|all_people| people.newFrom all_people.uniq}
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Detects who has left the organization and `yield` s them one by one to the given block
5
+ # @note to be used only when the input file is the full DB
6
+ # @param entries [Eco::API::Common::People::Entries] the input entries with the data.
7
+ # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
8
+ # @param options [Hash] the options.
9
+ # @yield [person] gives each person of `people` that is not present in `entries`.
10
+ # @yieldparam person [Ecoportal::API::V1::Person] the person that leaves the org.
11
+ # @return [Eco::API::Organization::People] the leavers.
12
+ def with_each_leaver(entries, people, options)
13
+ leavers = people.map do |person|
14
+ unless entries.find(person, strict: micro.strict_search?(options))
15
+ yield(person) if block_given?
16
+ person
17
+ end
18
+ end.compact
19
+ people.newFrom leavers
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Finds those in `entries` that already exist in the organization (`people`) and `yield` s them one by one to the given block.
5
+ # @note
6
+ # - it also links to `person.entry` the input data `entry`.
7
+ # @param entries [Eco::API::Common::People::Entries] the input entries with the data.
8
+ # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
9
+ # @param options [Hash] the options.
10
+ # @param log_starter [Boolean] log error message if an `entry` does not have match in `people`.
11
+ # @yield [entry, person] gives each **found** `person` of `entries` that is not present in `people`.
12
+ # @yieldparam entry [PersonEntry] the input entry with the data we should set on person.
13
+ # @yieldparam person [Ecoportal::API::V1::Person] the **found** person.
14
+ # @return [Eco::API::Organization::People] the found people.
15
+ def with_each_present(entries, people, options, log_starter: false)
16
+ found = []
17
+ micro.with_each(entries, people, options) do |entry, person|
18
+ if person.new? && log_starter
19
+ session.logger.error("This person does not exist: #{entry.to_s(:identify)}")
20
+ next
21
+ end
22
+ found << person
23
+ yield(entry, person) if block_given?
24
+ end
25
+ people.newFrom found
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Detects who in the `entries` is new in the organization and `yield` s them one by one to the given block.
5
+ # @note
6
+ # - it also links to `person.entry` the input data `entry`.
7
+ # @param entries [Eco::API::Common::People::Entries] the input entries with the data.
8
+ # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
9
+ # @param options [Hash] the options.
10
+ # @param log_present [Boolean] log error message if an `entry` has match in `people`.
11
+ # @yield [entry, person] gives each **new** `person` of `entries` that is not present in `people`.
12
+ # @yieldparam entry [PersonEntry] the input entry with the data we should set on person.
13
+ # @yieldparam person [Ecoportal::API::V1::Person] the **new** person.
14
+ # @return [Eco::API::Organization::People] the starters.
15
+ def with_each_starter(entries, people, options, log_present: false)
16
+ starters = []
17
+ micro.with_each(entries, people, options) do |entry, person|
18
+ if !person.new? && log_present
19
+ session.logger.error("This person (id: '#{person.id}') already exists: #{entry.to_s(:identify)}")
20
+ next
21
+ end
22
+ staters << person
23
+ yield(entry, person) if block_given?
24
+ end
25
+ people.newFrom starters
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Finds all the subordinates of `supervisor`.
5
+ # @note if `supervisor` is `nil`, it will return all people with no supervisor.
6
+ # @param supervisor [String, Ecoportal::API::V1::Person, Hash] the `supervisor` or the `id` / `external_id` thereof.
7
+ # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
8
+ # @yield [subordinate] block that does some stuff with `subordinate`.
9
+ # @yieldparam subordinate [Ecoportal::API::V1::Person] each one of the suborinates of `supervisor`.
10
+ # @return [Eco::API::Organization::People] the **subordinates** of `supervisor`.
11
+ def with_each_subordinate(supervisor, people)
12
+ people.supervisor_id(_person_id(supervisor, people)).tap do |subordinates|
13
+ subordinates.each do |subordinate|
14
+ yield(subordinate) if block_given?
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def _person_id(value, people)
22
+ case value
23
+ when Hash
24
+ _person_id(value["id"] || value["external_id"], people)
25
+ when Ecoportal::API::V1::Person
26
+ _person_id(value.id || value.external_id, people)
27
+ when String
28
+ micro.with_supervisor(value, people)&.id
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,36 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Finds the supervisor among `people` by using the `supervisor_id` of `value`.
5
+ # @param value [String, Ecoportal::API::V1::Person, Hash] the subordinate person,
6
+ # or the unique identifier of the supervisor to be found (preferably the `external_id`).
7
+ # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
8
+ # @param strict [Boolean] specifies if the search should be `strict` (see {Eco::API::Organization::People#person}).
9
+ # @yield [supervisor] block that does some stuff with supevisor.
10
+ # @yieldparam supervisor [Ecoportal::API::V1::Person] the person object of the supervisor, or `nil` if not found.
11
+ # @return [nil, Ecoportal::API::V1::Person] found `supervisor` of `value`.
12
+ def with_supervisor(value, people, strict: false)
13
+ if sup_id = with_supervisor_supervisor_id(value)
14
+ people.person(id: sup_id, external_id: sup_id, email: sup_id, strict: strict)
15
+ end.tap do |supervisor|
16
+ yield(supervisor) if block_given?
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def with_supervisor_supervisor_id(value)
23
+ return nil if value.to_s.strip.empty?
24
+ case
25
+ when value.respond_to?(:supervisor_id)
26
+ value.supervisor_id
27
+ when value.is_a?(Hash)
28
+ value["supervisor_id"]
29
+ else
30
+ value
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -3,24 +3,18 @@ module Eco
3
3
  module Organization
4
4
  class People < Eco::Language::Models::Collection
5
5
  # build the shortcuts of Collection
6
- attr_collection :id, :external_id, :email, :name, :supervisor_id
7
6
  attr_presence :account, :details
7
+ attr_collection :id, :external_id, :email, :name, :supervisor_id
8
8
 
9
9
  alias_method :people, :to_a
10
10
 
11
- def initialize(people = [], klass: Ecoportal::API::Internal::Person, factory: nil)
11
+ def initialize(people = [], klass: Ecoportal::API::Internal::Person)
12
12
  @klass = Ecoportal::API::Internal::Person unless klass == Ecoportal::API::V1::Person
13
13
  super(people, klass: @klass)
14
14
  @caches_init = false
15
15
  end
16
16
 
17
- def to_json
18
- to_a.to_json
19
- end
20
-
21
- def [](id_or_ext)
22
- id(id_or_ext) || external_id(id_or_ext)
23
- end
17
+ # @!group Main identifier helpers
24
18
 
25
19
  def id(*args)
26
20
  attr('id', *args).first
@@ -30,6 +24,13 @@ module Eco
30
24
  attr('external_id', *args).first
31
25
  end
32
26
 
27
+ def [](id_or_ext)
28
+ id(id_or_ext) || external_id(id_or_ext)
29
+ end
30
+ # @!endgroup
31
+
32
+ # @!group Special filters
33
+
33
34
  def users
34
35
  account_present(true)
35
36
  end
@@ -42,7 +43,41 @@ module Eco
42
43
  account_present(false)
43
44
  end
44
45
 
45
- # It searches a person using the paramters given.
46
+ def supervisors
47
+ sup_ids = self.ids & self.supervisor_ids
48
+ sup_ids.map do |id|
49
+ person(id: id, strict: true)
50
+ end.yield_self do |supervisors|
51
+ newFrom supervisors
52
+ end
53
+ end
54
+
55
+ def missing_supervisors_ids
56
+ sup_ids = self.supervisor_ids
57
+ sup_ids - (sup_ids & self.ids)
58
+ end
59
+
60
+ def filter_tags_any(tags)
61
+ attr("filter_tags", tags, default_modifier.any.insensitive)
62
+ end
63
+
64
+ def filter_tags_all(tags)
65
+ attr("filter_tags", tags, default_modifier.all.insensitive)
66
+ end
67
+
68
+ def policy_group_ids_any(ids)
69
+ attr("policy_group_ids", tags, default_modifier.any.insensitive)
70
+ end
71
+
72
+ def policy_group_ids_all(ids)
73
+ attr("policy_group_ids", tags, default_modifier.all.insensitive)
74
+ end
75
+
76
+ # @!endgroup
77
+
78
+ # @!group Searchers
79
+
80
+ # It searches a person using the parameters given.
46
81
  # @param id [String] the `internal id` of the person
47
82
  # @param external_id [String] the `exernal_id` of the person
48
83
  # @param email [String] the `email` of the person
@@ -72,23 +107,34 @@ module Eco
72
107
  end
73
108
 
74
109
  def find(object, strict: false)
75
- id = object.respond_to?("id")? object.send("id") : nil
76
- external_id = object.respond_to?("external_id")? object.send("external_id") : nil
77
- email = object.respond_to?("email")? object.send("email") : nil
110
+ id = attr_value(object, "id")
111
+ external_id = attr_value(object, "external_id")
112
+ email = attr_value(object, "email")
78
113
  person(id: id, external_id: external_id, email: email, strict: strict)
79
114
  end
115
+ # @!endgroup
116
+
117
+ # @!group Basic Collection Methods
118
+
119
+ def to_json
120
+ to_a.to_json
121
+ end
122
+
123
+ def newFrom(data)
124
+ self.class.new(data, klass: @klass)
125
+ end
80
126
 
81
127
  def uniq(strict: false, include_unsearchable: false)
82
- init_caches
83
128
  unsearchable = []
84
- newFrom to_a.each_with_object([]) do |person, people|
129
+ to_a.each_with_object([]) do |person, people|
85
130
  if found = find(person, strict: strict)
86
131
  people << found
87
132
  else
88
133
  unsearchable << person
89
134
  end
90
- end.tap do |people|
91
- people << unsearchable if include_unsearchable
135
+ end.yield_self do |found|
136
+ found += unsearchable if include_unsearchable
137
+ newFrom found
92
138
  end
93
139
  end
94
140
 
@@ -103,15 +149,19 @@ module Eco
103
149
  end
104
150
 
105
151
  def exclude!(object, strict: false)
106
- self < exclude(object, strict: strict).to_a
152
+ self < exclude(object, strict: strict)
107
153
  end
108
154
 
109
155
  def exclude_people(list, strict: false)
110
- discarded = list.map do |person|
156
+ list.map do |person|
111
157
  find(person, strict: strict)
112
- end.compact
113
- newFrom to_a - discarded
158
+ end.compact.yield_self do |discarded|
159
+ newFrom to_a - discarded
160
+ end
114
161
  end
162
+ # @!endgroup
163
+
164
+ # @!group Groupping methods
115
165
 
116
166
  def email_id_maps
117
167
  users.group_by(:email).transform_values { |person| person.id }
@@ -121,23 +171,10 @@ module Eco
121
171
  to_h(:supervisor_id)
122
172
  end
123
173
 
124
- # only those that are present
125
- def supervisors
126
- h_all = self.to_h
127
- sup_ids = h_all.keys & self.supervisor_ids
128
- newFrom h_all.select do |id, person|
129
- sup_ids.include?(id)
130
- end.values
131
- end
132
-
133
- def missing_supervisors_ids
134
- sup_ids = self.supervisor_ids
135
- sup_ids - (sup_ids & self.ids)
136
- end
137
-
138
174
  def to_h(attr = "id")
139
175
  super(attr || "id")
140
176
  end
177
+ # @!endgroup
141
178
 
142
179
  protected
143
180