eco-helpers 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eco/api.rb +1 -0
  3. data/lib/eco/api/common.rb +1 -0
  4. data/lib/eco/api/common/class_helpers.rb +33 -0
  5. data/lib/eco/api/common/people.rb +1 -0
  6. data/lib/eco/api/common/people/person_attribute_parser.rb +52 -0
  7. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +2 -2
  8. data/lib/eco/api/common/people/person_parser.rb +16 -10
  9. data/lib/eco/api/common/version_patches.rb +2 -3
  10. data/lib/eco/api/common/version_patches/ecoportal_api.rb +4 -0
  11. data/lib/eco/api/common/version_patches/{base_model.rb → ecoportal_api/base_model.rb} +0 -0
  12. data/lib/eco/api/common/version_patches/{external_person.rb → ecoportal_api/external_person.rb} +0 -0
  13. data/lib/eco/api/common/version_patches/{internal_person.rb → ecoportal_api/internal_person.rb} +0 -0
  14. data/lib/eco/api/common/version_patches/hash.rb +2 -0
  15. data/lib/eco/api/common/version_patches/hash/deep_merge.rb +34 -0
  16. data/lib/eco/api/error.rb +133 -0
  17. data/lib/eco/api/error/handler.rb +19 -0
  18. data/lib/eco/api/error/handlers.rb +22 -0
  19. data/lib/eco/api/organization/people.rb +11 -11
  20. data/lib/eco/api/organization/policy_groups.rb +8 -0
  21. data/lib/eco/api/policies.rb +26 -3
  22. data/lib/eco/api/policies/policy.rb +4 -5
  23. data/lib/eco/api/session.rb +27 -18
  24. data/lib/eco/api/session/batch.rb +12 -6
  25. data/lib/eco/api/session/batch/errors.rb +134 -0
  26. data/lib/eco/api/session/batch/job.rb +213 -0
  27. data/lib/eco/api/session/batch/jobs.rb +72 -0
  28. data/lib/eco/api/session/batch/jobs_groups.rb +85 -0
  29. data/lib/eco/api/session/batch/status.rb +133 -0
  30. data/lib/eco/api/session/config.rb +36 -14
  31. data/lib/eco/api/session/config/base_config.rb +2 -0
  32. data/lib/eco/api/session/config/people.rb +8 -0
  33. data/lib/eco/api/session/config/post_launch.rb +58 -0
  34. data/lib/eco/api/session/config/workflow.rb +189 -0
  35. data/lib/eco/api/session/task.rb +49 -6
  36. data/lib/eco/api/usecases.rb +137 -2
  37. data/lib/eco/api/usecases/base_case.rb +20 -8
  38. data/lib/eco/api/usecases/base_io.rb +97 -0
  39. data/lib/eco/api/usecases/default_case.rb +19 -0
  40. data/lib/eco/api/usecases/default_cases.rb +2 -2
  41. data/lib/eco/api/usecases/default_cases/change_email_case.rb +2 -2
  42. data/lib/eco/api/usecases/default_cases/create_case.rb +2 -2
  43. data/lib/eco/api/usecases/default_cases/create_details_case.rb +2 -2
  44. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +2 -2
  45. data/lib/eco/api/usecases/default_cases/delete_case.rb +2 -2
  46. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +2 -2
  47. data/lib/eco/api/usecases/default_cases/hris_case.rb +2 -2
  48. data/lib/eco/api/usecases/default_cases/new_email_case.rb +2 -2
  49. data/lib/eco/api/usecases/default_cases/new_id_case.rb +2 -2
  50. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +11 -8
  51. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +2 -2
  52. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +2 -2
  53. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +2 -2
  54. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +2 -2
  55. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +2 -2
  56. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +2 -2
  57. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +2 -2
  58. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +4 -4
  59. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +3 -3
  60. data/lib/eco/api/usecases/default_cases/update_case.rb +2 -2
  61. data/lib/eco/api/usecases/default_cases/update_details_case.rb +2 -2
  62. data/lib/eco/api/usecases/default_cases/upsert_case.rb +2 -2
  63. data/lib/eco/api/usecases/use_case.rb +23 -36
  64. data/lib/eco/api/usecases/use_case_chain.rb +14 -24
  65. data/lib/eco/api/usecases/use_case_io.rb +23 -75
  66. data/lib/eco/assets.rb +11 -11
  67. data/lib/eco/cli.rb +37 -0
  68. data/lib/eco/cli/config.rb +63 -1
  69. data/lib/eco/cli/config/default.rb +15 -0
  70. data/lib/eco/cli/config/default/filters.rb +69 -0
  71. data/lib/eco/cli/config/default/input.rb +21 -0
  72. data/lib/eco/cli/config/default/options.rb +47 -0
  73. data/lib/eco/cli/config/default/people.rb +39 -0
  74. data/lib/eco/cli/config/default/usecases.rb +63 -0
  75. data/lib/eco/cli/config/default/workflow.rb +86 -0
  76. data/lib/eco/cli/config/input.rb +40 -0
  77. data/lib/eco/cli/config/options_set.rb +35 -0
  78. data/lib/eco/cli/config/people_filters.rb +38 -0
  79. data/lib/eco/cli/config/use_cases.rb +87 -0
  80. data/lib/eco/cli/scripting/args_helpers.rb +10 -4
  81. data/lib/eco/cli/scripting/argument.rb +6 -0
  82. data/lib/eco/language/models/collection.rb +1 -0
  83. data/lib/eco/version.rb +1 -1
  84. metadata +32 -12
  85. data/lib/eco/api/policies/base_policy.rb +0 -14
  86. data/lib/eco/api/policies/used_policies.rb +0 -37
  87. data/lib/eco/api/session/batch_job.rb +0 -215
  88. data/lib/eco/api/session/batch_jobs.rb +0 -62
  89. data/lib/eco/api/session/batch_status.rb +0 -205
  90. data/lib/eco/api/session/job_groups.rb +0 -75
  91. data/lib/eco/api/usecases/use_group.rb +0 -124
  92. data/lib/eco/cli/config/options.rb +0 -11
@@ -0,0 +1,97 @@
1
+ module Eco
2
+ module API
3
+ class UseCases
4
+ class BaseIO < BaseCase
5
+ @types = [:import, :filter, :transform, :sync, :export]
6
+
7
+ class << self
8
+ def input_required?(type)
9
+ !valid_type?(type) || [:import, :sync].include?(type)
10
+ end
11
+
12
+ def people_required?(type)
13
+ !valid_type?(type) || [:filter, :transform, :sync, :export].include?(type)
14
+ end
15
+
16
+ end
17
+
18
+ attr_reader :input, :people, :session, :options
19
+ attr_reader :type
20
+ attr_accessor :output
21
+
22
+ # @param type [Symbol] a valid type (among `self.class.types`)
23
+ # @param input [Eco::API::Common::People::Entries, Eco::API::Organization::People] the input data of reference.
24
+ # @param people [Eco::API::Organization::People] people object.
25
+ # @param session [Eco::API:Session]
26
+ # @param options [Hash] hash with symbol keys (i.e. behaviour modifiers, cli trackers, filters, etc.)
27
+ def initialize(type: nil, input: nil, people: nil, session:, options: {})
28
+ @output = nil
29
+ self.type = type if type
30
+
31
+ if self.type
32
+ validate_args(input: input, people: people, session: session, options: options)
33
+ end
34
+
35
+ @input = input
36
+ @people = people
37
+ @session = session
38
+ @options = options
39
+ end
40
+
41
+ # @param value [Symbol] a valid type (among `self.class.types`)
42
+ def type=(value)
43
+ self.class.validate_type(value)
44
+ @type = value
45
+ end
46
+
47
+ # Helper to obtain an `BaseIO` objcect from any child class.
48
+ # @return [Eco::API::UseCases::BaseIO]
49
+ def base
50
+ kargs = params(keyed: true).merge(type: self.type).slice(:type, :input, :people, :session, :options)
51
+ Eco::API::UseCases::BaseIO.new(**kargs)
52
+ end
53
+
54
+ # @see Eco::API::UseCases::BaseIO#initialize
55
+ # @return [Eco::API::UseCases::BaseIO]
56
+ def new(type: self.type, input: self.input, people: self.people, session: self.session, options: self.options)
57
+ self.class.new(type: type, input: input, people: people, session: session, options: options)
58
+ end
59
+
60
+ # Helper to build a `Hash` of symbol keys or `Array` with params to do callbacks.
61
+ def params(keyed: false)
62
+ kargs = {}
63
+ kargs.merge!(input: input) if input_required?
64
+ kargs.merge!(people: people) if people_required?
65
+ kargs.merge!(session: session, options: options)
66
+ keyed ? kargs : kargs.values
67
+ end
68
+
69
+ private
70
+
71
+ def validate_args(input:, people:, session:, options:)
72
+ case
73
+ when !session.is_a?(Eco::API::Session)
74
+ raise "A UseCase needs a Session object. Given: #{session}"
75
+ when input_required? && !input
76
+ raise "UseCase of type '#{type}' requires a valid input. None given"
77
+ when people_required? && !people.is_a?(Eco::API::Organization::People)
78
+ raise "UseCase of type '#{type}' requires a People object. Given: #{people}"
79
+ when !options || (options && !options.is_a?(Hash))
80
+ raise "To inject dependencies via ':options' it should be a Hash object. Given: #{options}"
81
+ end
82
+ true
83
+ end
84
+
85
+ def input_required?
86
+ self.class.input_required?(type)
87
+ end
88
+
89
+ def people_required?
90
+ self.class.people_required?(type)
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,19 @@
1
+ module Eco
2
+ module API
3
+ class UseCases
4
+ class DefaultCase
5
+
6
+ def initialize(cases, **options)
7
+ raise "Expected a Eco::API::UseCases object. Given #{cases.class}" if !cases.is_a?(Eco::API::UseCases)
8
+ @cases = cases
9
+ @options = options
10
+ end
11
+
12
+ def process
13
+ raise "You should reimplement this method"
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,7 +1,7 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
4
- class DefaultCases < UseGroup
3
+ class UseCases
4
+ class DefaultCases < Eco::API::UseCases
5
5
 
6
6
  def initialize(*args)
7
7
  super(*args)
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class ChangeEMailCase < BaseCase
5
+ class ChangeEMailCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("change-email", type: :sync) do |entries, people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class CreateCase < UseCases::BaseCase
5
+ class CreateCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("create", type: :sync) do |entries, people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class CreateDetailsCase < UseCases::BaseCase
5
+ class CreateDetailsCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("create-details", type: :sync) do |entries, people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class CreateDetailsWithSupervisorCase < UseCases::BaseCase
5
+ class CreateDetailsWithSupervisorCase < DefaultCase
6
6
 
7
7
  def process
8
8
  # good candidate to do @cases.case("create-details").use.chain(@cases.case("set-supervisor").use)
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class DeleteCase < BaseCase
5
+ class DeleteCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("delete", type: :transform) do |people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class EmailAsIdCase < BaseCase
5
+ class EmailAsIdCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("email-as-id", type: :transform) do |people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class HrisCase < BaseCase
5
+ class HrisCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("hris", type: :sync) do |entries, people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class NewEmailCase < BaseCase
5
+ class NewEmailCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("new-email", type: :sync) do |entries, people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class NewIdCase < BaseCase
5
+ class NewIdCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("new-id", type: :sync) do |entries, people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class RecoverDBCase < BaseCase
5
+ class RecoverDBCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("recover-db", type: :sync) do |entries, people, session, options, usecase|
@@ -10,22 +10,25 @@ module Eco
10
10
  raise "Your input should be an 'Eco::API::Organization::People' object. Got: #{entries.class}"
11
11
  end
12
12
 
13
- creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account])
14
- update = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account])
15
- remove = session.job_group("main").new("remove", usecase: usecase, type: :delete, sets: [:core, :details, :account])
13
+ if options.dig(:include, :create)
14
+ creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account])
15
+ end
16
16
 
17
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
18
- pgs = session.policy_groups
17
+ update = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account])
19
18
 
20
19
  if options.dig(:include, :delete)
20
+ remove = session.job_group("main").new("remove", usecase: usecase, type: :delete, sets: [:core, :details, :account])
21
21
  people.exclude(entries).map {|person| remove.add(person)}
22
22
  end
23
23
 
24
+ strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
25
+ pgs = session.policy_groups
26
+
24
27
  entries.each.with_index do |entry, i|
25
28
  create = ! (person = people.find(entry, strict: strict_search))
26
29
 
27
30
  if create && !options.dig(:include, :create)
28
- session.logger.error("Entry(#{i}) - this person does not exist: #{entry.email}")
31
+ session.logger.error("Entry(#{i}) - this person does not exist: #{entry.name} (#{entry.email})")
29
32
  else
30
33
  person = session.new_person if create
31
34
 
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class RefreshPresets < BaseCase
5
+ class RefreshPresets < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("refresh-presets", type: :transform) do |people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class ReinviteCase < BaseCase
5
+ class ReinviteCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("reinvite", type: :transform) do |people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class RemoveAccountCase < BaseCase
5
+ class RemoveAccountCase < DefaultCase
6
6
 
7
7
  def process
8
8
 
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class ResetLandingPageCase < UseCases::BaseCase
5
+ class ResetLandingPageCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("reset-landing-page", type: :transform) do |people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class SetDefaultTagCase < BaseCase
5
+ class SetDefaultTagCase < DefaultCase
6
6
  # take the deepest tag (the one that is further down in the tree)
7
7
  # different options (several nodes at the same depth):
8
8
  # => take the common node between them (i.e. you have Hamilton and Auckland -> take New Zealand)
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class SetSupervisorCase < BaseCase
5
+ class SetSupervisorCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("set-supervisor", type: :sync) do |entries, people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class SwitchSupervisorCase < UseCases::BaseCase
5
+ class SwitchSupervisorCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("switch-supervisor", type: :transform) do |people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class ToCsvCase < BaseCase
5
+ class ToCsvCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("to-csv", type: :export) do |people, session, options, usecase|
@@ -11,7 +11,7 @@ module Eco
11
11
  next false
12
12
  end
13
13
 
14
- unless file = options[:file]
14
+ unless file = options[:file] || options.dig(:export, :file, :name)
15
15
  session.logger.error("Destination file not specified")
16
16
  next false
17
17
  end
@@ -22,7 +22,7 @@ module Eco
22
22
  entry = session.new_entry(people.first, dependencies: deps)
23
23
  header = entry.to_hash.keys
24
24
 
25
- if options.dig(:nice_header)
25
+ if options.dig(:nice_header) || options.dig(:export, :options, :nice_header)
26
26
  name_maps = session.schema.fields_by_alt_id.transform_values do |fld|
27
27
  fld.name
28
28
  end.merge({
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class ToCsvDetailedCase < BaseCase
5
+ class ToCsvDetailedCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("to-csv-detailed", type: :export) do |people, session, options, usecase|
@@ -11,7 +11,7 @@ module Eco
11
11
  next false
12
12
  end
13
13
 
14
- unless file = options[:file]
14
+ unless file = options[:file] || options.dig(:export, :file, :name)
15
15
  session.logger.error("Destination file not specified")
16
16
  next false
17
17
  end
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class UpdateCase < BaseCase
5
+ class UpdateCase < DefaultCase
6
6
 
7
7
  def process
8
8
  #opts = CLI::Options.new do |p|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class UpdateDetailsCase < BaseCase
5
+ class UpdateDetailsCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("update-details", type: :sync) do |entries, people, session, options, usecase|
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class DefaultCases
5
- class UpsertCase < BaseCase
5
+ class UpsertCase < DefaultCase
6
6
 
7
7
  def process
8
8
  @cases.define("upsert", type: :sync) do |entries, people, session, options, usecase|