eco-helpers 2.0.49 → 2.0.52

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b4a2ad99ea2e78519b611cdcdb74743ae9dd1a8f5707164ebcc686c21d83700
4
- data.tar.gz: b9387499878102825b1396bf35394cc12c35c4b738380f3bfd52e2407c4239af
3
+ metadata.gz: c6424664c072321d2d1c45388fea06cfe16e8e42f4949e202be31555e8e052fa
4
+ data.tar.gz: 7b18ae74a24157949fbb80b26320b31312ef91c43eb73f1384c8c964da52e79c
5
5
  SHA512:
6
- metadata.gz: d574a346ba439207b85cfd1e265ed4b3c3f3742dd6308adfc8a873717bea8fd0f9ceaec49fcde1ea5575068c4e00395a4a200b42d624b45e707bd89d68b4560a
7
- data.tar.gz: 99a77ede34b322d1ebe6ad54822db841ae111e7063a4dbd6317dd092222a43919e8e059536db9f9c386bbbc31cd8043e64192f425a4fae055f3511ae6f622e67
6
+ metadata.gz: 6d1e57dc41b76a01bc80cf08108b4e50a456976dbe05bb33a4620cccb2ae745c76e88ce5af0c43795171ac758f6f55465835e22373a484967dd5a4f3ed57e318
7
+ data.tar.gz: 901007d2346ef608b67011e6f49f560c16a5cb6f0010b16fa24441bf2cfd06a8f8aa82dfd4ba78b86e50cd0aca340047173f6e80800f56340dad668bb27f1516
data/CHANGELOG.md CHANGED
@@ -1,15 +1,41 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [2.0.52] - 2022-02-xx
5
+
6
+ ### Added
7
+ - `Eco::API::UseCases::DefaultCases::CleanUnknownTags` include `default_tag` refresh
8
+
9
+ ### Changed
10
+ ### Fixed
11
+ - `Eco::Data::Files::Directory#dir_files` to be returned in alphabetic order
12
+
13
+ ## [2.0.51] - 2022-02-27
14
+
15
+ ### Added
16
+ - `Eco::API::MicroCases#person_update!` launches an update against the Server for one person
17
+ - `Eco::API::MicroCases#take_email_from_account` with given an account associated to an `email` that we cannot take, it associates that account to a different email
18
+
19
+ ## [2.0.50] - 2022-02-23
20
+
21
+ ### Added
22
+ - Ability to configure delay between jobs and job groups
23
+ - `Eco::API::Session::Batch::Jobs`, `#delay_between_jobs`
24
+ - `Eco::API::Session::Batch::JobsGroups`, `#delay_between_groups`
25
+ - `Eco::API::Session::Config`, added methods `#delay_between_jobs` and `#delay_between_job_groups`
26
+ - New use case `Eco::API::UseCases::DefaultCases::TagPaths` to create a `csv` with all `tag` paths
27
+ - Its purpose is to ease the build of tag remaps
28
+ - Ideally, after remapping the tags of an org, you would want to fix the tag paths based on the current tagtree
29
+
30
+ ### Changed
31
+ - `workflow`, when no operation specified it logs as `info`, rather than `warn`
32
+
4
33
  ## [2.0.49] - 2022-02-14
5
34
 
6
35
  ### Added
7
36
  - `Eco::API::Session::Batch::RequestStats#message` now it shows which details have changed
8
37
  - `Eco::API::UseCases::DefaultCases::ReinviteTransCase` the option `-force` will also send an invite to users that have accepted the invitation
9
38
 
10
- ### Changed
11
- ### Fixed
12
-
13
39
  ## [2.0.48] - 2022-02-04
14
40
 
15
41
  ### Changed
@@ -0,0 +1,39 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # It updates an idividual person.
5
+ # @note if it succeeds the update, it calls `person.consolidate!`
6
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
7
+ # @param context [String] main part of the message.
8
+ # @param reason [String] why are we updating now.
9
+ # @return [Boolean] `true` if it succeded to update, and `false` otherwise
10
+ def person_update!(person, context: "Session", reason: "")
11
+ logger.debug("#{context}, going to \"#{reason}\".\nPerson: #{person_ref(person)}")
12
+ if response = api.people.update(person)
13
+ if response.success?
14
+ person.consolidate!
15
+ true
16
+ else
17
+ msg = "#{context} Error #{response.status}: #{response.body}\n"
18
+ msg += " -- Failed to \"#{reason}\".\n"
19
+ msg += " • Person: #{person_ref(person)}"
20
+ logger.error(msg)
21
+ false
22
+ end
23
+ else
24
+ msg = "#{context} Error (connection error)\n"
25
+ msg += " -- Failed to \"#{reason}\".\n"
26
+ msg += " • Person: #{person_ref(person)}"
27
+ logger.error(msg)
28
+ false
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def person_ref(person)
35
+ Eco::API::Session::Batch::Feedback.person_ref(person)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,89 @@
1
+ module Eco
2
+ module API
3
+ class MicroCases
4
+ # Frees up `target_email` from an account not present in this org.
5
+ # Allows to force `target_email` on the current user's account.
6
+ # - If the person does not have account, this case will not do anything.
7
+ # - If `original_doc["account"]` is `nil` (no account on server side), this case will not do anything.
8
+ # - If the `target_email` and the `current_email` are the same or empty, this case will not do anything.
9
+ # @note
10
+ # - **It does not do the final update to the server to the `target_email`**. You will need to do this part yourself.
11
+ # - You would call this function only when you got an error of `email already taken`.
12
+ # - If the `target_email` is associated to a user in the same org, this will fail.
13
+ # @param person [Ecoportal::API::V1::Person] the person we want to update, carrying the changes to be done.
14
+ # @param dest_email [String, Proc] the email that we will move the other account to, when we free up `target_email`.
15
+ # @param target_email [String] the email that we want to free up from another account and bring to ours.
16
+ # If it's empty, the `person.email` will be used instead.
17
+ # @param options [Hash] the options.
18
+ # @param current_email [String] the email that the person's account is currently linked.
19
+ # As the current email should be associated with this person's account on server side, we use `original_doc["email"]`.
20
+ # @param context [String] main core part of logs. Provides context to the logs.
21
+ def take_email_from_account(person, dest_email:, target_email: nil, options: {}, context: "Session")
22
+ return false if options.dig(:exclude, :account)
23
+ return false unless account = person.account
24
+ return false unless had_account = person.original_doc["account"]
25
+
26
+ target_email ||= person.email
27
+ account_email = person.original_doc["email"]
28
+
29
+ return false unless target_email != account_email
30
+ return false if account_email.to_s.strip.empty?
31
+ return false if target_email.to_s.strip.empty?
32
+
33
+ if dest_email.is_a?(String)
34
+ return false unless target_email != dest_email
35
+ return false unless dest_email != account_email
36
+ return false if dest_email.to_s.strip.empty?
37
+ end
38
+
39
+ account_json = JSON.parse(account.to_json)
40
+ person.email = account_email
41
+
42
+ if success = _take_email_remove_account!(person, context: context)
43
+ if success = _take_email_acquire_account!(person, target_email, account: {}, context: context)
44
+ if success = _take_email_email_free_up!(person, dest_email: dest_email, context: context)
45
+ if success = _take_email_remove_account!(person, context: context)
46
+ if success = _take_email_acquire_account!(person, account_email, account: account_json, context: context)
47
+ person.email = target_email
48
+ end
49
+ end
50
+ else # free up target email
51
+ # restore
52
+ if _take_email_remove_account!(person, context: context)
53
+ _take_email_acquire_account!(person, account_email, account: account_json, context: context)
54
+ end
55
+ success = false
56
+ end
57
+ else # aquire other account
58
+ # restore
59
+ _take_email_acquire_account!(person, account_email, account: account_json, context: context)
60
+ success = false
61
+ end
62
+ end
63
+ success
64
+ end
65
+
66
+ private
67
+
68
+ def _take_email_acquire_account!(person, target_email, account: {}, context: "Session")
69
+ person.account = account
70
+ person.account.send_invites = false
71
+ person.email = target_email
72
+ micro.person_update!(person, reason: "bring account with email '#{target_email}'", context: context)
73
+ end
74
+
75
+ def _take_email_email_free_up!(person, dest_email:, target_email: nil, context: "Session")
76
+ target_email ||= person.email
77
+ person.email = dest_email.is_a?(Proc)? dest_email.call(target_email) : dest_email
78
+ reason = "free up email '#{target_email}', by moving account to '#{person.email}'"
79
+ micro.person_update!(person, reason: reason, context: context)
80
+ end
81
+
82
+ def _take_email_remove_account!(person, context: "Session")
83
+ person.account = nil
84
+ micro.person_update!(person, reason: "remove account", context: context)
85
+ end
86
+
87
+ end
88
+ end
89
+ end
@@ -19,6 +19,7 @@ require_relative 'microcases/people_cache'
19
19
  require_relative 'microcases/people_load'
20
20
  require_relative 'microcases/people_refresh'
21
21
  require_relative 'microcases/people_search'
22
+ require_relative 'microcases/person_update'
22
23
  require_relative 'microcases/preserve_filter_tags'
23
24
  require_relative 'microcases/preserve_default_tag'
24
25
  require_relative 'microcases/preserve_policy_groups'
@@ -26,9 +27,10 @@ require_relative 'microcases/set_account'
26
27
  require_relative 'microcases/set_core_with_supervisor'
27
28
  require_relative 'microcases/set_core'
28
29
  require_relative 'microcases/refresh_default_tag'
30
+ require_relative 'microcases/s3upload_targets'
29
31
  require_relative 'microcases/set_supervisor'
30
32
  require_relative 'microcases/strict_search'
31
- require_relative 'microcases/s3upload_targets'
33
+ require_relative 'microcases/take_email_from_account'
32
34
  require_relative 'microcases/with_each'
33
35
  require_relative 'microcases/with_each_leaver'
34
36
  require_relative 'microcases/with_each_present'
@@ -3,6 +3,8 @@ module Eco
3
3
  class Session
4
4
  class Batch
5
5
  class Jobs < API::Common::Session::BaseSession
6
+ DELAY_BETWEEN_JOBS = 0.3
7
+
6
8
  include Enumerable
7
9
  attr_reader :name
8
10
 
@@ -87,11 +89,12 @@ module Eco
87
89
  # - if there was post-launch callback for a `Batch::Job`, it calls it.
88
90
  # @return [Hash<Eco::API::Session::Batch::Job, Eco::API::Session::Batch::Status>]
89
91
  def launch(simulate: false)
90
- each do |job|
92
+ each_with_index do |job, idx|
91
93
  if job.pending?
92
94
  status[job] = job_status = job.launch(simulate: simulate)
93
95
  callback = @callbacks[job]
94
96
  callback.call(job, job_status) if callback
97
+ Eco::API::Session::Batch::JobsGroups.counter(delay_between_jobs) if !simulate && idx < self.length - 1
95
98
  end
96
99
  end
97
100
  launch(simulate: simulate) if pending?
@@ -125,6 +128,11 @@ module Eco
125
128
  end.join("\n")
126
129
  end
127
130
 
131
+ private
132
+
133
+ def delay_between_jobs
134
+ config.delay_between_jobs || DELAY_BETWEEN_JOBS
135
+ end
128
136
  end
129
137
  end
130
138
  end
@@ -93,10 +93,10 @@ module Eco
93
93
  status[group] = group_status = group.launch(simulate: simulate)
94
94
  callback = @callbacks[group]
95
95
  callback.call(group, group_status) if callback
96
- self.class.counter(DELAY_BETWEEN_GROUPS) if !simulate && idx < @order.length - 1
96
+ self.class.counter(delay_between_groups) if !simulate && idx < @order.length - 1
97
97
  end
98
98
  end
99
- launch(simulate: simulate) if pending?
99
+ launch(simulate: simulate) if pending?
100
100
  return status
101
101
  end
102
102
 
@@ -127,6 +127,11 @@ module Eco
127
127
  end.join("\n")
128
128
  end
129
129
 
130
+ private
131
+
132
+ def delay_between_groups
133
+ config.delay_between_job_groups || DELAY_BETWEEN_GROUPS
134
+ end
130
135
  end
131
136
  end
132
137
  end
@@ -363,6 +363,18 @@ module Eco
363
363
  yield(wf) if block_given?
364
364
  end
365
365
  end
366
+
367
+ # @return [nil, Interger] seconds between jobs
368
+ def delay_between_jobs(seconds = nil)
369
+ self["delay_between_jobs"] = seconds if seconds
370
+ self["delay_between_jobs"]
371
+ end
372
+
373
+ # @return [nil, Interger] seconds between job groups
374
+ def delay_between_job_groups(seconds = nil)
375
+ self["delay_between_job_groups"] = seconds if seconds
376
+ self["delay_between_job_groups"]
377
+ end
366
378
  # @!endgroup
367
379
 
368
380
  end
@@ -23,6 +23,12 @@ class Eco::API::UseCases::DefaultCases::CleanUnknownTags < Eco::API::Common::Loa
23
23
  unknown_tags = person.filter_tags.select {|tag| !tag?(tag)}
24
24
  person.filter_tags -= unknown_tags
25
25
  unknown_tag!(*unknown_tags)
26
+ if (account = person.account) && tag = account.default_tag
27
+ unless tag?(tag)
28
+ micro.refresh_default_tag(person.entry, person, options)
29
+ unknown_tag!(tag)
30
+ end
31
+ end
26
32
  update.add(person)
27
33
  end
28
34
  end
@@ -0,0 +1,25 @@
1
+ class Eco::API::UseCases::DefaultCases::TagPaths < Eco::API::Common::Loaders::UseCase
2
+ name "create-tag-paths"
3
+ type :other
4
+
5
+ def main(session, options, usecase)
6
+ CSV.open("tag_paths.csv", "w") do |csv|
7
+ csv << ["Tag", "Path"]
8
+ tag_paths.each do |values|
9
+ csv << values
10
+ end
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def tag_paths
17
+ @tag_paths ||= tagtree.tags.map do |tag|
18
+ [tag, tagtree.path(tag).join("|")]
19
+ end
20
+ end
21
+
22
+ def tagtree
23
+ session.tagtree
24
+ end
25
+ end
@@ -19,6 +19,7 @@ require_relative 'default_cases/codes_to_tags_case'
19
19
  require_relative 'default_cases/create_case'
20
20
  require_relative 'default_cases/create_details_case'
21
21
  require_relative 'default_cases/create_details_with_supervisor_case'
22
+ require_relative 'default_cases/create_tag_paths_case'
22
23
  require_relative 'default_cases/delete_trans_case'
23
24
  require_relative 'default_cases/delete_sync_case'
24
25
  require_relative 'default_cases/email_as_id_case'
@@ -87,6 +87,9 @@ ASSETS.cli.config do |cnf|
87
87
  options.deep_merge!(other: {file: {codes_column: col_codes}})
88
88
  end
89
89
 
90
+ desc = "Creates a CSV with the paths to each tag"
91
+ cases.add("-create-tag-paths", :other, desc, case_name: "create-tag-paths")
92
+
90
93
  desc = "Cleans from filter_tags those tags that are not present in the tagtree (as per tagtree.json file)."
91
94
  desc += " It will preserve standard register tags of most common registers (i.e. EVENT, RISK)."
92
95
  cases.add("-clean-unknown-tags", :transform, desc, case_name: "clean-unknown-tags")
@@ -80,7 +80,7 @@ ASSETS.cli.config do |config|
80
80
  wf.on(:usecases) do |wf_cases, io|
81
81
  unless config.usecases.process(io: io)
82
82
  msg = "No update operation specified... quitting"
83
- io.session.logger.warn(msg)
83
+ io.session.logger.info(msg)
84
84
  exit(1)
85
85
  end
86
86
  io
@@ -53,7 +53,7 @@ module Eco
53
53
 
54
54
  def dir_files(file: nil, pattern: dir_pattern)
55
55
  find = !!file ? file_pattern(file) : file_pattern(pattern)
56
- Dir.glob(find)
56
+ Dir.glob(find).sort
57
57
  end
58
58
 
59
59
  def newest_file(files_list = nil, file: nil)
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "2.0.49"
2
+ VERSION = "2.0.52"
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: 2.0.49
4
+ version: 2.0.52
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
@@ -462,6 +462,7 @@ files:
462
462
  - lib/eco/api/microcases/people_load.rb
463
463
  - lib/eco/api/microcases/people_refresh.rb
464
464
  - lib/eco/api/microcases/people_search.rb
465
+ - lib/eco/api/microcases/person_update.rb
465
466
  - lib/eco/api/microcases/preserve_default_tag.rb
466
467
  - lib/eco/api/microcases/preserve_filter_tags.rb
467
468
  - lib/eco/api/microcases/preserve_policy_groups.rb
@@ -472,6 +473,7 @@ files:
472
473
  - lib/eco/api/microcases/set_core_with_supervisor.rb
473
474
  - lib/eco/api/microcases/set_supervisor.rb
474
475
  - lib/eco/api/microcases/strict_search.rb
476
+ - lib/eco/api/microcases/take_email_from_account.rb
475
477
  - lib/eco/api/microcases/with_each.rb
476
478
  - lib/eco/api/microcases/with_each_leaver.rb
477
479
  - lib/eco/api/microcases/with_each_present.rb
@@ -530,6 +532,7 @@ files:
530
532
  - lib/eco/api/usecases/default_cases/create_case.rb
531
533
  - lib/eco/api/usecases/default_cases/create_details_case.rb
532
534
  - lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb
535
+ - lib/eco/api/usecases/default_cases/create_tag_paths_case.rb
533
536
  - lib/eco/api/usecases/default_cases/delete_sync_case.rb
534
537
  - lib/eco/api/usecases/default_cases/delete_trans_case.rb
535
538
  - lib/eco/api/usecases/default_cases/email_as_id_case.rb