eco-helpers 1.5.13 → 1.5.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,41 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class SetSupervisorCase < DefaultCase
6
-
7
- def process
8
- @cases.define("set-supervisor", type: :sync) do |entries, people, session, options, usecase|
9
- job = session.job_group("main").new("update", usecase: usecase, type: :update, sets: :core)
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
- person = people.find(entry, strict: strict_search)
15
-
16
- if !person
17
- session.logger.error("This person does not exist: #{entry.to_s(:identify)}")
18
- else
19
- sup_id = entry.supervisor_id
20
- supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
21
-
22
- if !supervisor
23
- if entry.supervisor_id
24
- msg = "Entry(#{i}) - supervisor id #{entry.supervisor_id} does not exist for person: #{entry.to_s(:identify)}"
25
- session.logger.warn(msg)
26
- end
27
- else
28
- # set internal id of the supervisor (detect if actually changed)
29
- person.supervisor_id = supervisor.id
30
- job.add(person)
31
- end
32
- end
33
- end
34
- end
35
- end
36
-
37
- end
38
- end
39
- end
40
- end
41
- end
@@ -1,90 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class TransferAccountCase < DefaultCase
6
-
7
- def process
8
- @cases.define("transfer-account", type: :sync) do |entries, people, session, options, usecase|
9
- remove_account = session.job_group("main").new("remove account", usecase: usecase, type: :update, sets: :account)
10
- add_account = session.job_group("post").new("add account", usecase: usecase, type: :update, sets: :account)
11
-
12
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
13
-
14
- done = []; pending = []
15
- pairs = entries.each_with_object([]) do |source|
16
- entry_hash = source.internal_entry
17
- unless entry_hash.key?("destination-id")
18
- session.logger.error("You haven't defined a column 'destination-id' to whom the account should be transferred")
19
- exit(1)
20
- end
21
- if peer_id = entry_hash["destination-id"]
22
- if peer = entries.entry(id: peer_id, external_id: peer_id)
23
- if done.include?(peer)
24
- session.logger.error("You paired '#{peer_id}' twice. A person can only receive account from 1 user")
25
- exit(1)
26
- end
27
- pending.delete(source)
28
- pending.delete(peer)
29
- done.push(source).push(pair)
30
- [source, peer]
31
- else
32
- pending.push(source)
33
- nil
34
- end
35
- else
36
- pending.push(source)
37
- end
38
- end.compact
39
-
40
- # Data input integrity check
41
- unless pending.empty?
42
- msg = "You haven't defined a pair for the following ids:"
43
- msg += pending.each_with_object("") do |entry, str|
44
- str << "\n#{entry.id || entry.external_id}"
45
- end
46
- session.logger.error(msg)
47
- exit(1)
48
- end
49
-
50
- pairs.each do |pair|
51
- src_entry, dst_entry = pair
52
- unless src_person = people.find(src_entry, strict: strict_search)
53
- session.logger.error("This person does not exist: #{src_entry.to_s(:identify)}")
54
- exit(1)
55
- end
56
-
57
- unless dst_person = people.find(dst_entry, strict: strict_search)
58
- session.logger.error("This person does not exist: #{dst_person.to_s(:identify)}")
59
- exit(1)
60
- end
61
-
62
- unless account_doc = src_person.account&.doc
63
- session.logger.error("You are trying to move account from a person that doesn't have: #{src_person.id | src_person.external_id}")
64
- exit(1)
65
- end
66
-
67
- if dst_person.email.to_s.strip.empty?
68
- session.logger.error("A person you are trying to add account doesn't have email: #{dst_person.id | dst_person.external_id}")
69
- exit(1)
70
- end
71
-
72
- src_person.account = nil
73
- remove_account.add(src_person)
74
- add_account.add(dst_person) do |person|
75
- # only if we got to remove the account of the original person
76
- if account_doc && src_person.as_update == {}
77
- person.account = account_doc
78
- end
79
- end
80
-
81
- end
82
-
83
- end
84
- end
85
-
86
- end
87
- end
88
- end
89
- end
90
- end
@@ -1,112 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class UpdateCase < DefaultCase
6
-
7
- def process
8
- @cases.define("update", 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
- 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
-
22
- unless options.dig(:exclude, :core)
23
- ini_tags = person.filter_tags || []
24
-
25
- core_attrs = ["name", "external_id", "email", "filter_tags"]
26
- core_excluded = core_attrs.map.select {|attr| options.dig(:exclude, attr.to_sym)}
27
- core_excluded.push("supervisor_id")
28
-
29
- entry.set_core(person, exclude: core_excluded)
30
-
31
- if session.tagtree && !options.dig(:exclude, :filter_tags)
32
- person.filter_tags = session.tagtree.user_tags(
33
- initial: ini_tags,
34
- final: person.filter_tags,
35
- preserve_custom: true,
36
- add_custom: true
37
- )
38
- end
39
- end
40
-
41
- entry.set_details(person) unless options.dig(:exclude, :details)
42
-
43
- unless options.dig(:exclude, :account)
44
- add_account = !person.account
45
- ini_pg_ids = person.account&.policy_group_ids || []
46
-
47
- account_excluded = []
48
- account_excluded.push("policy_group_ids") if options.dig(:exclude, :policy_groups)
49
-
50
- entry.set_account(person, exclude: account_excluded)
51
-
52
- person.account.send_invites = options[:send_invites] if options.key?(:send_invites)
53
-
54
- unless options.dig(:exclude, :policy_groups) && !create
55
- end_pg_ids = person.account.policy_group_ids || []
56
-
57
- if add_account && def_id && !entry.policy_group_ids?
58
- # on account creation, if missing policy_group_ids column in the input
59
- # use default_usergroup, if it's defined
60
- end_pg_ids = [def_id]
61
- end
62
-
63
- # avoid false updates by preserving the original order
64
- person.account.policy_group_ids = pgs.user_pg_ids(
65
- initial: ini_pg_ids,
66
- final: end_pg_ids
67
- )
68
- end
69
-
70
- person.account.permissions_custom = session.new_preset(person) unless options.dig(:exclude, :abilities)
71
-
72
- unless options.dig(:exclude, :filter_tags) || entry.default_tag?
73
- if session.tagtree
74
- person.account.default_tag = session.tagtree.default_tag(*person.filter_tags)
75
- else
76
- tags = person.filter_tags || []
77
- person.account.default_tag = tags.first unless tags.length > 1
78
- end
79
- end
80
-
81
- end
82
- update.add(person)
83
-
84
- # set supervisor
85
- unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
86
- if !(sup_id = entry.supervisor_id)
87
- person.supervisor_id = nil
88
- else
89
- if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
90
- person.supervisor_id = supervisor.id
91
- else
92
- # delay setting supervisor if does not exit
93
- supers.add(person) do |person|
94
- person.supervisor_id = sup_id
95
- end
96
- end
97
- end
98
- end
99
-
100
- else
101
- session.logger.error("This person does not exist: #{entry.to_s(:identify)}")
102
- end
103
- end
104
- end
105
-
106
- end
107
-
108
- end
109
- end
110
- end
111
- end
112
- end
@@ -1,64 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class UpdateDetailsCase < DefaultCase
6
-
7
- def process
8
- @cases.define("update-details", type: :sync) do |entries, people, session, options, usecase|
9
- job = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details])
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
-
14
- entries.each.with_index do |entry, i|
15
- if person = people.find(entry, strict: strict_search)
16
- unless options.dig(:exclude, :core)
17
- core_attrs = ["name", "external_id", "email", "filter_tags"]
18
- core_excluded = core_attrs.map.select {|attr| options.dig(:exclude, attr.to_sym)}
19
- core_excluded.push("supervisor_id")
20
-
21
- entry.set_core(person, exclude: core_excluded) unless options.dig(:exclude, :core)
22
-
23
- if session.tagtree && !options.dig(:exclude, :filter_tags)
24
- person.filter_tags = session.tagtree.user_tags(
25
- initial: ini_tags,
26
- final: person.filter_tags,
27
- preserve_custom: true,
28
- add_custom: true
29
- )
30
- end
31
- end
32
-
33
- entry.set_details(person)
34
-
35
- job.add(person)
36
-
37
- # set supervisor
38
- unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
39
- if !(sup_id = entry.supervisor_id)
40
- person.supervisor_id = nil
41
- else
42
- if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
43
- person.supervisor_id = supervisor.id
44
- else
45
- # delay setting supervisor if does not exit
46
- supers.add(person) do |person|
47
- person.supervisor_id = sup_id
48
- end
49
- end
50
- end
51
- end
52
-
53
- else
54
- session.logger.error("This person does not exist: #{entry.to_s(:identify)}")
55
- end
56
- end
57
- end
58
- end
59
-
60
- end
61
- end
62
- end
63
- end
64
- end
@@ -1,114 +0,0 @@
1
- module Eco
2
- module API
3
- class UseCases
4
- class DefaultCases
5
- class UpsertCase < DefaultCase
6
-
7
- def process
8
- @cases.define("upsert", 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
-
13
- strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
14
- pgs = session.policy_groups
15
-
16
- if session.config.people.default_usergroup?
17
- def_id = pgs.to_id(session.config.people.default_usergroup)
18
- end
19
-
20
- entries.each_with_index do |entry, i|
21
- person = people.find(entry, strict: strict_search)
22
- person = session.new_person if create = !person
23
-
24
- unless options.dig(:exclude, :core) && !create
25
- ini_tags = person.filter_tags || []
26
-
27
- core_excluded = ["supervisor_id"]
28
-
29
- unless create
30
- core_attrs = ["name", "external_id", "email", "filter_tags"]
31
- core_excluded += core_attrs.map.select {|attr| options.dig(:exclude, attr.to_sym)}
32
- end
33
-
34
- entry.set_core(person, exclude: core_excluded)
35
- if session.tagtree && !options.dig(:exclude, :filter_tags)
36
- person.filter_tags = session.tagtree.user_tags(
37
- initial: ini_tags,
38
- final: person.filter_tags,
39
- preserve_custom: true,
40
- add_custom: true
41
- )
42
- end
43
- end
44
-
45
- entry.set_details(person) unless options.dig(:exclude, :details)
46
-
47
- unless options.dig(:exclude, :account)
48
- add_account = !person.account
49
- ini_pg_ids = person.account&.policy_group_ids || []
50
-
51
- account_excluded = []
52
- account_excluded.push("policy_group_ids") if options.dig(:exclude, :policy_groups) && !create
53
-
54
- entry.set_account(person, exclude: account_excluded)
55
-
56
- person.account.send_invites = options[:send_invites] if options.key?(:send_invites)
57
-
58
- unless options.dig(:exclude, :policy_groups) && !create
59
- end_pg_ids = person.account.policy_group_ids || []
60
-
61
- if add_account && def_id && !entry.policy_group_ids?
62
- # on account creation, if missing policy_group_ids column in the input
63
- # use default_usergroup, if it's defined
64
- end_pg_ids = [def_id]
65
- end
66
-
67
- # avoid false updates by preserving the original order
68
- person.account.policy_group_ids = pgs.user_pg_ids(
69
- initial: ini_pg_ids,
70
- final: end_pg_ids
71
- )
72
- end
73
-
74
- person.account.permissions_custom = session.new_preset(person) unless !create && options.dig(:exclude, :abilities)
75
-
76
- unless options.dig(:exclude, :filter_tags) || entry.default_tag?
77
- if session.tagtree
78
- person.account.default_tag = session.tagtree.default_tag(*person.filter_tags)
79
- else
80
- tags = person.filter_tags || []
81
- person.account.default_tag = tags.first unless tags.length > 1
82
- end
83
- end
84
-
85
- end
86
-
87
- creation.add(person) if create
88
- update.add(person) unless create
89
-
90
- # set supervisor
91
- unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
92
- if !(sup_id = entry.supervisor_id)
93
- person.supervisor_id = nil
94
- else
95
- if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
96
- person.supervisor_id = supervisor.id
97
- else
98
- # delay setting supervisor if does not exit
99
- supers.add(person) do |person|
100
- person.supervisor_id = sup_id
101
- end
102
- end
103
- end
104
- end
105
-
106
- end
107
- end
108
- end
109
-
110
- end
111
- end
112
- end
113
- end
114
- end