eco-helpers 0.6.3 → 0.6.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/eco-helpers.gemspec +2 -0
- data/lib/eco/api/organization.rb +0 -1
- data/lib/eco/api/organization/people.rb +2 -4
- data/lib/eco/api/organization/tag_tree.rb +57 -4
- data/lib/eco/api/usecases/default_cases.rb +2 -0
- data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +57 -0
- data/lib/eco/version.rb +1 -1
- metadata +42 -4
- data/lib/eco/api/organization/account.rb +0 -23
- data/lib/eco/api/organization/presets_backup.rb +0 -220
- data/lib/eco/api/organization_old.rb +0 -73
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a9a891a431684fe13200bdd2b55d92c418fdd09
|
4
|
+
data.tar.gz: badc9350ac7b4f8b1061a7f7ab25393af5bace7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0df330e81d17f875c120dcd0abc87f142a478e91b186cf4f70a4634ce513ad69a6d16fd4e5eea9da17ef7b366c84b129758d38a001627134f488ba35181bfdc2
|
7
|
+
data.tar.gz: 7154d3081842a3efc237bdbd80e8a5fc310a079eee2e71861456b5e47fc64c6fedaa15e880d5a97a10be2fc50059896fc328e630702976ee9672c87d5c2d266a
|
data/eco-helpers.gemspec
CHANGED
@@ -23,6 +23,8 @@ Gem::Specification.new do |s|
|
|
23
23
|
s.require_paths = ["lib"]
|
24
24
|
|
25
25
|
s.add_development_dependency "rspec", "~> 3", ">= 3.8"
|
26
|
+
s.add_development_dependency "yard", "~> 0.9", ">= 0.9.18"
|
27
|
+
s.add_development_dependency "redcarpet", "~> 3.4", ">= 3.4.0"
|
26
28
|
|
27
29
|
s.add_dependency 'ecoportal-api', '~> 0.3', '>= 0.3.6'
|
28
30
|
s.add_dependency 'faker', '~> 1', '>= 1.9'
|
data/lib/eco/api/organization.rb
CHANGED
@@ -8,6 +8,5 @@ end
|
|
8
8
|
require_relative 'organization/tag_tree'
|
9
9
|
require_relative 'organization/presets'
|
10
10
|
require_relative 'organization/preferences'
|
11
|
-
require_relative 'organization/account'
|
12
11
|
require_relative 'organization/people'
|
13
12
|
require_relative 'organization/policy_groups'
|
@@ -9,10 +9,8 @@ module Eco
|
|
9
9
|
|
10
10
|
alias_method :people, :to_a
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
def initialize(people = [], klass: INTERNAL::Person, factory: nil)
|
15
|
-
@klass = INTERNAL::Person unless klass == EXTERNAL::Person
|
12
|
+
def initialize(people = [], klass: Ecoportal::API::Internal::Person, factory: nil)
|
13
|
+
@klass = Ecoportal::API::Internal::Person unless klass == Ecoportal::API::V1::Person
|
16
14
|
super(people, klass: @klass)
|
17
15
|
@caches_init = false
|
18
16
|
end
|
@@ -2,10 +2,21 @@ module Eco
|
|
2
2
|
module API
|
3
3
|
module Organization
|
4
4
|
|
5
|
+
# Provides helpers to deal with tagtrees.
|
5
6
|
class TagTree
|
6
7
|
attr_reader :tag, :nodes
|
7
8
|
attr_reader :depth, :path
|
8
9
|
|
10
|
+
# @example Node format:
|
11
|
+
# {"tag": "NODE NAME", "nodes": subtree}
|
12
|
+
# @example Tree/subtree format:
|
13
|
+
# [[Node], ...]
|
14
|
+
# @example Input format example:
|
15
|
+
# tree = [{"tag" => "AUSTRALIA", "nodes" => [
|
16
|
+
# {"tag" => "SYNDEY", "nodes" => []}
|
17
|
+
# ]}]
|
18
|
+
# tree = TagTree.new(tree.to_json)
|
19
|
+
# @param tagtree [String] representation of the tagtree in json.
|
9
20
|
def initialize(tagtree = [], depth: -1, path: [], enviro: nil)
|
10
21
|
case tagtree
|
11
22
|
when String
|
@@ -29,29 +40,68 @@ module Eco
|
|
29
40
|
init_hashes
|
30
41
|
end
|
31
42
|
|
43
|
+
# List of tag nodes of the entire subtree.
|
44
|
+
# @return [Array<String>]
|
32
45
|
def tags
|
33
46
|
@hash_tags.keys
|
34
47
|
end
|
35
48
|
|
49
|
+
# Verifies if a tag exists in the tree.
|
50
|
+
# @param key [String] tag to verify.
|
51
|
+
# @return [Boolean]
|
36
52
|
def tag?(key)
|
37
53
|
@hash_tags.key?(key.upcase)
|
38
54
|
end
|
39
55
|
|
56
|
+
# Finds a subtree node.
|
57
|
+
# @param key [String] parent node of subtree.
|
58
|
+
# @return [TagTree, nil] if the tag `key` is a node, returns that node.
|
40
59
|
def get_node(key)
|
41
60
|
return nil unless tag?(key)
|
42
61
|
@hash_tags[key.upcase]
|
43
62
|
end
|
44
63
|
|
64
|
+
# Filters tags out that do not belong to the tree
|
65
|
+
# @param list [Array<String>] source tags.
|
66
|
+
# @return [Array<String>]
|
45
67
|
def filter_tags(list)
|
46
68
|
return [] unless list && list.is_a?(Array)
|
47
69
|
list.select {|str| tag?(str)}
|
48
70
|
end
|
49
71
|
|
72
|
+
# Finds the path from a node `key` to the its root node in the tree.
|
73
|
+
# If `key` is not specified, returns the path from current node to root.
|
74
|
+
# @note the `path` is not relative to the subtree, but absolute to the entire tree.
|
75
|
+
# @param key [String] tag to find the path to.
|
76
|
+
# @return [Array<String>]
|
50
77
|
def path(key = nil)
|
51
78
|
return @path if !key
|
52
79
|
@hash_paths[key.upcase]
|
53
80
|
end
|
54
81
|
|
82
|
+
# Helper to assign tags to a person account.
|
83
|
+
# * It preserves the `:initial` order, in case the `:final` tags are the same
|
84
|
+
#
|
85
|
+
# @example Usage example:
|
86
|
+
# tree = [{"tag" => "Australia", "nodes" => [
|
87
|
+
# {"tag" => "SYNDEY", "nodes" => []},
|
88
|
+
# {"tag" => "MELMOURNE", "nodes" => []}
|
89
|
+
# ]}]
|
90
|
+
#
|
91
|
+
# tree = TagTree.new(tree.to_json)
|
92
|
+
# original = ["SYNDEY", "RISK"]
|
93
|
+
# final = ["MELMOURNE", "EVENT"]
|
94
|
+
#
|
95
|
+
# tree.user_tags(initial: original, final: final) # out: ["MELMOURNE", "RISK"]
|
96
|
+
# tree.user_tags(initial: original, final: final, preserve_custom: false) # out: ["MELMOURNE"]
|
97
|
+
# tree.user_tags(initial: original, final: final, add_custom: true) # out: ["MELMOURNE", "RISK", "EVENT"]
|
98
|
+
# tree.user_tags(initial: original, final: final, preserve_custom: false, add_custom: true) # out: ["MELMOURNE", "EVENT"]
|
99
|
+
#
|
100
|
+
# @param initial [Array<String>] original tags a person has in their account.
|
101
|
+
# @param final [Array<String>] target tags the person should have in their account afterwards.
|
102
|
+
# @param preserve_custom [Boolean] indicates if original tags that are not in the tree should be added/preserved.
|
103
|
+
# @param add_custom [Boolean] indicates if target tags that are not in the tree should be really added.
|
104
|
+
# @return [Array<String>] with the treated final tags.
|
55
105
|
def user_tags(initial: [], final: [], preserve_custom: true, add_custom: false)
|
56
106
|
initial = [initial].flatten.compact
|
57
107
|
final = [final].flatten.compact
|
@@ -64,10 +114,13 @@ module Eco
|
|
64
114
|
(initial & final) + new_tags
|
65
115
|
end
|
66
116
|
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
117
|
+
# Helper to decide which among the tags will be the default.
|
118
|
+
# * take the deepest tag (the one that is further down in the tree)
|
119
|
+
# * if there are different options (several nodes at the same depth):
|
120
|
+
# * take the common node between them (i.e. you have Hamilton and Auckland -> take New Zealand)
|
121
|
+
# * if there's no common node between them, take the `first` (unless they are at top level of the tree)
|
122
|
+
# @param [Array<String>] values list of tags.
|
123
|
+
# @return [String] default tag.
|
71
124
|
def default_tag(*values)
|
72
125
|
values = filter_tags(values)
|
73
126
|
nodes = []; depth = -1
|
@@ -16,6 +16,7 @@ module Eco
|
|
16
16
|
EmailAsIdCase.new(self).process
|
17
17
|
NewIdCase.new(self).process
|
18
18
|
NewEmailCase.new(self).process
|
19
|
+
SwitchSupervisorCase.new(self).process
|
19
20
|
SetSupervisorCase.new(self).process
|
20
21
|
UpdateDetailsCase.new(self).process
|
21
22
|
CreateDetailsCase.new(self).process
|
@@ -40,6 +41,7 @@ require_relative 'default_cases/email_as_id_case'
|
|
40
41
|
require_relative 'default_cases/new_id_case'
|
41
42
|
require_relative 'default_cases/new_email_case'
|
42
43
|
require_relative 'default_cases/set_supervisor_case'
|
44
|
+
require_relative 'default_cases/switch_supervisor_case'
|
43
45
|
require_relative 'default_cases/update_details_case'
|
44
46
|
require_relative 'default_cases/create_details_case'
|
45
47
|
require_relative 'default_cases/create_details_with_supervisor_case'
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Eco
|
2
|
+
module API
|
3
|
+
module UseCases
|
4
|
+
class DefaultCases
|
5
|
+
class SwitchSupervisorCase < UseCases::BaseCase
|
6
|
+
|
7
|
+
def process
|
8
|
+
@cases.define("switch-supervisor", type: :transform) do |people, session, options|
|
9
|
+
|
10
|
+
unless old_id = options.dig(:super, :old)
|
11
|
+
session.logger.error("You haven't specified the original supervisor. Aborting...")
|
12
|
+
exit
|
13
|
+
end
|
14
|
+
|
15
|
+
# we could be setting the supervisor to nil
|
16
|
+
unless options[:super].key?(:new)
|
17
|
+
session.logger.error("You haven't specified the new supervisor. Aborting...")
|
18
|
+
exit
|
19
|
+
end
|
20
|
+
|
21
|
+
pp old_id
|
22
|
+
pp new_id = options.dig(:super, :new)
|
23
|
+
|
24
|
+
unless old_sup = people.person(id: old_id, external_id: old_id, email: old_id)
|
25
|
+
session.logger.error("Couldn't find any user with that id: '#{old_id}'. Aborting...")
|
26
|
+
exit
|
27
|
+
end
|
28
|
+
|
29
|
+
unless new_sup = people.person(id: new_id, external_id: new_id, email: new_id)
|
30
|
+
session.logger.error("Couldn't find any user with that id: '#{new_id}'. Aborting...")
|
31
|
+
exit
|
32
|
+
end
|
33
|
+
|
34
|
+
people = people.supervisor_id(old_sup.id)
|
35
|
+
unless people.length > 0
|
36
|
+
session.logger.error("There are no people with supervisor #{old_sup.external_id} (#{old_sup.name} - #{old_sup.email}). Aborting...")
|
37
|
+
exit
|
38
|
+
end
|
39
|
+
|
40
|
+
session.logger.info("Going to change supervisor '#{old_sup.name}' (#{old_sup.external_id}) to '#{new_sup.name}' (#{new_sup.external_id})")
|
41
|
+
|
42
|
+
# create batch queue
|
43
|
+
supers = session.job_group("main").new("update", type: :update, sets: :core)
|
44
|
+
|
45
|
+
people.each.with_index do |person, i|
|
46
|
+
person.supervisor_id = new_sup.id
|
47
|
+
supers.add(person)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/eco/version.rb
CHANGED
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: 0.6.
|
4
|
+
version: 0.6.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oscar Segura
|
@@ -30,6 +30,46 @@ dependencies:
|
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '3.8'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: yard
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0.9'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 0.9.18
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0.9'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 0.9.18
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: redcarpet
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '3.4'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.4.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.4'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 3.4.0
|
33
73
|
- !ruby/object:Gem::Dependency
|
34
74
|
name: ecoportal-api
|
35
75
|
requirement: !ruby/object:Gem::Requirement
|
@@ -210,16 +250,13 @@ files:
|
|
210
250
|
- lib/eco/api/common/version_patches/external_person.rb
|
211
251
|
- lib/eco/api/eco_faker.rb
|
212
252
|
- lib/eco/api/organization.rb
|
213
|
-
- lib/eco/api/organization/account.rb
|
214
253
|
- lib/eco/api/organization/people.rb
|
215
254
|
- lib/eco/api/organization/policy_groups.rb
|
216
255
|
- lib/eco/api/organization/preferences.rb
|
217
256
|
- lib/eco/api/organization/preferences_reference.json
|
218
257
|
- lib/eco/api/organization/presets.rb
|
219
|
-
- lib/eco/api/organization/presets_backup.rb
|
220
258
|
- lib/eco/api/organization/presets_values.json
|
221
259
|
- lib/eco/api/organization/tag_tree.rb
|
222
|
-
- lib/eco/api/organization_old.rb
|
223
260
|
- lib/eco/api/session.rb
|
224
261
|
- lib/eco/api/session/batch.rb
|
225
262
|
- lib/eco/api/session/batch_job.rb
|
@@ -254,6 +291,7 @@ files:
|
|
254
291
|
- lib/eco/api/usecases/default_cases/reset_landing_page_case.rb
|
255
292
|
- lib/eco/api/usecases/default_cases/set_default_tag_case.rb
|
256
293
|
- lib/eco/api/usecases/default_cases/set_supervisor_case.rb
|
294
|
+
- lib/eco/api/usecases/default_cases/switch_supervisor_case.rb
|
257
295
|
- lib/eco/api/usecases/default_cases/to_csv_case.rb
|
258
296
|
- lib/eco/api/usecases/default_cases/update_details_case.rb
|
259
297
|
- lib/eco/api/usecases/default_cases/upsert_account_case.rb
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module Eco
|
2
|
-
module API
|
3
|
-
module Account
|
4
|
-
class Account
|
5
|
-
|
6
|
-
attr_accessor :policy_group_ids, :filter_tags
|
7
|
-
attr_accessor :permissions_preset, :permissions_custom
|
8
|
-
attr_accessor :login_provider_ids
|
9
|
-
attr_accessor :landing_page_id, :preferences, :starred_ids
|
10
|
-
attr_reader :prefilter
|
11
|
-
#attr_reader :presets
|
12
|
-
|
13
|
-
def initialize(init = {})
|
14
|
-
#@presets = AccountPresets.new
|
15
|
-
end
|
16
|
-
def set_details(person)
|
17
|
-
|
18
|
-
end
|
19
|
-
private
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,220 +0,0 @@
|
|
1
|
-
module Eco
|
2
|
-
module API
|
3
|
-
module Account
|
4
|
-
|
5
|
-
#TODO set rules, make Presets immutable, and init from config
|
6
|
-
class PresetsFactory
|
7
|
-
ABILITIES = File.join(__dir__, 'presets_values.json')
|
8
|
-
DEFAULT_CUSTOM = 'presets_custom.json'
|
9
|
-
DEFAULT_MAPS = 'presets_map.json'
|
10
|
-
|
11
|
-
def initialize(init = {}, policy_groups: [])
|
12
|
-
@abilities = JSON.load(File.open(ABILITIES))
|
13
|
-
|
14
|
-
init_custom(init)
|
15
|
-
init_maps(init)
|
16
|
-
|
17
|
-
@policy_groups = policy_groups
|
18
|
-
@policy_groups_by_id = policy_groups.map{ |pg| [pg.id, pg] }.to_h
|
19
|
-
@policy_groups_by_name = policy_groups.map { |pg| [pg.name&.downcase, pg] }.to_h
|
20
|
-
end
|
21
|
-
|
22
|
-
def new(*policy_group_ids_or_names)
|
23
|
-
names = policy_group_ids_or_names.map { |id_name| policy_group_name(id_name) }
|
24
|
-
preset_names = names.map { |name| @presets_map.fetch(name, nil) }
|
25
|
-
# validate
|
26
|
-
compile(*preset_names)
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def policy_group_name(id_name)
|
32
|
-
@policy_groups_by_id.fetch(id_name, nil)&.name&.downcase ||
|
33
|
-
@policy_groups_by_name.fetch(id_name&.downcase, nil)&.name&.downcase
|
34
|
-
end
|
35
|
-
|
36
|
-
def init_custom(init = {})
|
37
|
-
file = File.expand_path(init.fetch('presets_custom', DEFAULT_CUSTOM))
|
38
|
-
@presets_custom = JSON.load(File.open(file))
|
39
|
-
end
|
40
|
-
|
41
|
-
def init_maps(init = {})
|
42
|
-
file = File.expand_path(init.fetch('presets_map', DEFAULT_MAPS))
|
43
|
-
@presets_map = JSON.load(File.open(file))
|
44
|
-
end
|
45
|
-
|
46
|
-
def compile(*preset_names)
|
47
|
-
@presets_custom.values_at(*preset_names).reduce(empty_model) do |p1, p2|
|
48
|
-
merge(p1, p2)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def merge(preset1, preset2)
|
53
|
-
@abilities.map do |key, values|
|
54
|
-
idx = [
|
55
|
-
values.index(preset1[key]),
|
56
|
-
values.index(preset2[key])
|
57
|
-
].compact.max
|
58
|
-
[key, idx && values[idx]]
|
59
|
-
end.to_h
|
60
|
-
end
|
61
|
-
|
62
|
-
def empty_model
|
63
|
-
JSON.parse(@abilities.to_json).transform_values {|v| nil }
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
class Presets
|
69
|
-
DEFAULT_PRESET = "basic"
|
70
|
-
PRESETS_VALUES = File.join(__dir__, 'presets_values.json')
|
71
|
-
PRESETS_REFERENCE = File.join(__dir__, 'presets_reference.json')
|
72
|
-
|
73
|
-
attr_reader :default
|
74
|
-
attr_reader :presets_model, :presets_values, :presets_reference
|
75
|
-
attr_reader :native_presets, :featured_presets, :all_presets
|
76
|
-
attr_reader :rules
|
77
|
-
def initialize(init = {})
|
78
|
-
init = {} if !init || !init.is_a?(Hash)
|
79
|
-
|
80
|
-
@presets_values = JSON.load(File.open(PRESETS_VALUES))
|
81
|
-
# get the index of each value of each ability (so we can compare for highest and lowest)
|
82
|
-
@presets_values_hashed = JSON.parse(@presets_values.to_json) \
|
83
|
-
.transform_values {|v| Hash[v.map.with_index.to_a] }
|
84
|
-
|
85
|
-
@presets_model = self.empty_model
|
86
|
-
@presets_reference = init.fetch('reference', JSON.load(File.open(PRESETS_REFERENCE)))
|
87
|
-
@native_presets = ["read_only", "forms", "editor", "administrator", "custom"]
|
88
|
-
@all_presets = @presets_reference.keys
|
89
|
-
@featured_presets = @all_presets - @native_presets
|
90
|
-
@default = init.fetch("default", DEFAULT_PRESET)
|
91
|
-
|
92
|
-
self.set_rules(init.fetch('rules', nil))
|
93
|
-
end
|
94
|
-
|
95
|
-
def set_rules(rules)
|
96
|
-
@rules = rules
|
97
|
-
end
|
98
|
-
|
99
|
-
def empty_model
|
100
|
-
JSON.parse(@presets_values.to_json).transform_values {|v| nil }
|
101
|
-
end
|
102
|
-
|
103
|
-
def is_custom?(preset_name)
|
104
|
-
return (!preset_name || preset_name == "custom")
|
105
|
-
end
|
106
|
-
|
107
|
-
def find_preset(preset)
|
108
|
-
found = @all_presets.select { |ref|
|
109
|
-
preset_ref = @presets_reference.fetch(ref, nil)
|
110
|
-
presets_equal?(preset, preset_ref)
|
111
|
-
}
|
112
|
-
return found.switch()
|
113
|
-
end
|
114
|
-
|
115
|
-
# returns a new hash with presets defaulting to a preset profile
|
116
|
-
def new_model(default = nil)
|
117
|
-
presets_sample = @presets_reference.fetch(default, @presets_model)
|
118
|
-
JSON.parse(presets_sample.to_json)
|
119
|
-
end
|
120
|
-
|
121
|
-
def default_model
|
122
|
-
self.new_model(@default)
|
123
|
-
end
|
124
|
-
|
125
|
-
def get_models(presets_list = [])
|
126
|
-
presets_list.map {|v| self.new_model(v) }
|
127
|
-
end
|
128
|
-
|
129
|
-
def presets_equal?(preset1, preset2)
|
130
|
-
return false if !preset1 || !preset2
|
131
|
-
all_keys = self.new_model.keys
|
132
|
-
# "no access" can / should be nil; or not specified (notice that it is not so for updates)
|
133
|
-
return !all_keys.empty? && all_keys.all? { |key| preset1.fetch(key, nil) == preset2.fetch(key, nil) }
|
134
|
-
end
|
135
|
-
|
136
|
-
# among a set of presets, it returns the presets doc
|
137
|
-
# for each flag, it contains the highest (or lowest) of all the input presets
|
138
|
-
# by giving at least the abilities of the preset min: to each flag
|
139
|
-
# and / or the max: abilities one if specified
|
140
|
-
def merge (presets, highest: true, min: nil, max: nil)
|
141
|
-
max_preset_model = max ? self.new_model(max) : nil
|
142
|
-
min = self.default if min == "default"
|
143
|
-
|
144
|
-
return self.new_model(min).map do |k_flag, min_ability|
|
145
|
-
levels = presets.map {|pre| ability_level(k_flag, pre[k_flag]) }
|
146
|
-
|
147
|
-
min_lev = ability_level(k_flag, min_ability)
|
148
|
-
max_lev = Float::INFINITY
|
149
|
-
if max
|
150
|
-
max_ability = max_preset_model.fetch(k_flag, nil)
|
151
|
-
max_lev = ability_level(k_flag, max_ability)
|
152
|
-
end
|
153
|
-
if highest
|
154
|
-
level = highest(levels, flag: k_flag, min: min_lev, max: max_lev)
|
155
|
-
else
|
156
|
-
level = lowest(levels, flag: k_flag, min: min_lev, max: max_lev)
|
157
|
-
end
|
158
|
-
|
159
|
-
[k_flag, level_ability(k_flag, level)]
|
160
|
-
end.to_h
|
161
|
-
end
|
162
|
-
|
163
|
-
def self.test(num = 2, min: "random", max: "random")
|
164
|
-
account_presets = self.new
|
165
|
-
all_presets = account_presets.all_presets
|
166
|
-
|
167
|
-
num = rand(2..5) if num == "random"
|
168
|
-
list_types = all_presets.sample(num)
|
169
|
-
|
170
|
-
min = rand > 0.5 if min == "random"
|
171
|
-
max = rand > 0.5 if max == "random"
|
172
|
-
|
173
|
-
min = all_presets.sample(1).first if (!!min === min) && min
|
174
|
-
max = all_presets.sample(1).first if (!!max === max) && max
|
175
|
-
|
176
|
-
presets = account_presets.get_models(list_types)
|
177
|
-
|
178
|
-
puts "min: #{min}; max: #{max}" if min || max
|
179
|
-
puts "chosen presets: #{list_types}"
|
180
|
-
puts "pre-merge models: #{presets}"
|
181
|
-
pp account_presets.merge(presets) unless min || max
|
182
|
-
pp account_presets.merge(presets, min: min, max: max) if min && max
|
183
|
-
pp account_presets.merge(presets, min: min) if min && !max
|
184
|
-
pp account_presets.merge(presets, max: max) if !min && max
|
185
|
-
end
|
186
|
-
|
187
|
-
private
|
188
|
-
|
189
|
-
# given an array list of values for a flag
|
190
|
-
# it returns the highest of all the abilities
|
191
|
-
def highest(list, flag:, min: 0, max: Float::INFINITY)
|
192
|
-
m = (list + [min]).max
|
193
|
-
return (max < m)? max : m
|
194
|
-
end
|
195
|
-
|
196
|
-
# it returns the highest of all the values
|
197
|
-
def lowest(list, flag:, min: 0, max: Float::INFINITY)
|
198
|
-
m = (list + [max]).min
|
199
|
-
return (min > m)? min : m if min
|
200
|
-
return m
|
201
|
-
end
|
202
|
-
|
203
|
-
# given an ability of a flag
|
204
|
-
# it returns the level of that flag
|
205
|
-
def ability_level(flag, ability)
|
206
|
-
return 0 unless ability
|
207
|
-
return @presets_values_hashed.dig(flag, ability)
|
208
|
-
end
|
209
|
-
|
210
|
-
# given a level of one flag
|
211
|
-
# it returns the ability of that flag
|
212
|
-
def level_ability(flag, level)
|
213
|
-
return nil unless level && level > 0
|
214
|
-
return @presets_values[flag][level]
|
215
|
-
end
|
216
|
-
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
module Eco
|
2
|
-
module API
|
3
|
-
#TODO move as part of API::Session and inherit from < API::Common::Session::BaseModel
|
4
|
-
#TODO boost autoconfiguration (via file <- FileManager.dir)
|
5
|
-
class Organization
|
6
|
-
|
7
|
-
DUMMY_USER_EXTERNAL_ID = 'dummy'
|
8
|
-
|
9
|
-
attr_reader :tag_tree, :policy_groups, :person_schemas
|
10
|
-
attr_reader :default_person, :default_schema, :default_schema_id
|
11
|
-
attr_reader :default_policy_group_ids, :default_filter_tags, :default_login_provider_ids
|
12
|
-
attr_reader :login_providers
|
13
|
-
attr_reader :account_presets, :account_preferences
|
14
|
-
|
15
|
-
def initialize(init = {})
|
16
|
-
api = init.fetch('api', nil)
|
17
|
-
@@api = api&.instance_of?(Ecoportal::API::Internal) && api
|
18
|
-
# todo: maps { 'policy_group' : 'preset'}
|
19
|
-
set_settings(init, override: true)
|
20
|
-
end
|
21
|
-
|
22
|
-
def set_settings(init = {}, override: false)
|
23
|
-
set_defaults(init)
|
24
|
-
@login_providers = init['login_providers'] || @@api&.login_providers&.to_a || \
|
25
|
-
@login_providers || [] if (!@login_providers || :override)
|
26
|
-
@tag_tree = init['tag_tree'] || @default_filter_tags || @tag_tree || [] if (!@tag_tree || :override)
|
27
|
-
@policy_groups = init['policy_groups'] || @@api&.policy_groups&.to_a || \
|
28
|
-
@policy_groups || [] if (!@policy_groups || :override)
|
29
|
-
|
30
|
-
@person_schemas = init['person_schemas'] || @@api&.person_schemas&.to_a || @person_schemas || \
|
31
|
-
[@default_schema] || [] if (!@person_schemas || :override)
|
32
|
-
end
|
33
|
-
|
34
|
-
def default_schema=(value)
|
35
|
-
@default_schema = value || @person_schemas&.first
|
36
|
-
self.default_schema_id = @default_schema&.id || @default_schema&.schema_id || @default_schema_id
|
37
|
-
end
|
38
|
-
|
39
|
-
def default_schema_id=(value)
|
40
|
-
@default_schema_id = value
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def set_defaults(init = {})
|
46
|
-
@account_presets = Account::Presets.new(init.fetch('presets', nil))
|
47
|
-
@account_preferences = Account::Preferences.new
|
48
|
-
|
49
|
-
@default_person = get_default_person(init)
|
50
|
-
@default_policy_group_ids = init['default_policy_group_ids'] || @default_person&.account&.policy_group_ids || []
|
51
|
-
@default_filter_tags = init['default_filter_tags'] || @default_person&.account&.filter_tags || []
|
52
|
-
@default_login_provider_ids = init['default_login_provider_ids'] || @default_person&.account&.login_provider_ids || []
|
53
|
-
default_schema = get_schema_from_person(@default_person)
|
54
|
-
self.default_schema = init['default_schema'] || default_schema
|
55
|
-
end
|
56
|
-
|
57
|
-
def get_default_person(init = {})
|
58
|
-
return nil if !@@api
|
59
|
-
return init.fetch('default_person', (@@api && @@api.people.get(DUMMY_USER_EXTERNAL_ID)).result || nil)
|
60
|
-
end
|
61
|
-
|
62
|
-
def get_schema_from_person(person)
|
63
|
-
return nil if !person
|
64
|
-
doc = person.details.doc
|
65
|
-
doc['id'] = doc['schema_id']
|
66
|
-
doc.delete('schema_id')
|
67
|
-
schema = Ecoportal::API::Internal::PersonSchema.new(doc)
|
68
|
-
return schema
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|