eco-helpers 1.5.9 → 1.5.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/CHANGELOG.md +58 -0
  4. data/eco-helpers.gemspec +3 -2
  5. data/lib/eco/api/common/people/default_parsers/csv_parser.rb +15 -2
  6. data/lib/eco/api/common/people/person_entry.rb +16 -4
  7. data/lib/eco/api/common/session/logger.rb +9 -1
  8. data/lib/eco/api/common/session/logger/cache.rb +91 -0
  9. data/lib/eco/api/common/session/logger/log.rb +48 -0
  10. data/lib/eco/api/microcases/set_core_with_supervisor.rb +5 -3
  11. data/lib/eco/api/organization/tag_tree.rb +7 -0
  12. data/lib/eco/api/policies.rb +1 -0
  13. data/lib/eco/api/policies/default_policies.rb +12 -0
  14. data/lib/eco/api/policies/default_policies/99_user_access_policy.rb +100 -0
  15. data/lib/eco/api/session.rb +13 -0
  16. data/lib/eco/api/session/batch/errors.rb +12 -2
  17. data/lib/eco/api/session/batch/job.rb +17 -7
  18. data/lib/eco/api/session/config/api.rb +1 -1
  19. data/lib/eco/version.rb +1 -1
  20. metadata +10 -17
  21. data/lib/eco/api/usecases/backup/append_usergroups_case.rb +0 -36
  22. data/lib/eco/api/usecases/backup/create_case.rb +0 -104
  23. data/lib/eco/api/usecases/backup/create_details_case.rb +0 -31
  24. data/lib/eco/api/usecases/backup/create_details_with_supervisor_case.rb +0 -48
  25. data/lib/eco/api/usecases/backup/hris_case.rb +0 -124
  26. data/lib/eco/api/usecases/backup/set_default_tag_case.rb +0 -49
  27. data/lib/eco/api/usecases/backup/set_supervisor_case.rb +0 -41
  28. data/lib/eco/api/usecases/backup/transfer_account_case.rb +0 -90
  29. data/lib/eco/api/usecases/backup/update_case.rb +0 -112
  30. data/lib/eco/api/usecases/backup/update_details_case.rb +0 -64
  31. data/lib/eco/api/usecases/backup/upsert_case.rb +0 -114
@@ -187,6 +187,8 @@ module Eco
187
187
  end
188
188
 
189
189
  # Does merge `Eco::API::UseCases::DefaultCases` with the custom cases.
190
+ # @note
191
+ # - the order matters, as a default usecase can be redefined by a custom one with same name
190
192
  # @return [Eco::API::UseCases]
191
193
  def usecases
192
194
  @usecases ||= config.usecases.dup.tap do |cases|
@@ -195,6 +197,17 @@ module Eco
195
197
  end
196
198
  end
197
199
 
200
+ # Does merge `Eco::API::Policies::DefaultPolicies` with the custom policies.
201
+ # @note
202
+ # - the default policies are added at the end (meaning they will run after the custom policies)
203
+ # @return [Eco::API::Policies]
204
+ def policies
205
+ @policies ||= config.policies.dup.tap do |policies|
206
+ default_policies = Eco::API::Policies::DefaultPolicies.new
207
+ policies.merge(default_policies)
208
+ end
209
+ end
210
+
198
211
  # Set of helpers to simplify your code
199
212
  # @see Eco::API::MicroCases
200
213
  # @return [Eco::API::MicroCases]
@@ -123,7 +123,7 @@ module Eco
123
123
  entry = queue.to_a[i]
124
124
  response = status[i]
125
125
  msg = "Error #{response.status}: #{response.body}\n"
126
- msg += "-- Failed to batch #{method} (entry #{i+1}). Person: #{person_ref(entry)}"
126
+ msg += "-- Failed to batch #{method}. Person: #{person_ref(entry)}"
127
127
  end
128
128
  msg
129
129
  end
@@ -164,7 +164,8 @@ module Eco
164
164
  private
165
165
 
166
166
  def person_ref(entry)
167
- "(id: '#{get_attr(entry, :id)}') '#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')"
167
+ row_str = (row = get_row(entry)) ? "(row: #{row}) " : nil
168
+ "#{row_str}(id: '#{get_attr(entry, :id)}') '#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')"
168
169
  end
169
170
 
170
171
  def get_attr(entry, attr)
@@ -175,6 +176,15 @@ module Eco
175
176
  end
176
177
  end
177
178
 
179
+ def get_row(value)
180
+ case value
181
+ when Eco::API::Common::People::PersonEntry
182
+ value.idx
183
+ when Ecoportal::API::V1::Person
184
+ get_row(value.entry)
185
+ end
186
+ end
187
+
178
188
  end
179
189
  end
180
190
  end
@@ -71,12 +71,6 @@ module Eco
71
71
  @subjobs ||= Eco::API::Session::Batch::Jobs.new(enviro, name: "childs-of:#{self.name}")
72
72
  end
73
73
 
74
- def subjobs_add(name = "ad-hoc:job-from:#{self.name}", usecase: self.usecase, &block)
75
- dup(name, usecase: usecase).tap do |subjob|
76
- subjobs.add(subjob, &block)
77
- end
78
- end
79
-
80
74
  # @return [Boolean] `true` if the current batch job is a result of an error_handler
81
75
  def error_handler?
82
76
  usecase? && usecase.is_a?(Eco::API::Error::Handler)
@@ -211,6 +205,7 @@ module Eco
211
205
  end
212
206
 
213
207
  msg << status.errors.message unless !status
208
+ msg << subjobs_summary
214
209
  end
215
210
  end.join("\n")
216
211
  end
@@ -238,7 +233,7 @@ module Eco
238
233
  # Applies the changes introduced by api policies
239
234
  def apply_policies(pre_queue)
240
235
  people(pre_queue).tap do |entries|
241
- policies = session.config.policies
236
+ policies = session.policies
242
237
  unless policies.empty? || options.dig(:skip, :api_policies)
243
238
  policies.launch(people: entries, session: session, options: options, job: self)
244
239
  end
@@ -307,6 +302,21 @@ module Eco
307
302
  file_manager.save_json(requests, file, :timestamp)
308
303
  end
309
304
 
305
+ # Adds a job tied to the current job
306
+ # Used with error handlers that need their own job to run
307
+ def subjobs_add(name = "ad-hoc:job-from:#{self.name}", usecase: self.usecase, &block)
308
+ dup(name, usecase: usecase).tap do |subjob|
309
+ subjobs.add(subjob, &block)
310
+ end
311
+ end
312
+
313
+ def subjobs_summary
314
+ return "" unless subjobs.count > 0
315
+ [].tap do |msg|
316
+ subjobs.map {|subjob| msg << subjob.summary}
317
+ end.join("\n")
318
+ end
319
+
310
320
  end
311
321
  end
312
322
  end
@@ -142,7 +142,7 @@ module Eco
142
142
  when :v1
143
143
  klass.new(external_key, host: host, logger: logger)
144
144
  when :v2
145
- klass.new(user_key: user_key, org_key: external_key, logger: logger)
145
+ klass.new(user_key: user_key, org_key: external_key, host: host, logger: logger)
146
146
  end.tap do |api|
147
147
  unless !api || log_connection?
148
148
  @logger.info("Created api#{self.version(version)} connection on '#{name}' enviro, pointing to '#{host}' in '#{mode}' mode")
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "1.5.9"
2
+ VERSION = "1.5.14"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eco-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.9
4
+ version: 1.5.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
@@ -116,7 +116,7 @@ dependencies:
116
116
  requirements:
117
117
  - - ">="
118
118
  - !ruby/object:Gem::Version
119
- version: 0.7.3
119
+ version: 0.7.4
120
120
  - - "<"
121
121
  - !ruby/object:Gem::Version
122
122
  version: '0.8'
@@ -126,7 +126,7 @@ dependencies:
126
126
  requirements:
127
127
  - - ">="
128
128
  - !ruby/object:Gem::Version
129
- version: 0.7.3
129
+ version: 0.7.4
130
130
  - - "<"
131
131
  - !ruby/object:Gem::Version
132
132
  version: '0.8'
@@ -136,7 +136,7 @@ dependencies:
136
136
  requirements:
137
137
  - - ">="
138
138
  - !ruby/object:Gem::Version
139
- version: 0.7.2
139
+ version: 0.7.3
140
140
  - - "<"
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0.8'
@@ -146,7 +146,7 @@ dependencies:
146
146
  requirements:
147
147
  - - ">="
148
148
  - !ruby/object:Gem::Version
149
- version: 0.7.2
149
+ version: 0.7.3
150
150
  - - "<"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0.8'
@@ -286,6 +286,8 @@ files:
286
286
  - lib/eco/api/common/session/environment.rb
287
287
  - lib/eco/api/common/session/file_manager.rb
288
288
  - lib/eco/api/common/session/logger.rb
289
+ - lib/eco/api/common/session/logger/cache.rb
290
+ - lib/eco/api/common/session/logger/log.rb
289
291
  - lib/eco/api/common/session/mailer.rb
290
292
  - lib/eco/api/common/session/s3_uploader.rb
291
293
  - lib/eco/api/common/session/sftp.rb
@@ -342,6 +344,8 @@ files:
342
344
  - lib/eco/api/organization/presets_values.json
343
345
  - lib/eco/api/organization/tag_tree.rb
344
346
  - lib/eco/api/policies.rb
347
+ - lib/eco/api/policies/default_policies.rb
348
+ - lib/eco/api/policies/default_policies/99_user_access_policy.rb
345
349
  - lib/eco/api/policies/policy.rb
346
350
  - lib/eco/api/session.rb
347
351
  - lib/eco/api/session/batch.rb
@@ -367,17 +371,6 @@ files:
367
371
  - lib/eco/api/session/config/sftp.rb
368
372
  - lib/eco/api/session/config/workflow.rb
369
373
  - lib/eco/api/usecases.rb
370
- - lib/eco/api/usecases/backup/append_usergroups_case.rb
371
- - lib/eco/api/usecases/backup/create_case.rb
372
- - lib/eco/api/usecases/backup/create_details_case.rb
373
- - lib/eco/api/usecases/backup/create_details_with_supervisor_case.rb
374
- - lib/eco/api/usecases/backup/hris_case.rb
375
- - lib/eco/api/usecases/backup/set_default_tag_case.rb
376
- - lib/eco/api/usecases/backup/set_supervisor_case.rb
377
- - lib/eco/api/usecases/backup/transfer_account_case.rb
378
- - lib/eco/api/usecases/backup/update_case.rb
379
- - lib/eco/api/usecases/backup/update_details_case.rb
380
- - lib/eco/api/usecases/backup/upsert_case.rb
381
374
  - lib/eco/api/usecases/base_case.rb
382
375
  - lib/eco/api/usecases/base_io.rb
383
376
  - lib/eco/api/usecases/default_cases.rb
@@ -474,7 +467,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
474
467
  requirements:
475
468
  - - ">="
476
469
  - !ruby/object:Gem::Version
477
- version: '0'
470
+ version: 2.4.4
478
471
  required_rubygems_version: !ruby/object:Gem::Requirement
479
472
  requirements:
480
473
  - - ">="
@@ -1,36 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class AppendUsergroupsCase < DefaultCase
6
-
7
- def process
8
- @cases.define("append-usergroups", type: :sync) do |entries, people, session, options, usecase|
9
- update = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account])
10
-
11
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
12
- pgs = session.policy_groups
13
-
14
- entries.each.with_index do |entry, i|
15
- if person = people.find(entry, strict: strict_search)
16
- if person.account
17
- ini_pg_ids = person.account&.policy_group_ids || []
18
- add = entry.internal_entry["policy_group_ids"].to_s.split("|").compact
19
- person.account.policy_group_ids = (ini_pg_ids | add).uniq
20
-
21
- person.account.permissions_custom = session.new_preset(person) unless options.dig(:exclude, :abilities)
22
- update.add(person)
23
- end
24
- else
25
- session.logger.error("This person does not exist: #{entry.to_s(:identify)}")
26
- end
27
- end
28
- end
29
-
30
- end
31
-
32
- end
33
- end
34
- end
35
- end
36
- end
@@ -1,104 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class CreateCase < DefaultCase
6
-
7
- def process
8
- @cases.define("create", type: :sync) do |entries, people, session, options, usecase|
9
- creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account])
10
- supers = session.job_group("post").new("supers", usecase: usecase, type: :update, sets: :core)
11
-
12
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
13
- pgs = session.policy_groups
14
-
15
- if session.config.people.default_usergroup?
16
- def_id = pgs.to_id(session.config.people.default_usergroup)
17
- end
18
-
19
- entries.each.with_index do |entry, i|
20
- if person = people.find(entry, strict: strict_search)
21
- session.logger.error("This person (id: '#{person.id}') already exists: #{entry.to_s(:identify)}")
22
- else
23
- person = session.new_person
24
- core_excluded = ["supervisor_id"]
25
-
26
- ini_tags = person.filter_tags || []
27
- entry.set_core(person, exclude: core_excluded)
28
-
29
- if session.tagtree
30
- person.filter_tags = session.tagtree.user_tags(
31
- initial: ini_tags,
32
- final: person.filter_tags,
33
- preserve_custom: true,
34
- add_custom: true
35
- )
36
- end
37
-
38
- entry.set_details(person) unless options.dig(:exclude, :details)
39
-
40
- unless options.dig(:exclude, :account)
41
- add_account = !person.account
42
- ini_pg_ids = person.account&.policy_group_ids || []
43
-
44
- account_excluded = []
45
- account_excluded.push("policy_group_ids") if options.dig(:exclude, :policy_groups) && !create
46
-
47
- entry.set_account(person, exclude: account_excluded)
48
-
49
- person.account&.send_invites = options[:send_invites] if options.key?(:send_invites)
50
-
51
- unless options.dig(:exclude, :policy_groups) && !create
52
- end_pg_ids = person.account.policy_group_ids || []
53
-
54
- if add_account && def_id && !entry.policy_group_ids?
55
- # on account creation, if missing policy_group_ids column in the input
56
- # use default_usergroup, if it's defined
57
- end_pg_ids = [def_id]
58
- end
59
-
60
- # avoid false updates by preserving the original order
61
- person.account.policy_group_ids = pgs.user_pg_ids(
62
- initial: ini_pg_ids,
63
- final: end_pg_ids
64
- )
65
- end
66
-
67
- person.account.permissions_custom = session.new_preset(person)
68
-
69
- if session.tagtree
70
- person.account.default_tag = session.tagtree.default_tag(*person.filter_tags)
71
- else
72
- tags = person.filter_tags || []
73
- person.account.default_tag = tags.first unless tags.length > 1
74
- end
75
- end
76
-
77
- creation.add(person)
78
-
79
- # set supervisor
80
- unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
81
- if !(sup_id = entry.supervisor_id)
82
- person.supervisor_id = nil
83
- else
84
- if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
85
- person.supervisor_id = supervisor.id
86
- else
87
- # delay setting supervisor if does not exit
88
- supers.add(person) do |person|
89
- person.supervisor_id = sup_id
90
- end
91
- end
92
- end
93
- end
94
-
95
- end
96
- end
97
- end
98
- end
99
-
100
- end
101
- end
102
- end
103
- end
104
- end
@@ -1,31 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class CreateDetailsCase < DefaultCase
6
-
7
- def process
8
- @cases.define("create-details", type: :sync) do |entries, people, session, options, usecase|
9
- creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details])
10
-
11
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
12
-
13
- entries.each.with_index do |entry, i|
14
- if person = people.find(entry, strict: strict_search)
15
- session.logger.error("This person (id: '#{person.id}') already exists: #{entry.to_s(:identify)}")
16
- else
17
- person = session.new_person
18
- entry.set_core(person, exclude: "supervisor_id")
19
- entry.set_details(person)
20
-
21
- creation.add(person)
22
- end
23
- end
24
- end
25
- end
26
-
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,48 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class CreateDetailsWithSupervisorCase < DefaultCase
6
-
7
- def process
8
- # good candidate to do @cases.case("create-details").use.chain(@cases.case("set-supervisor").use)
9
- @cases.define("create-details-with-supervisor", type: :sync) do |entries, people, session, options, usecase|
10
- creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details])
11
- supers = session.job_group("post").new("supers", usecase: usecase, type: :update, sets: :core)
12
-
13
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
14
-
15
- entries.each.with_index do |entry, i|
16
- if person = people.find(entry, strict: strict_search)
17
- session.logger.error("This person (id: '#{person.id}') already exists: #{entry.to_s(:identify)}")
18
- else
19
- person = session.new_person
20
- entry.set_core(person, exclude: "supervisor_id")
21
- entry.set_details(person)
22
-
23
- creation.add(person)
24
-
25
- # set supervisor
26
- if !(sup_id = entry.supervisor_id)
27
- person.supervisor_id = nil
28
- else
29
- if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
30
- person.supervisor_id = supervisor.id
31
- else
32
- # delay setting supervisor if does not exit
33
- supers.add(person) do |person|
34
- person.supervisor_id = sup_id
35
- end
36
- end
37
- end
38
-
39
- end
40
- end
41
- end
42
- end
43
-
44
- end
45
- end
46
- end
47
- end
48
- end
@@ -1,124 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class HrisCase < DefaultCase
6
-
7
- def process
8
- @cases.define("hris", type: :sync) do |entries, people, session, options, usecase|
9
- creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account])
10
- update = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account])
11
- supers = session.job_group("post").new("supers", usecase: usecase, type: :update, sets: :core)
12
- leavers = session.job_group("post").new("leavers", usecase: usecase, type: :update, sets: :account)
13
-
14
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
15
- pgs = session.policy_groups
16
-
17
- people.each_with_index do |person, i|
18
- if !entries.find(person, strict: strict_search)
19
- leavers.add(person) do |person|
20
- person.supervisor_id = nil
21
- person.account = nil if person.account
22
- end
23
- end
24
- end
25
-
26
-
27
- if session.config.people.default_usergroup?
28
- def_id = pgs.to_id(session.config.people.default_usergroup)
29
- end
30
-
31
- entries.each_with_index do |entry, i|
32
- person = people.find(entry, strict: strict_search)
33
- person = session.new_person if create = !person
34
-
35
- unless options.dig(:exclude, :core) && !create
36
- ini_tags = person.filter_tags || []
37
-
38
- core_attrs = ["name", "external_id", "email", "filter_tags"]
39
- core_excluded = core_attrs.map.select {|attr| options.dig(:exclude, attr.to_sym)}
40
- core_excluded.push("supervisor_id")
41
-
42
- entry.set_core(person, exclude: core_excluded)
43
- if session.tagtree && !options.dig(:exclude, :filter_tags)
44
- person.filter_tags = session.tagtree.user_tags(
45
- initial: ini_tags,
46
- final: person.filter_tags,
47
- preserve_custom: true,
48
- add_custom: true
49
- )
50
- end
51
- end
52
-
53
- entry.set_details(person) unless options.dig(:exclude, :details)
54
-
55
- unless options.dig(:exclude, :account)
56
- add_account = !person.account
57
- ini_pg_ids = person.account&.policy_group_ids || []
58
-
59
- account_excluded = []
60
- account_excluded.push("policy_group_ids") if options.dig(:exclude, :policy_groups) && !create
61
-
62
- entry.set_account(person, exclude: account_excluded)
63
-
64
- person.account.send_invites = options[:send_invites] if options.key?(:send_invites)
65
-
66
- unless options.dig(:exclude, :policy_groups) && !create
67
- end_pg_ids = person.account.policy_group_ids || []
68
-
69
- if add_account && def_id && !entry.policy_group_ids?
70
- # on account creation, if missing policy_group_ids column in the input
71
- # use default_usergroup, if it's defined
72
- end_pg_ids = [def_id]
73
- end
74
-
75
- # avoid false updates by preserving the original order
76
- person.account.policy_group_ids = pgs.user_pg_ids(
77
- initial: ini_pg_ids,
78
- final: end_pg_ids
79
- )
80
- end
81
-
82
- person.account.permissions_custom = session.new_preset(person) unless !create && options.dig(:exclude, :abilities)
83
-
84
- unless options.dig(:exclude, :filter_tags) || entry.default_tag?
85
- if session.tagtree
86
- person.account.default_tag = session.tagtree.default_tag(*person.filter_tags)
87
- else
88
- tags = person.filter_tags || []
89
- person.account.default_tag = tags.first unless tags.length > 1
90
- end
91
- end
92
- end
93
-
94
- creation.add(person) if create
95
- update.add(person) unless create
96
-
97
- # set supervisor
98
- unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
99
- if !(sup_id = entry.supervisor_id)
100
- person.supervisor_id = nil
101
- else
102
- if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
103
- person.supervisor_id = supervisor.id
104
- else
105
- # delay setting supervisor if does not exit
106
- supers.add(person) do |person|
107
- person.supervisor_id = sup_id
108
- end
109
- end
110
- end
111
- end
112
-
113
- end
114
-
115
- end
116
-
117
-
118
- end
119
-
120
- end
121
- end
122
- end
123
- end
124
- end