eco-helpers 2.0.25 → 2.0.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +53 -6
  3. data/lib/eco/api/common.rb +0 -1
  4. data/lib/eco/api/common/loaders.rb +2 -0
  5. data/lib/eco/api/common/loaders/base.rb +58 -0
  6. data/lib/eco/api/common/loaders/case_base.rb +33 -0
  7. data/lib/eco/api/common/loaders/error_handler.rb +2 -2
  8. data/lib/eco/api/common/loaders/parser.rb +30 -5
  9. data/lib/eco/api/common/loaders/policy.rb +1 -1
  10. data/lib/eco/api/common/loaders/use_case.rb +1 -1
  11. data/lib/eco/api/common/people/default_parsers/csv_parser.rb +93 -1
  12. data/lib/eco/api/common/people/entries.rb +83 -14
  13. data/lib/eco/api/common/people/entry_factory.rb +10 -9
  14. data/lib/eco/api/common/people/person_attribute_parser.rb +8 -0
  15. data/lib/eco/api/common/people/person_factory.rb +4 -2
  16. data/lib/eco/api/common/people/person_parser.rb +7 -1
  17. data/lib/eco/api/common/people/supervisor_helpers.rb +1 -1
  18. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +0 -8
  19. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +0 -8
  20. data/lib/eco/api/microcases/set_core_with_supervisor.rb +4 -2
  21. data/lib/eco/api/microcases/set_supervisor.rb +29 -8
  22. data/lib/eco/api/microcases/with_each.rb +7 -3
  23. data/lib/eco/api/microcases/with_each_starter.rb +3 -2
  24. data/lib/eco/api/organization/people.rb +1 -1
  25. data/lib/eco/api/session.rb +7 -2
  26. data/lib/eco/api/session/batch/job.rb +8 -0
  27. data/lib/eco/api/usecases/default_cases/create_case.rb +10 -1
  28. data/lib/eco/api/usecases/default_cases/create_details_case.rb +10 -1
  29. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +10 -1
  30. data/lib/eco/api/usecases/default_cases/hris_case.rb +6 -2
  31. data/lib/eco/api/usecases/default_cases/upsert_case.rb +10 -1
  32. data/lib/eco/cli/config/default/input.rb +2 -2
  33. data/lib/eco/cli/config/default/options.rb +23 -7
  34. data/lib/eco/cli/config/default/usecases.rb +16 -0
  35. data/lib/eco/cli/config/default/workflow.rb +7 -4
  36. data/lib/eco/cli/config/filters.rb +6 -2
  37. data/lib/eco/cli/config/filters/input_filters.rb +3 -2
  38. data/lib/eco/cli/config/filters/people_filters.rb +3 -2
  39. data/lib/eco/cli/config/help.rb +1 -1
  40. data/lib/eco/cli/config/options_set.rb +6 -4
  41. data/lib/eco/cli/config/use_cases.rb +6 -3
  42. data/lib/eco/csv.rb +2 -0
  43. data/lib/eco/version.rb +1 -1
  44. metadata +3 -2
  45. data/lib/eco/api/common/base_loader.rb +0 -72
@@ -80,21 +80,21 @@ module Eco
80
80
  # @param data [Array<Hash>] data to be parsed. It cannot be used alongside with `file:`
81
81
  # @param file [String] absolute or relative path to the input file. It cannot be used alongside with `data:`.
82
82
  # @param format [Symbol] it must be used when you use the option `file:` (i.e. `:xml`, `:csv`), as it specifies the format of the input `file:`.
83
- # @param encoding [String] optional parameter to read `file:` by expecting certain encoding.
83
+ # @param options [Hash] further options.
84
+ # @option options [String] :encoding optional parameter to read `file:` by expecting certain encoding.
85
+ # @option options [Boolean] :check_headers signals if the `csv` file headers should be expected.
84
86
  # @return [Eco::API::Common::People::Entries] collection of `Eco::API::Common::People::PersonEntry`.
85
- def entries(data: (no_data = true; nil), file: (no_file = true; nil), format: (no_format = true; nil), encoding: nil)
87
+ def entries(data: (no_data = true; nil), file: (no_file = true; nil), format: (no_format = true; nil), **options)
86
88
  fatal("You should at least use data: or file:, but not both") if no_data == no_file
87
89
  fatal("You must specify a valid format: (symbol) when you use file.") if file && no_format
88
90
  fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
89
91
  fatal("There is no parser/serializer for format ':#{format.to_s}'") unless no_format || @person_parser.defined?(format)
90
92
 
91
- kargs = {}
92
- kargs.merge!(content: data) unless no_data
93
- kargs.merge!(file: file) unless no_file
94
- kargs.merge!(format: format) unless no_format
95
- kargs.merge!(encoding: encoding) if encoding
93
+ options.merge!(content: data) unless no_data
94
+ options.merge!(file: file) unless no_file
95
+ options.merge!(format: format) unless no_format
96
96
 
97
- Entries.new(to_array_of_hashes(**kargs), klass: PersonEntry, factory: self)
97
+ Entries.new(to_array_of_hashes(**options), klass: PersonEntry, factory: self)
98
98
  end
99
99
 
100
100
  def to_array_of_hashes(**kargs)
@@ -118,7 +118,8 @@ module Eco
118
118
  logger.error("Input data as 'Hash' not supported. Expecting 'Enumerable' or 'String'")
119
119
  exit(1)
120
120
  when String
121
- to_array_of_hashes(content: person_parser.parse(format, content))
121
+ deps = {check_headers: true} if kargs[:check_headers]
122
+ to_array_of_hashes(content: person_parser.parse(format, content, deps: deps || {}))
122
123
  when Enumerable
123
124
  sample = content.to_a.first
124
125
  case sample
@@ -6,6 +6,14 @@ module Eco
6
6
  # Class to define a parser/serializer.
7
7
  class PersonAttributeParser < Eco::Language::Models::ParserSerializer
8
8
 
9
+ # @note
10
+ # - This was introduced at a later stage and might not be available for certain org-parsers configs
11
+ # @return [RequiredAttrs]
12
+ def required_attrs
13
+ @required_attrs ||= @dependencies[:required_attrs]
14
+ #@required_attrs ||= RequiredAttrs.new(attr, :unkown, [attr])
15
+ end
16
+
9
17
  # @see Eco::Language::Models::ParserSerializer#def_parser
10
18
  # @note
11
19
  # - additionally, you can declare a callback `active:` to determine if when the
@@ -10,7 +10,7 @@ module Eco
10
10
 
11
11
  attr_reader :schema, :schema_attrs
12
12
 
13
- def initialize(person: {}, schema: {}, account: {}, modifier: Common::People::PersonModifier.new)
13
+ def initialize(person: nil, schema: {}, account: {}, modifier: Common::People::PersonModifier.new)
14
14
  @modifier = Common::People::PersonModifier.new(modifier)
15
15
  @person = person
16
16
  @account = account
@@ -77,7 +77,9 @@ module Eco
77
77
  when Hash
78
78
  JSON.parse(person.to_json)
79
79
  else
80
- {}
80
+ {
81
+ "subordinates" => 0
82
+ }
81
83
  end
82
84
  end
83
85
 
@@ -59,9 +59,15 @@ module Eco
59
59
 
60
60
  # @!group Scopping attributes (identifying, presence & active)
61
61
 
62
+ # @return [Array<Eco::API::Common::Loaders::Parser::RequiredAttrs>]
63
+ def required_attrs
64
+ @parsers.values_at(*all_attrs(include_defined_parsers: true)).compact.map(&:required_attrs).compact
65
+ end
66
+
62
67
  # All the internal name attributes, including _core_, _account_ and _details_.
63
68
  def all_attrs(include_defined_parsers: false)
64
- all_model_attrs | defined_model_attrs
69
+ return all_model_attrs | defined_model_attrs if include_defined_parsers
70
+ all_model_attrs
65
71
  end
66
72
 
67
73
  # Scopes `source_attrs` using the _**core** attributes_.
@@ -15,7 +15,7 @@ module Eco
15
15
  # Reorders as follows:
16
16
  # 1. supervisors, people with no supervisor or where their supervisor not present
17
17
  # 2. subordinates
18
- # @return [Array<Entry>] `values` sorted by supervisors/subordinates
18
+ # @return [Array<PersonEntry>] `values` sorted by supervisors/subordinates
19
19
  def sort_by_supervisors(values, supervisors_first: true)
20
20
  raise "Expected non hash Enumerable. Given: #{values.class}" if values.is_a?(Hash)
21
21
  return [] unless values && values.is_a?(Enumerable)
@@ -5,14 +5,6 @@ module Ecoportal
5
5
  class Person
6
6
  attr_accessor :entry
7
7
 
8
- def reset_details!
9
- doc["details"] = JSON.parse(original_doc["details"])
10
- end
11
-
12
- def consolidate_details!
13
- original_doc["details"] = JSON.parse(doc["details"])
14
- end
15
-
16
8
  def identify(section = :person)
17
9
  if entry && section == :entry
18
10
  entry.to_s(:identify)
@@ -3,14 +3,6 @@ module Ecoportal
3
3
  class Internal
4
4
  class Person
5
5
 
6
- def reset_account!
7
- doc["account"] = JSON.parse(original_doc["account"])
8
- end
9
-
10
- def consolidate_account!
11
- original_doc["account"] = JSON.parse(doc["account"])
12
- end
13
-
14
6
  def new?(doc = :initial)
15
7
  ref_doc = (doc == :original) ? original_doc : initial_doc
16
8
  !ref_doc["details"] && !ref_doc["account"]
@@ -12,9 +12,11 @@ module Eco
12
12
  unless options.dig(:exclude, :core) && !person.new?
13
13
  micro.set_core(entry, person, options)
14
14
  if entry.supervisor_id?
15
- micro.set_supervisor(entry.supervisor_id, person, people, options) do |unkown_id|
15
+ micro.set_supervisor(person, entry.supervisor_id, people, options) do |unknown_id|
16
16
  # delay setting supervisor if does not exit
17
- supers_job.add(person) {|person| person.supervisor_id = unkown_id}
17
+ supers_job.add(person) do |person|
18
+ micro.set_supervisor(person, unknown_id, people, options)
19
+ end
18
20
  end
19
21
  end
20
22
  end
@@ -1,22 +1,27 @@
1
1
  module Eco
2
2
  module API
3
3
  class MicroCases
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`.
4
+ # Unique access point to set the `supervisor_id` value on a person.
7
5
  # @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.
6
+ # @param sup_id [nil, String] the **supervisor id** we should set on the `person`.
7
+ # @param people [Eco::API::Organization::People] _People_ involved in the current update.
9
8
  # @param options [Hash] the options.
10
9
  # @yield [supervisor_id] callback when the supervisor_id is **unknown** (not `nil` nor any one's in `people`).
11
10
  # @yieldparam supervisor_id [String] the **unknown** `supervisor_id`.
12
- def set_supervisor(sup_id, person, people, options)
11
+ def set_supervisor(person, sup_id, people, options)
13
12
  unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
14
- micro.with_supervisor(sup_id, people) do |supervisor|
13
+ cur_id = person.supervisor_id
14
+ cur_super = cur_id && with_supervisor(cur_id, people)
15
+ micro.with_supervisor(sup_id, people) do |new_super|
15
16
  if !sup_id
16
17
  person.supervisor_id = nil
17
- elsif supervisor
18
- person.supervisor_id = supervisor.id
18
+ descrease_subordinates(cur_super)
19
+ elsif new_super && id = new_super.id
20
+ person.supervisor_id = id
21
+ descrease_subordinates(cur_super)
22
+ increase_subordinates(new_super)
19
23
  elsif !block_given?
24
+ descrease_subordinates(cur_super)
20
25
  person.supervisor_id = sup_id
21
26
  else
22
27
  yield(sup_id) if block_given?
@@ -25,6 +30,22 @@ module Eco
25
30
  end
26
31
  end
27
32
 
33
+ private
34
+
35
+ def descrease_subordinates(person, by = 1)
36
+ if person.is_a?(Ecoportal::API::V1::Person)
37
+ person.subordinates -= by
38
+ #person.subordinates = 0 if person.subordinates < 0
39
+ end
40
+ end
41
+
42
+ def increase_subordinates(person, by = 1)
43
+ if person.is_a?(Ecoportal::API::V1::Person)
44
+ #person.subordinates = 0 if person.subordinates < 0
45
+ person.subordinates += by
46
+ end
47
+ end
48
+
28
49
  end
29
50
  end
30
51
  end
@@ -7,16 +7,20 @@ module Eco
7
7
  # @param entries [Eco::API::Common::People::Entries] the input entries with the data.
8
8
  # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
9
9
  # @param options [Hash] the options.
10
+ # @param append_created [Boolean] whether or not a new person will be added to the `people` object.
10
11
  # @yield [entry, person] gives each entry, and the paired person thereof (new or existing).
11
12
  # @yieldparam entry [PersonEntry] the input entry with the data we should set on person.
12
13
  # @yieldparam person [Ecoportal::API::V1::Person] the found person that matches `entry`, or a new person otherwise.
13
14
  # @return [Eco::API::Organization::People] all the people, including new and existing ones.
14
- def with_each(entries, people, options)
15
+ def with_each(entries, people, options, append_created: true)
15
16
  @_skip_all_multiple_results = false
17
+ people_copy = people.newFrom(people.to_a)
16
18
  entries.each_with_object([]) do |entry, scoped|
17
19
  begin
18
- unless person = people.find(entry, strict: micro.strict_search?(options))
19
- person = session.new_person
20
+ unless person = people_copy.find(entry, strict: micro.strict_search?(options))
21
+ person = session.new_person.tap do |person|
22
+ people << person if append_created
23
+ end
20
24
  end
21
25
  rescue Eco::API::Organization::People::MultipleSearchResults => e
22
26
  unless @_skip_all_multiple_results
@@ -8,13 +8,14 @@ module Eco
8
8
  # @param people [Eco::API::Organization::People] target existing _People_ of the current update.
9
9
  # @param options [Hash] the options.
10
10
  # @param log_present [Boolean] log error message if an `entry` has match in `people`.
11
+ # @param append_created [Boolean] whether or not a new person will be added to the `people` object.
11
12
  # @yield [entry, person] gives each **new** `person` of `entries` that is not present in `people`.
12
13
  # @yieldparam entry [PersonEntry] the input entry with the data we should set on person.
13
14
  # @yieldparam person [Ecoportal::API::V1::Person] the **new** person.
14
15
  # @return [Eco::API::Organization::People] the starters.
15
- def with_each_starter(entries, people, options, log_present: false)
16
+ def with_each_starter(entries, people, options, log_present: false, append_created: true)
16
17
  starters = []
17
- micro.with_each(entries, people, options) do |entry, person|
18
+ micro.with_each(entries, people, options, append_created: append_created) do |entry, person|
18
19
  if !person.new?
19
20
  if log_present
20
21
  session.logger.error("This person (id: '#{person.id}') already exists: #{entry.to_s(:identify)}")
@@ -258,7 +258,7 @@ module Eco
258
258
 
259
259
  def init_caches
260
260
  return if @caches_init
261
- @by_id = to_h
261
+ @by_id = no_nil_key(to_h)
262
262
  @by_external_id = no_nil_key(to_h('external_id'))
263
263
  @by_users_email = no_nil_key(existing_users.to_h('email'))
264
264
  @by_non_users_email = no_nil_key(non_users.to_h('email'))
@@ -153,9 +153,14 @@ module Eco
153
153
  # Generates an entries collection from a csv input file.
154
154
  # @see Eco::API::Common::People::EntryFactory#entries
155
155
  # @param file [String] file to generate the entries from.
156
+ # @param (see Eco::API::Session#entries)
156
157
  # @return [Eco::API::Common::People::Entries] collection of entries.
157
- def csv_entries(file)
158
- return entries(file: file, format: :csv)
158
+ def csv_entries(file, **kargs)
159
+ kargs.merge!({
160
+ file: file,
161
+ format: :csv
162
+ })
163
+ return entries(**kargs)
159
164
  end
160
165
 
161
166
  # Generates the collection of entries that should be discarded from an update.
@@ -304,6 +304,9 @@ module Eco
304
304
  if !simulate && status
305
305
  status.queue.map do |entry|
306
306
  if status.success?(entry)
307
+ if type == :create && entry.respond_to?(:id=)
308
+ entry.id = status[entry].body["id"]
309
+ end
307
310
  entry.consolidate! if entry.respond_to?(:consolidate!)
308
311
  #else # do not entry.reset! (keep track on changes still)
309
312
  end
@@ -324,7 +327,12 @@ module Eco
324
327
  end
325
328
  end
326
329
  elsif simulate
330
+ fake_id = 111111111111111111111111
327
331
  queue.map do |entry|
332
+ if type == :create && entry.respond_to?(:id=)
333
+ entry.id = fake_id.to_s
334
+ fake_id += 1
335
+ end
328
336
  entry.consolidate! if entry.respond_to?(:consolidate!)
329
337
  end
330
338
  end
@@ -2,12 +2,15 @@ class Eco::API::UseCases::DefaultCases::CreateCase < Eco::API::Common::Loaders::
2
2
  name "create"
3
3
  type :sync
4
4
 
5
+ attr_reader :options
6
+
5
7
  def main(entries, people, session, options, usecase)
8
+ options = @options
6
9
  micro = session.micro
7
10
  creation = session.new_job("main", "create", :create, usecase)
8
11
  supers = session.new_job("post", "supers", :update, usecase, :core)
9
12
 
10
- micro.with_each_starter(entries, people, options, log_present: true) do |entry, person|
13
+ micro.with_each_starter(entries, people, options, log_present: true, append_created: append_created) do |entry, person|
11
14
  creation.add(person)
12
15
  micro.set_core_with_supervisor(entry, person, people, supers, options)
13
16
  entry.set_details(person) unless options.dig(:exclude, :details)
@@ -15,4 +18,10 @@ class Eco::API::UseCases::DefaultCases::CreateCase < Eco::API::Common::Loaders::
15
18
  end
16
19
  end
17
20
 
21
+ private
22
+
23
+ def append_created
24
+ options.dig(:people, :append_created)
25
+ end
26
+
18
27
  end
@@ -2,15 +2,24 @@ class Eco::API::UseCases::DefaultCases::CreateDetailsCase < Eco::API::Common::Lo
2
2
  name "create-details"
3
3
  type :sync
4
4
 
5
+ attr_reader :options
6
+
5
7
  def main(entries, people, session, options, usecase)
8
+ @options = options
6
9
  micro = session.micro
7
10
  creation = session.new_job("main", "create", :create, usecase)
8
11
 
9
- micro.with_each_starter(entries, people, options, log_present: true) do |entry, person|
12
+ micro.with_each_starter(entries, people, options, log_present: true, append_created: append_created) do |entry, person|
10
13
  creation.add(person)
11
14
  micro.set_core(entry, person, options)
12
15
  entry.set_details(person) unless options.dig(:exclude, :details)
13
16
  end
14
17
  end
15
18
 
19
+ private
20
+
21
+ def append_created
22
+ options.dig(:people, :append_created)
23
+ end
24
+
16
25
  end
@@ -2,16 +2,25 @@ class Eco::API::UseCases::DefaultCases::CreateDetailsWithSupervisorCase < Eco::A
2
2
  name "create-details-with-supervisor"
3
3
  type :sync
4
4
 
5
+ attr_reader :options
6
+
5
7
  def main(entries, people, session, options, usecase)
8
+ @options = options
6
9
  micro = session.micro
7
10
  creation = session.new_job("main", "create", :create, usecase)
8
11
  supers = session.new_job("post", "supers", :update, usecase, :core)
9
12
 
10
- micro.with_each_starter(entries, people, options, log_present: true) do |entry, person|
13
+ micro.with_each_starter(entries, people, options, log_present: true, append_created: append_created) do |entry, person|
11
14
  creation.add(person)
12
15
  micro.set_core_with_supervisor(entry, person, people, supers, options)
13
16
  entry.set_details(person) unless options.dig(:exclude, :details)
14
17
  end
15
18
  end
16
19
 
20
+ private
21
+
22
+ def append_created
23
+ options.dig(:people, :append_created)
24
+ end
25
+
17
26
  end
@@ -18,7 +18,7 @@ class Eco::API::UseCases::DefaultCases::HrisCase < Eco::API::Common::Loaders::Us
18
18
  leavers.add(person, &method(:leavers_callback))
19
19
  end
20
20
 
21
- micro.with_each(entries, people, options) do |entry, person|
21
+ micro.with_each(entries, people, options, append_created: append_created) do |entry, person|
22
22
  person.new? ? creation.add(person) : update.add(person)
23
23
  micro.set_core_with_supervisor(entry, person, people, supers, options)
24
24
  entry.set_details(person) unless options.dig(:exclude, :details)
@@ -28,6 +28,10 @@ class Eco::API::UseCases::DefaultCases::HrisCase < Eco::API::Common::Loaders::Us
28
28
 
29
29
  private
30
30
 
31
+ def append_created
32
+ options.dig(:people, :append_created)
33
+ end
34
+
31
35
  def leavers_callback(person)
32
36
  person.supervisor_id = nil
33
37
  person.account = nil if person.account
@@ -41,7 +45,7 @@ class Eco::API::UseCases::DefaultCases::HrisCase < Eco::API::Common::Loaders::Us
41
45
  if other_people.length > 3
42
46
  msg = "There are #{other_people.length} people in schemas other than #{active_schema.name}."
43
47
  msg << " Please, use the filter option '-schema_id SchemaName' for the 'hris' case to only include those of that schema"
44
- msg << " in the current update. The HRIS case identifies people that are not in the file as leavers."
48
+ msg << " in the current update. The HRIS case identifies people that are not in the file as leavers"
45
49
  msg << " (as it will remove the account of all the people of other schemas if they are not in the input file)."
46
50
  msg << "\n For example: -schema-id '#{active_schema.name.downcase}'"
47
51
  logger.error(msg)
@@ -2,13 +2,16 @@ class Eco::API::UseCases::DefaultCases::UpsertCase < Eco::API::Common::Loaders::
2
2
  name "upsert"
3
3
  type :sync
4
4
 
5
+ attr_reader :options
6
+
5
7
  def main(entries, people, session, options, usecase)
8
+ @options = options
6
9
  micro = session.micro
7
10
  creation = session.new_job("main", "create", :create, usecase)
8
11
  update = session.new_job("main", "update", :update, usecase)
9
12
  supers = session.new_job("post", "supers", :update, usecase, :core)
10
13
 
11
- micro.with_each(entries, people, options) do |entry, person|
14
+ micro.with_each(entries, people, options, append_created: append_created) do |entry, person|
12
15
  person.new? ? creation.add(person) : update.add(person)
13
16
  micro.set_core_with_supervisor(entry, person, people, supers, options)
14
17
  entry.set_details(person) unless options.dig(:exclude, :details)
@@ -16,4 +19,10 @@ class Eco::API::UseCases::DefaultCases::UpsertCase < Eco::API::Common::Loaders::
16
19
  end
17
20
  end
18
21
 
22
+ private
23
+
24
+ def append_created
25
+ options.dig(:people, :append_created)
26
+ end
27
+
19
28
  end