module Eco module API module UseCases class DefaultCases class SwitchSupervisorCase < UseCases::BaseCase def process @cases.define("switch-supervisor", type: :transform) do |people, session, options, usecase| unless old_id = options.dig(:super, :old) session.logger.error("You haven't specified the original supervisor. Aborting...") exit end # we could be setting the supervisor to nil unless options[:super].key?(:new) session.logger.error("You haven't specified the new supervisor. Aborting...") exit end new_id = options.dig(:super, :new) unless old_sup = people.person(id: old_id, external_id: old_id, email: old_id) session.logger.error("Couldn't find any user with that id (old-super): '#{old_id}'. Aborting...") exit end unless new_sup = people.person(id: new_id, external_id: new_id, email: new_id) session.logger.error("Couldn't find any user with that id (new-super): '#{new_id}'. Aborting...") exit end people = people.supervisor_id(old_sup.id) unless people.length > 0 session.logger.error("There are no people with supervisor #{old_sup.external_id} (#{old_sup.name} - #{old_sup.email}). Aborting...") exit end session.logger.info("Going to change supervisor '#{old_sup.name}' (#{old_sup.external_id}) to '#{new_sup.name}' (#{new_sup.external_id})") # create batch queue supers = session.job_group("main").new("update", usecase: usecase, type: :update, sets: :core) people.each.with_index do |person, i| person.supervisor_id = new_sup.id supers.add(person) end end end end end end end end