lib/eco/api/usecases/default_cases/update_case.rb in eco-helpers-1.0.5 vs lib/eco/api/usecases/default_cases/update_case.rb in eco-helpers-1.0.6
- old
+ new
@@ -3,69 +3,87 @@
class UseCases
class DefaultCases
class UpdateCase < DefaultCase
def process
- #opts = CLI::Options.new do |p|
- # p.option :exclude, "Allows to exclude certain options"
- # p.suboption :exclude, :core, "Excludes core details from the update.", optional: true
- # p.suboption :exclude, :details, "Excludes schema details from the update.", optional: true
- # p.suboption :exclude, :account, "Excludes account details from the update.", optional: true
- # p.suboption :exclude, :email, "Excludes core email from the update.", optional: true
- # p.suboption :exclude, :supervisor, "Excludes supervisor_id from the update.", optional: true
- # p.suboption :exclude, :abilities, "Excludes the abilities from the update.", optional: true
- #end
-
@cases.define("update", type: :sync) do |entries, people, session, options, usecase|
update = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account])
+ supers = session.job_group("post").new("supers", usecase: usecase, type: :update, sets: :core)
strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
pgs = session.policy_groups
entries.each.with_index do |entry, i|
if person = people.find(entry, strict: strict_search)
- core_excluded = ["supervisor_id"]
- core_excluded.push("email") if options.dig(:exclude, :email)
-
unless options.dig(:exclude, :core)
ini_tags = person.filter_tags || []
+
+ core_attrs = ["name", "external_id", "email", "filter_tags"]
+ core_excluded = core_attrs.map(&:to_sym).select {|attr| options.dig(:exclude, attr)}
+ core_excluded.push("supervisor_id")
+
entry.set_core(person, exclude: core_excluded)
- if session.tagtree
+ if session.tagtree && !options.dig(:exclude, :filter_tags)
person.filter_tags = session.tagtree.user_tags(
initial: ini_tags,
final: person.filter_tags,
preserve_custom: true,
add_custom: true
)
end
end
-
+
entry.set_details(person) unless options.dig(:exclude, :details)
unless options.dig(:exclude, :account)
ini_pg_ids = person.account&.policy_group_ids || []
- entry.set_account(person)
- person.account.policy_group_ids = pgs.user_pg_ids(
- initial: ini_pg_ids,
- final: person.account.policy_group_ids
- )
+ account_excluded = []
+ account_excluded.push("policy_group_ids") if options.dig(:exclude, :policy_groups)
- person.account.permissions_custom = session.new_preset(person) unless options.dig(:exclude, :abilities)
+ entry.set_account(person, exclude: account_excluded)
- if session.tagtree
- person.account.default_tag = session.tagtree.default_tag(*person.filter_tags)
- else
- tags = person.filter_tags
- person.account.default_tag = tags.first unless tags.length > 1
+ person.account.send_invites = options[:send_invites] if options.key?(:send_invites)
+
+ unless options.dig(:exclude, :policy_groups)
+ person.account.policy_group_ids = pgs.user_pg_ids(
+ initial: ini_pg_ids,
+ final: person.account.policy_group_ids
+ )
end
- person.account&.send_invites = options[:send_invites] if options.key?(:send_invites)
+ person.account.permissions_custom = session.new_preset(person) unless options.dig(:exclude, :abilities)
+ unless options.dig(:exclude, :filter_tags)
+ if session.tagtree
+ person.account.default_tag = session.tagtree.default_tag(*person.filter_tags)
+ else
+ tags = person.filter_tags
+ person.account.default_tag = tags.first unless tags.length > 1
+ end
+ end
+
end
update.add(person)
+
+ # set supervisor
+ unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
+ if !(sup_id = entry.supervisor_id)
+ person.supervisor_id = nil
+ else
+ if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
+ person.supervisor_id = supervisor.id
+ else
+ # delay setting supervisor if does not exit
+ supers.add(person) do |person|
+ person.supervisor_id = sup_id
+ end
+ end
+ end
+ end
+
else
session.logger.error("Entry(#{i}) - this person does not exist: #{entry.to_s(:identify)}")
end
end
end