# @example # -restore-db-from backup.json -skip-api-policies -skip-batch-policy -include-excluded -simulate class Eco::API::UseCases::Default::People::Amend::RestoreDBCase < Eco::API::Common::Loaders::UseCase name "restore-db" type :sync def main(entries, people, session, options, usecase) # rubocop:disable Metrics/AbcSize require_people_as_entries!(entries) restart = session.new_job("main", "restart", :create, usecase) if options.dig(:include, :create) update = session.new_job("main", "update", :update, usecase) delete = session.new_job("main", "delete", :update, usecase) if options.dig(:include, :delete) micro.with_each_leaver(entries, people, options) do |person| delete.add(person) end end re_starters = [] micro.with_each(entries, people, options) do |entry, person| if person.new? && !options.dig(:include, :create) re_starters.push(entry) next end person.new? ? restart.add(person) : update.add(person) core_copy(entry, person) unless options.dig(:exclude, :core) person.details = entry.details unless options.dig(:exclude, :details) unless options.dig(:exclude, :account) || !entry.account person.account ||= {} account_copy(entry.account, person.account) end end report_re_starters(re_starters) end private def core_copy(entry, person) 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) person.freemium = entry.freemium end def account_copy(src, dst) # rubocop:disable Metrics/AbcSize return unless src dst.default_tag = src.default_tag unless options.dig(:exclude, :filter_tags) dst.policy_group_ids = src.policy_group_ids unless options.dig(:exclude, :policy_groups) dst.permissions_custom = src.permissions_custom unless options.dig(:exclude, :abilities) unless options.dig(:exclude, :login_providers) || options[:source_enviro] dst.login_provider_ids = src.login_provider_ids end if src.preferences # rubocop:disable Style/IfUnlessModifier dst.doc["preferences"] = JSON.parse((src.doc["preferences"] || {}).to_json) end unless options[:source_enviro] dst.starred_ids = src.starred_ids dst.landing_page_id = src.landing_page_id end dst&.send_invites = options[:send_invites] if options.key?(:send_invites) end def require_people_as_entries!(entries) return true if entries.is_a?(Eco::API::Organization::People) log(:error) { "Your input should be an 'Eco::API::Organization::People' object. Got: #{entries.class}" } exit(1) end def report_re_starters(re_starters) return if re_starters.empty? examples = re_starters.slice(0, 4).map do |entry| "This person does not exist: '#{entry.name}' (#{entry.email})" end log(:error) { msg = [] msg << "There were #{re_starters.length} entries of the backup " msg << "that do not exist in the (filtered?) people manager." msg << "Some examples:" msg << " • #{examples.join("\n • ")}" msg.join("\n") } end end