eco-helpers 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +45 -2
  3. data/eco-helpers.gemspec +11 -10
  4. data/lib/eco/api.rb +1 -0
  5. data/lib/eco/api/common.rb +1 -1
  6. data/lib/eco/api/common/people/default_parsers.rb +4 -0
  7. data/lib/eco/api/common/people/default_parsers/freemium_parser.rb +30 -0
  8. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +42 -0
  9. data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +2 -1
  10. data/lib/eco/api/common/people/entry_factory.rb +8 -7
  11. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +2 -0
  12. data/lib/eco/api/common/people/person_parser.rb +1 -1
  13. data/lib/eco/api/common/session/base_session.rb +3 -6
  14. data/lib/eco/api/common/session/environment.rb +2 -23
  15. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +9 -1
  16. data/lib/eco/api/microcases.rb +31 -0
  17. data/lib/eco/api/microcases/core_excluded.rb +20 -0
  18. data/lib/eco/api/microcases/fix_filter_tags.rb +22 -0
  19. data/lib/eco/api/microcases/set_account.rb +32 -0
  20. data/lib/eco/api/microcases/set_core.rb +18 -0
  21. data/lib/eco/api/microcases/set_default_group.rb +33 -0
  22. data/lib/eco/api/microcases/set_default_tag.rb +23 -0
  23. data/lib/eco/api/microcases/set_supervisor.rb +26 -0
  24. data/lib/eco/api/microcases/strict_search.rb +14 -0
  25. data/lib/eco/api/organization/presets_values.json +1 -0
  26. data/lib/eco/api/session.rb +9 -6
  27. data/lib/eco/api/session/batch.rb +1 -44
  28. data/lib/eco/api/session/config.rb +2 -2
  29. data/lib/eco/api/session/config/api.rb +121 -7
  30. data/lib/eco/api/session/config/apis.rb +24 -25
  31. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +1 -0
  32. data/lib/eco/api/usecases/microed_cases/hris_case.rb +53 -0
  33. data/lib/eco/api/usecases/microed_cases/update_case.rb +33 -0
  34. data/lib/eco/api/usecases/microed_cases/update_details_case.rb +30 -0
  35. data/lib/eco/api/usecases/microed_cases/upsert_case.rb +36 -0
  36. data/lib/eco/version.rb +1 -1
  37. metadata +94 -53
@@ -0,0 +1,22 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ #self.define(:fix_filter_tags) do |entry, person, options|
5
+
6
+ # @param person [Person] the person we want to update, carrying the changes to be done.
7
+ # @param options [Hash] the options
8
+ def fix_filter_tags(person, options)
9
+ if session.tagtree
10
+ unless !person.new? && (options.dig(:exclude, :core) || options.dig(:exclude, :filter_tags))
11
+ person.filter_tags = session.tagtree.user_tags(
12
+ initial: person.original_doc["filter_tags"] || [],
13
+ final: person.filter_tags,
14
+ preserve_custom: true,
15
+ add_custom: true
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ #self.define("set_account") do |entry, person, session, options|
5
+
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.
8
+ # @param options [Hash] the options
9
+ def set_account(entry, person, options)
10
+ unless options.dig(:exclude, :account)
11
+ ini_pg_ids = person.account&.policy_group_ids || []
12
+
13
+ account_excluded = []
14
+ if options.dig(:exclude, :policy_groups) && !person.new?
15
+ account_excluded.push("policy_group_ids")
16
+ end
17
+
18
+ entry.set_account(person, exclude: account_excluded)
19
+
20
+ session.microcases.set_default_group(entry, person, session, options)
21
+
22
+ unless options.dig(:exclude, :abilities) && !person.new?
23
+ person.account.permissions_custom = session.new_preset(person)
24
+ end
25
+
26
+ session.microcases.set_default_tag(entry, person, session, options)
27
+ person.account.send_invites = options[:send_invites] if options.key?(:send_invites)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ #self.define("set_core") do |entry, person, session, options, people, supers|
5
+
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.
8
+ # @param options [Hash] the options
9
+ def set_account(entry, person, options, people, supers_job)
10
+ unless options.dig(:exclude, :core) && !person.new?
11
+ entry.set_core(person, exclude: micro.core_excluded(person, options))
12
+ micro.fix_filter_tags(person, options)
13
+ micro.set_supervisor(entry, person, options, people, supers_job)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ #self.define("set_default_group") do |entry, person, session, options|
5
+
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.
8
+ # @param options [Hash] the options
9
+ def set_default_group(entry, person, options)
10
+ unless options.dig(:exclude, :account)
11
+ unless options.dig(:exclude, :policy_groups) && !person.new?
12
+ end_pg_ids = person.account.policy_group_ids || []
13
+
14
+ if person.account_added? && def_usergroup_id && !entry.policy_group_ids?
15
+ # on account creation, if missing policy_group_ids column in the input
16
+ # use default_usergroup, if it's defined
17
+ end_pg_ids = [def_usergroup_id]
18
+ end
19
+
20
+ person.account.policy_group_ids = end_pg_ids
21
+ end
22
+ end
23
+ end
24
+
25
+ def def_usergroup_id
26
+ @def_usergroup_id ||= if session.config.people.default_usergroup?
27
+ session.policy_groups.to_id(session.config.people.default_usergroup)
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ #self.define("set_default_tag") do |entry, person, session, options|
5
+
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.
8
+ # @param options [Hash] the options
9
+ def set_default_tag(entry, person, options)
10
+ unless options.dig(:exclude, :account)
11
+ unless options.dig(:exclude, :filter_tags) || entry.default_tag?
12
+ if session.tagtree
13
+ person.account.default_tag = session.tagtree.default_tag(*person.filter_tags)
14
+ else
15
+ tags = person.filter_tags || []
16
+ person.account.default_tag = tags.first unless tags.length > 1
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module Eco
2
+ module API
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
8
+ 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)
13
+ person.supervisor_id = supervisor.id
14
+ else
15
+ # delay setting supervisor if does not exit
16
+ supers_job.add(person) do |person|
17
+ person.supervisor_id = sup_id
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ module Eco
2
+ module API
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
11
+ end
12
+ end
13
+ end
14
+ end
@@ -2,6 +2,7 @@
2
2
  "files": [null, "download", "upload", "browse", "administrate"],
3
3
  "data": [null, "view", "update", "administrate", "implement"],
4
4
  "reports": [null, "view", "edit", "administrate"],
5
+ "tasks": [null, "reassign_self", "reassign", "administrate"],
5
6
  "organization": [null, "view", "administrate", "implement"],
6
7
  "person_core": [null, "attach", "view_people_manager", "dashboard"],
7
8
  "person_core_create": [null, "create"],
@@ -86,19 +86,17 @@ module Eco
86
86
  schema = to_schema(schema) || self.schema
87
87
  return @entry_factories[schema&.id] if @entry_factories.key?(schema&.id)
88
88
 
89
- # TODO: check PersonEntry#to_internal and #to_external in init_attr_trackers
90
- # => when attr_map is avoided, it doesn't work as it should
91
89
  mappings = []
92
90
  if map_file = config.people.fields_mapper
93
91
  mappings = map_file ? file_manager.load_json(map_file) : []
94
92
  end
95
93
 
96
- @entry_factories[schema&.id] = Eco::API::Common::People::EntryFactory.new({
94
+ @entry_factories[schema&.id] = Eco::API::Common::People::EntryFactory.new(
95
+ enviro,
97
96
  schema: schema,
98
97
  person_parser: config.people.parser,
99
- attr_map: Eco::Data::Mapper.new(mappings),
100
- logger: logger
101
- })
98
+ attr_map: Eco::Data::Mapper.new(mappings)
99
+ )
102
100
  end
103
101
 
104
102
  # Helper to obtain a PersonFactory
@@ -197,6 +195,11 @@ module Eco
197
195
  end
198
196
  end
199
197
 
198
+ # shortcut to create a job of certain type within a group
199
+ def new_job(group, name, type, usecase, sets = [:core, :details, :account])
200
+ job_group(group).new(name, usecase: usecase, type: type, sets: sets)
201
+ end
202
+
200
203
  def jobs_launch(simulate: false)
201
204
  job_groups.launch(simulate: simulate)
202
205
  end
@@ -92,50 +92,7 @@ module Eco
92
92
  params = {per_page: DEFAULT_BATCH_BLOCK}.merge(params)
93
93
  client = people_api.client
94
94
 
95
- looping = !params.key?(:page)
96
- page = params[:page] || 1
97
-
98
- people = []; total_pages = nil
99
- cursor_id = nil
100
- loop do
101
- params.merge!(cursor_id: cursor_id) unless !cursor_id
102
- people_res, response = client_get(client, params: params.merge(page: page), silent: silent)
103
- people += people_res
104
-
105
- total_iterations ||= response.body["total_pages"]
106
- no_pages = !response.body["total_pages"]
107
- total_results ||= response.body["total_results"]
108
-
109
- if !total_iterations
110
- total_iterations ||= (total_results.to_f / params[:per_page]).ceil
111
- end
112
-
113
- msg = "iteration number: #{page}/#{total_iterations}, got num people #{people_res.length}, with total #{people.length} people got"
114
- msg = "search(q=#{params[:q]}) " + msg if params.key?(:q)
115
- logger.info(msg) unless silent
116
-
117
- iterate = (no_pages && cursor_id = response.body["cursor_id"]) || (looping && page < total_iterations)
118
- break unless iterate
119
- page += 1
120
- end
121
-
122
- return people
123
- end
124
-
125
- def client_get(client, params:, silent: false)
126
- response = client.get("/people", params: params)
127
- unless response.success?
128
- msg = "Request failed - params: #{params}"
129
- msg += "\n Error message: - Status #{response.status}: #{response.body}"
130
- fatal msg
131
- end
132
- people = []
133
- response.body["results"].each do |person_hash|
134
- person = Ecoportal::API::Internal::Person.new(person_hash)
135
- yield person if block_given?
136
- people.push(person)
137
- end
138
- [people, response]
95
+ return people_api.get_all(params: params, silent: silent)
139
96
  end
140
97
 
141
98
  def batch_from(data, method:, params: {}, silent: false)
@@ -116,8 +116,8 @@ module Eco
116
116
  apis.active_root_name
117
117
  end
118
118
 
119
- def api(logger = ::Logger.new(IO::NULL))
120
- apis.api(logger)
119
+ def api(logger = ::Logger.new(IO::NULL), version: nil)
120
+ apis.api(logger, version: version)
121
121
  end
122
122
 
123
123
  # FILES
@@ -4,19 +4,95 @@ module Eco
4
4
  class Config
5
5
  class Api < Hash
6
6
 
7
- def initialize(key:, host:, version:, mode: :local, root:)
7
+ class << self
8
+
9
+ def to_version(str)
10
+ case str.to_sym
11
+ when :external, :v1
12
+ :v1
13
+ when :v2, :oozes
14
+ :v2
15
+ else # :internal, :v0
16
+ :v0
17
+ end
18
+ end
19
+
20
+ def api_class(version = :v0)
21
+ case to_version(version)
22
+ when :v0
23
+ Ecoportal::API::Internal
24
+ when :v1
25
+ Ecoportal::API::V1
26
+ when :v2
27
+ Ecoportal::API::V2
28
+ else
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ def initialize(name, key:, host:, version:, mode: :local, root:, user_key: nil, external_key: nil)
8
35
  super(nil)
9
36
  @root = root
10
- self["key"] = key
11
- self["host"] = host
12
- self["version"] = version
13
- self["mode"] = mode
37
+ @apis = {}
38
+ self["name"] = name
39
+ self["key"] = key
40
+ self["host"] = host
41
+ self["version"] = version
42
+ self["mode"] = mode
43
+ self["user_key"] = user_key
44
+ self["external_key"] = external_key
45
+ end
46
+
47
+ def config
48
+ @root.config
49
+ end
50
+
51
+ def api(version: nil, logger: nil)
52
+ version = version ? version : self.version
53
+ @logger = logger if logger
54
+
55
+ if (current = get(version)) && self.logger.class == current.logger.class
56
+ return current
57
+ end
58
+
59
+ unless api_params?(version)
60
+ raise "The api configuration for '#{name}' is missing data for the api version '#{self.version(version)}'"
61
+ end
62
+
63
+ new_api(version).tap do |pi|
64
+ set(version, pi)
65
+ end
66
+ end
67
+
68
+ def set(version, api)
69
+ @apis[self.version(version)] = api
70
+ end
71
+
72
+ def get(version)
73
+ @apis[self.version(version)]
74
+ end
75
+
76
+ def name
77
+ self["name"]
14
78
  end
15
79
 
16
80
  def key
17
81
  self["key"]
18
82
  end
19
83
 
84
+ def user_key
85
+ self["user_key"] || @root.default_user_key
86
+ end
87
+
88
+ def external_key
89
+ self["external_key"] || ([:v1, :v2].include?(version) && key)
90
+ end
91
+
92
+ def internal_key
93
+ (version == :v0) && self["key"]
94
+ end
95
+
20
96
  def host
21
97
  self["host"]
22
98
  end
@@ -37,10 +113,48 @@ module Eco
37
113
  !local?
38
114
  end
39
115
 
40
- def version
41
- self["version"]
116
+ def version(value = nil)
117
+ self.class.to_version(value || self["version"])
42
118
  end
43
119
 
120
+ def logger
121
+ @logger ||= ::Logger.new(IO::NULL)
122
+ log_connection? ? @logger : ::Logger.new(IO::NULL)
123
+ end
124
+
125
+ private
126
+
127
+ def new_api(version)
128
+ klass = self.class.api_class(version)
129
+ case self.version(version)
130
+ when :v0
131
+ klass.new(internal_key, host: host, logger: logger)
132
+ when :v1
133
+ klass.new(external_key, host: host, logger: logger)
134
+ when :v2
135
+ klass.new(user_key: user_key, org_key: external_key, logger: logger)
136
+ end.tap do |pi|
137
+ unless log_connection? || !pi
138
+ @logger.info("Created api#{self.version(version)} connection on '#{name}' enviro, pointing to '#{host}' in '#{mode}' mode")
139
+ pi.logger.level = ::Logger::UNKNOWN
140
+ end
141
+ end
142
+ end
143
+
144
+ def api_params?(version)
145
+ case self.class.to_version(version)
146
+ when :v0
147
+ internal_key && host
148
+ when :v1
149
+ external_key && host
150
+ when :v2
151
+ external_key && user_key
152
+ end
153
+ end
154
+
155
+ def log_connection?
156
+ config.logger.log_connection?
157
+ end
44
158
 
45
159
  end
46
160
  end
@@ -26,17 +26,24 @@ module Eco
26
26
  end
27
27
  end
28
28
 
29
- def add(name, key:, host:, version: :internal, mode: :local)
29
+ def add(name, key:, host:, version: :internal, mode: :local, user_key: nil, external_key: nil)
30
30
  apis[name] = Session::Config::Api.new(
31
- key: key,
32
- host: host,
33
- version: version,
34
- mode: mode,
35
- root: self
31
+ name,
32
+ key: key,
33
+ host: host,
34
+ version: version,
35
+ mode: mode,
36
+ root: self,
37
+ user_key: user_key,
38
+ external_key: external_key
36
39
  )
37
40
  self
38
41
  end
39
42
 
43
+ def active_api
44
+ self["active-api"]
45
+ end
46
+
40
47
  def active_name=(name)
41
48
  raise "'#{name}' Api environment not defined" if !apis[name]
42
49
  self["active-name"] = name
@@ -53,34 +60,26 @@ module Eco
53
60
  active_name
54
61
  end
55
62
 
56
- def active_api
57
- self["active-api"]
58
- end
59
-
60
63
  def service_up?
61
- api_klass = (active_api.version == :external)? Ecoportal::API::External : Ecoportal::API::Internal
62
- @api_test ||= api_klass.new("foobar", host: active_api.host, logger: ::Logger.new(IO::NULL))
64
+ @api_test ||= Session::Config::Api.api_class(active_api.version).new("foobar", host: active_api.host, logger: ::Logger.new(IO::NULL))
63
65
  status = @api_test.client.get("/policy_groups").status
64
66
  # 401 Unauthorized "Permission denied. API key may be invalid."
65
67
  status == 401
66
68
  end
67
69
 
68
- def api(logger = ::Logger.new(IO::NULL), force_new: false)
69
- key = active_api.key
70
- host = active_api.host
71
- mode = active_api.mode
72
- version = active_api.version
73
- return @api if (!force_new) && @api && key == @key && host == @host && @mode == mode && version == @version
74
-
75
- api_klass = (version == :external)? Ecoportal::API::External : Ecoportal::API::Internal
70
+ def api(logger = ::Logger.new(IO::NULL), version: nil)
71
+ unless active_api
72
+ raise "There's no 'active_api'. Use apis.active_name='api_enviro_name' to set the active api"
73
+ end
74
+ active_api.api(version: version, logger: logger)
75
+ end
76
76
 
77
- @api = api_klass.new(key, host: host, logger: logger)
78
- @key = key; @host = host; @mode = mode; @version = version
79
- @api
77
+ def default_user_key=(key)
78
+ self["user_key"] = key
80
79
  end
81
80
 
82
- def new_api(logger = ::Logger.new(IO::NULL))
83
- api(logger, force_new: true)
81
+ def default_user_key
82
+ self["user_key"]
84
83
  end
85
84
 
86
85
  end