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,15 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ module Default
5
+ end
6
+ end
7
+ end
8
+ end
9
+
10
+ require_relative 'default/options'
11
+ require_relative 'default/input'
12
+ require_relative 'default/people'
13
+ require_relative 'default/filters'
14
+ require_relative 'default/usecases'
15
+ require_relative 'default/workflow'
@@ -0,0 +1,69 @@
1
+ ASSETS.cli.config do |cnf|
2
+ cnf.people_filters do |filters|
3
+
4
+ filters.add("-only-users") do |people, session, options|
5
+ options.deep_merge!(people: {filter: {account: true}})
6
+ people.users
7
+ end
8
+
9
+ filters.add("-no-account") do |people, session, options|
10
+ options.deep_merge!(people: {filter: {account: false}})
11
+ people.account_present(false)
12
+ end
13
+
14
+ filters.add("-with-details") do |people, session, options|
15
+ options.deep_merge!(people: {filter: {details: true}})
16
+ people.details_present
17
+ end
18
+
19
+ filters.add("-no-id") do |people, session, options|
20
+ options.deep_merge!(people: {filter: {external_id: nil}})
21
+ people.external_id_present(false)
22
+ end
23
+
24
+ filters.add("-supervisors") do |people, session, options|
25
+ options.deep_merge!(people: {filter: {subordinates: true}})
26
+ people.newFrom people.to_a.select {|person| person.subordinates > 0}
27
+ end
28
+
29
+ filters.add("-supervisor-id") do |people, session, options|
30
+ id = SCR.get_arg("-supervisor-id", with_param: true)
31
+ options.deep_merge!(people: {filter: {supervisor_id: id}})
32
+ unless sup = people.person(id: id, external_id: id, email: id, strict: true)
33
+ status = session.batch.search([{"id" => id}])
34
+ sup = status.people.first
35
+ end
36
+ raise "Supervisor with id '#{id}' does not exist" unless sup
37
+ people.supervisor_id(sup.id)
38
+ end
39
+
40
+ filters.add("-no-default-tag") do |people, session, options|
41
+ options.deep_merge!(people: {filter: {account: {default_tag: nil}}})
42
+ filtered = people.select do |person|
43
+ !person.account&.default_tag
44
+ end
45
+ people.newFrom filtered
46
+ end
47
+
48
+ filters.add("-schema-id") do |people, session, options|
49
+ sch_name = SCR.get_arg("-schema-id", with_param: true)
50
+ sch_id = session.schemas.to_id(sch_name)
51
+
52
+ unless sch_id
53
+ puts "You need to specify the schema id"
54
+ exit
55
+ end
56
+
57
+ options.deep_merge!(people: {filter: {details: {schema_id: sch_id}}})
58
+ session.logger.info("Filtering for schema #{sch_name}")
59
+
60
+ session.schema = sch_id
61
+
62
+ filtered = people.select do |person|
63
+ person.details && (person.details.schema_id == sch_id)
64
+ end
65
+ people.newFrom filtered
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,21 @@
1
+ ASSETS.cli.config do |cnf|
2
+ cnf.input(default_option: "-entries-from") do |session, str_opt, options|
3
+ input = []
4
+ if SCR.get_arg(str_opt)
5
+ file = SCR.get_file(str_opt, required: true)
6
+ options.deep_merge!(input: {file: {name: file}})
7
+ if SCR.get_arg("-xml")
8
+ options.deep_merge!(input: {file: {format: :xml}})
9
+ session.config.files.validate(:xml, file)
10
+ input = session.entries(file: file, format: :xml)
11
+ elsif SCR.get_arg("-json")
12
+ options.deep_merge!(input: {file: {format: :json}})
13
+ input = Eco::API::Organization::People.new(JSON.parse(File.read(file)))
14
+ else
15
+ options.deep_merge!(input: {file: {format: :csv}})
16
+ input = session.csv_entries(file)
17
+ end
18
+ end
19
+ input
20
+ end
21
+ end
@@ -0,0 +1,47 @@
1
+ ASSETS.cli.config do |cnf|
2
+ cnf.options_set do |options_set, options|
3
+
4
+ options_set.add(["-dry-run", "-simulate"]) do |options|
5
+ options[:dry_run] = true
6
+ options[:simulate] = true
7
+ end
8
+
9
+ options[:end_get] = true
10
+ options_set.add("-no-get") do |options|
11
+ options[:end_get] = false
12
+ end
13
+
14
+ options_set.add("-search-strict") do |options|
15
+ options.deep_merge!(search: {strict: true})
16
+ end
17
+ options_set.add("-search-soft") do |options|
18
+ options.deep_merge!(search: {strict: false})
19
+ end
20
+
21
+ options_set.add(["-no-invites", "-exclude-invites"]) do |options|
22
+ options.merge!(send_invites: false)
23
+ end
24
+ options_set.add("-send-invites") do |options|
25
+ options.merge!(send_invites: true)
26
+ end
27
+
28
+ options_set.add("-exclude-core") do |options|
29
+ options.deep_merge!(exclude: {core: true})
30
+ end
31
+ options_set.add("-exclude-supervisor") do |options|
32
+ options.deep_merge!(exclude: {supervisor: true})
33
+ end
34
+ options_set.add("-exclude-email") do |options|
35
+ options.deep_merge!(exclude: {email: true})
36
+ end
37
+ options_set.add("-exclude-details") do |options|
38
+ options.deep_merge!(exclude: {details: true})
39
+ end
40
+ options_set.add("-exclude-account") do |options|
41
+ options.deep_merge!(exclude: {account: true})
42
+ end
43
+ options_set.add("-exclude-abilities") do |options|
44
+ options.deep_merge!(exclude: {abilities: true})
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,39 @@
1
+ ASSETS.cli.config do |cnf|
2
+ cnf.people do |input, session, options|
3
+ people = Eco::API::Organization::People.new([])
4
+ if SCR.get_arg("-get-people")
5
+ options.deep_merge!(people: {
6
+ get: {from: :remote, type: :full}
7
+ })
8
+ people = session.do.file_people
9
+ elsif SCR.get_arg("-people-from-backup")
10
+ file = SCR.get_arg("-people-from-backup", with_param: true)
11
+ options.deep_merge!(people: {
12
+ get: {from: :local, type: :backup}
13
+ })
14
+ people = JSON.parse(File.read(file))
15
+ people = Eco::API::Organization::People.new(people)
16
+ elsif SCR.get_arg("-get-partial")
17
+ unless input && input.is_a?(Enumerable)
18
+ raise "To use -get-partial (partial updates), you need to use -entries-from"
19
+ end
20
+ options.deep_merge!(people: {
21
+ get: {from: :remote, type: :partial}
22
+ })
23
+
24
+ people = session.do.search(input, options: options)
25
+ else
26
+ options.deep_merge!(people: {
27
+ get: {from: :local, type: :full}
28
+ })
29
+ people = session.do.load_people(modifier: [:newest, :save])
30
+ if people.empty?
31
+ options.deep_merge!(people: {
32
+ get: {from: :remote, type: :full}
33
+ })
34
+ people = session.do.file_people if people.empty?
35
+ end
36
+ end
37
+ people
38
+ end
39
+ end
@@ -0,0 +1,63 @@
1
+ ASSETS.cli.config do |cnf|
2
+ cnf.usecases do |cases|
3
+
4
+ cases.add("-people-to-csv", :export) do |people, session, options|
5
+ file = SCR.get_file("-people-to-csv", required: true, should_exist: false)
6
+ options.deep_merge!(export: {file: {name: file, format: :csv}})
7
+ options.deep_merge!(export: {options: {nice_header: true}}) if SCR.get_arg("-nice-header")
8
+
9
+ case_name = SCR.get_arg("-detailed")? "to-csv-detailed" : "to-csv"
10
+ session.usecases.case(case_name)
11
+ end
12
+
13
+ cases.add("-reset-landing-page", :transform, case_name: "reset-landing-page")
14
+ cases.add("-email-as-id", :transform, case_name: "email-as-id")
15
+ cases.add("-new-id-from", :sync, case_name: "new-id")
16
+ # won't succeed if some other user with that email in all eP
17
+ cases.add("-new-email-from", :sync, case_name: "new-email")
18
+ # forces the change of email no matter what,
19
+ # unless some other user with that email in the same organization
20
+ cases.add("-change-email-from", :sync, case_name: "change-email")
21
+ cases.add("-set-supervisor-from", :sync, case_name: "set-supervisor")
22
+ cases.add("-switch-supervisor", :transform, case_name: "switch-supervisor") do |people, session, options|
23
+ unless old_id = SCR.get_arg("-old-super", with_param: true)
24
+ session.logger.error("You must specify an -old-super to target whose supervisor is changing")
25
+ exit
26
+ end
27
+ options.deep_merge!(super: {old: old_id})
28
+
29
+ unless new_id = SCR.get_arg("-new-super", with_param: true)
30
+ session.logger.error("You must specify the -new-super id. To reset to nil the supervisor, please, specify nil.")
31
+ exit
32
+ end
33
+ new_id = new_id == "nil"? nil : new_id
34
+ options.deep_merge!(super: {new: new_id})
35
+ end
36
+
37
+ cases.add("-create-details-from", :sync, case_name: "create-details")
38
+ cases.add("-update-details-from", :sync, case_name: "update-details")
39
+ cases.add("-reinvite", :transform, case_name: "reinvite")
40
+ cases.add("-set-default-tag", :transform, case_name: "set-default-tag")
41
+
42
+ cases.add("-recover-db-from", :sync, case_name: "recover-db") do |input, people, session, options|
43
+ unless input && input.is_a?(Eco::API::Organization::People)
44
+ file = SCR.get_file("-recover-db-from", required: true)
45
+ input = Eco::API::Organization::People.new(JSON.parse(File.read(file)))
46
+ session.logger.info("Source DB: loaded #{input.length} entries.")
47
+ end
48
+
49
+ options.deep_merge!(include: {delete: true}) if SCR.get_arg("-include-delete")
50
+ options.deep_merge!(include: {create: true}) if SCR.get_arg("-include-create")
51
+ end
52
+
53
+ cases.add("-create-from", :sync, case_name: "create")
54
+ cases.add("-update-from", :sync, case_name: "update")
55
+ cases.add("-refresh-presets", :transform, case_name: "refresh-presets")
56
+ cases.add("-remove-account", :transform, case_name: "remove-account")
57
+ cases.add("-remove-account-from", :sync, case_name: "remove-account")
58
+ cases.add("-delete", :transform, case_name: "delete")
59
+ cases.add("-delete-from", :sync, case_name: "delete")
60
+ cases.add("-upsert-from", :sync, case_name: "upsert")
61
+ cases.add("-hris-from", :sync, case_name: "hris")
62
+ end
63
+ end
@@ -0,0 +1,86 @@
1
+ ASSETS.cli.config do |config|
2
+ ASSETS.config.workflow do |wf|
3
+
4
+ wf.on(:load) do |wf_load, io|
5
+ io = io.new(input: config.input.get(io: io))
6
+ io = io.new(people: config.people(io: io))
7
+ io = io.new(people: config.people_filters.process(io: io))
8
+ end
9
+
10
+ wf.before(:usecases) do |wf_cases, io|
11
+ # save partial entries -> should be native to session.workflow
12
+ partial_update = io.options.dig(:people, :get, :type) == :partial
13
+ if !io.options[:dry_run] && partial_update
14
+ #enviro.config.people.cache
15
+ io.session.file_manager.save_json(io.people, "cache/people_partial.json", :timestamp)
16
+ end
17
+ io
18
+ end
19
+
20
+ wf.on(:usecases) do |wf_cases, io|
21
+ unless config.usecases.process(io: io)
22
+ io.session.logger.warn("No update operation specified... quitting")
23
+ exit
24
+ end
25
+ io
26
+ end
27
+
28
+ wf.before(:launch_jobs) do |wf_jobs, io|
29
+ SCR.stop_on_unknown!
30
+ io
31
+ end
32
+
33
+ wf.on(:launch_jobs) do |wf_jobs, io|
34
+ io.session.jobs_launch(simulate: io.options[:dry_run])
35
+ io
36
+ end
37
+
38
+ wf.before(:post_launch) do |wf_post, io|
39
+ if io.session.post_launch.empty?
40
+ wf_post.skip!
41
+ else
42
+ partial_update = io.options.dig(:people, :get, :type) == :partial
43
+ if !io.options[:dry_run] && partial_update
44
+ # get target people afresh
45
+ people = io.session.do.people_refresh(people: io.people, include_created: true)
46
+ io = io.new(people: people)
47
+ else
48
+ wf_post.skip!
49
+ msg = "Although there are post_launch cases, they will NOT be RUN"
50
+ if io.options[:dry_run]
51
+ msg+= ", because we are in dry-run (simulate)."
52
+ elsif !partial_update
53
+ msg+= ", because it is not a partial update (-get-partial)"
54
+ end
55
+ io.session.logger.info(msg)
56
+ end
57
+ end
58
+ io
59
+ end
60
+
61
+ wf.for(:post_launch) do |wf_post|
62
+
63
+ wf_post.on(:usecases) do |wf_postcases, io|
64
+ io.session.post_launch.each do |use|
65
+ io = use.launch(io: io).base
66
+ end
67
+ io
68
+ end
69
+
70
+ wf_post.on(:launch_jobs) do |wf_postlaunch, io|
71
+ io.session.jobs_launch(simulate: io.options[:dry_run])
72
+ io
73
+ end
74
+ end
75
+
76
+ wf.on(:end) do |wf_end, io|
77
+ partial_update = io.options.dig(:people, :get, :type) == :partial
78
+ unless !io.options[:end_get] || io.options[:dry_run] || partial_update
79
+ people = io.session.do.file_people
80
+ io = io.new(people: people)
81
+ end
82
+ io
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,40 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ class Input
5
+
6
+ attr_reader :core_config
7
+ attr_reader :default_option
8
+
9
+ def initialize(core_config:, default_option: nil)
10
+ @core_config = core_config
11
+ @default_option = default_option
12
+ @callbacks = {}
13
+ end
14
+
15
+ def default_option?
16
+ !!default_option
17
+ end
18
+
19
+ def define(option: nil, &block)
20
+ option ||= default_option
21
+ raise "Missing option to identify the input (no default defined)" unless !!option
22
+ raise "Missing block to define the input obtention process" unless block
23
+ @callbacks[option] = block
24
+ end
25
+
26
+ def get(io:, option: nil)
27
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
28
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
29
+ end
30
+
31
+ option ||= default_option
32
+ raise "Missing option to identify the input (no default defined)" unless !!option
33
+ callback = @callbacks[option] || @callbacks[default_option]
34
+ callback.call(io.session, option, io.options)
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ class OptionsSet
5
+ attr_reader :core_config
6
+
7
+ def initialize(core_config:)
8
+ @core_config = core_config
9
+ @options_set = {}
10
+ end
11
+
12
+ def add(option)
13
+ raise "Missing block to define the options builder" unless block_given?
14
+ callback = Proc.new
15
+ [option].flatten.compact.each do |opt|
16
+ @options_set[opt] = callback
17
+ end
18
+ self
19
+ end
20
+
21
+ def process(io:)
22
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
23
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
24
+ end
25
+
26
+ @options_set.each do |arg, callback|
27
+ callback.call(io.options) if SCR.get_arg(arg)
28
+ end
29
+ io.options
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ class PeopleFilters
5
+ attr_reader :core_config
6
+
7
+ def initialize(core_config:)
8
+ @core_config = core_config
9
+ @filters = {}
10
+ end
11
+
12
+ def add(option)
13
+ raise "Missing block to define the filters builder" unless block_given?
14
+ callback = Proc.new
15
+ [option].flatten.compact.each do |opt|
16
+ @filters[opt] = callback
17
+ end
18
+ self
19
+ end
20
+
21
+ def process(io:)
22
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
23
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
24
+ end
25
+
26
+ io = io.new(type: :transform)
27
+ @filters.each do |arg, callback|
28
+ if SCR.get_arg(arg)
29
+ io = io.new(people: callback.call(*io.params))
30
+ end
31
+ end
32
+ io.people
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end