module Eco module API module UseCases class DefaultCases class RecoverDBCase < BaseCase 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 creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account]) update = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account]) remove = session.job_group("main").new("remove", usecase: usecase, type: :delete, sets: [:core, :details, :account]) strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict)) pgs = session.policy_groups if options.dig(:include, :delete) people.exclude(entries).map {|person| remove.add(person)} end entries.each.with_index do |entry, i| create = ! (person = people.find(entry, strict: strict_search)) if create && !options.dig(:include, :create) session.logger.error("Entry(#{i}) - this person does not exist: #{entry.email}") else person = session.new_person if create unless options.dig(:exclude, :core) person.external_id = entry.external_id person.name = entry.name person.email = entry.email unless options.dig(:exclude, :email) person.supervisor_id = entry.supervisor_id unless options.dig(:exclude, :supervisor) end unless options.dig(:exclude, :account) person.account = entry.account person.account&.send_invites = options[:send_invites] if options.key?(:send_invites) end person.details = entry.details unless options.dig(:exclude, :details) creation.add(person) if create update.add(person) unless create end end end end end end end end end