module Eco module API class UseCases class DefaultCases class RecoverDBCase < DefaultCase def process @cases.define("recover-db", type: :sync) do |entries, people, session, options, usecase| unless entries.is_a?(Eco::API::Organization::People) raise "Your input should be an 'Eco::API::Organization::People' object. Got: #{entries.class}" end if options.dig(:include, :create) creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account]) end update = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account]) if options.dig(:include, :delete) remove = session.job_group("main").new("remove", usecase: usecase, type: :delete, sets: [:core, :details, :account]) people.exclude(entries).map {|person| remove.add(person)} end strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict)) pgs = session.policy_groups not_exist = [] entries.each.with_index do |entry, i| create = ! (person = people.find(entry, strict: strict_search)) if create && !options.dig(:include, :create) not_exist.push(entry); else person = session.new_person if create unless options.dig(:exclude, :core) person.external_id = entry.external_id unless options.dig(:exclude, :external_id) person.name = entry.name unless options.dig(:exclude, :name) person.email = entry.email unless options.dig(:exclude, :email) person.supervisor_id = entry.supervisor_id unless options.dig(:exclude, :supervisor) person.filter_tags = entry.filter_tags || [] unless options.dig(:exclude, :filter_tags) end unless options.dig(:exclude, :account) #person.account = entry.account if eaccount = entry.account person.account ||= {} person.account.default_tag = eaccount.default_tag unless options.dig(:exclude, :filter_tags) unless options.dig(:exclude, :policy_groups) && person.account.policy_group_ids person.account.policy_group_ids = eaccount.policy_group_ids end unless options.dig(:exclude, :abilities) && (person.account.permissions_preset || person.account.permissions_custom) person.account.permissions_preset = eaccount.permissions_preset if eaccount.permissions_preset person.account.permissions_custom = eaccount.permissions_custom end person.account.login_provider_ids = eaccount.login_provider_ids if eaccount.login_provider_ids person.account.accept_eula = eaccount.accept_eula if eaccount.accept_eula if eaccount.preferences person.account.doc["preferences"] = JSON.parse((eaccount.doc["preferences"] || {}).to_json) end person.account.starred_ids = eaccount.starred_ids if eaccount.landing_page_id person.account.landing_page_id = eaccount.landing_page_id if eaccount.landing_page_id person.account&.send_invites = options[:send_invites] if options.key?(:send_invites) end end unless options.dig(:exclude, :details) person.details = entry.details end creation.add(person) if create update.add(person) unless create end end unless not_exist.empty? session.logger.error("There were #{not_exist.length} entries of the back up that do not exist in the (filtered?) people manager") session.logger.error("Some examples:") not_exist.slice(0, 4).each_with_index do |entry, i| session.logger.error("Entry(#{i}) - this person does not exist: #{entry.name} (#{entry.email})") end end end end end end end end end