Class: Eco::API::UseCases::DefaultCases::SetSupervisorCase

Inherits:
Common::Loaders::UseCase show all
Defined in:
lib/eco/api/usecases/backup/set_supervisor_case.rb,
lib/eco/api/usecases/default_cases/set_supervisor_case.rb

Instance Method Summary collapse

Methods inherited from Common::Loaders::UseCase

#initialize, type, #type

Methods inherited from Common::BaseLoader

<=>, created_at, #initialize, #name, name_only_once!, set_created_at!

Methods included from Common::ClassHelpers

#class_resolver, #descendants, #descendants?, #new_class, #resolve_class, #to_constant

Constructor Details

This class inherits a constructor from Eco::API::Common::Loaders::UseCase

Instance Method Details

#main(entries, people, session, options, usecase) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/eco/api/usecases/default_cases/set_supervisor_case.rb', line 5

def main(entries, people, session, options, usecase)
  micro    = session.micro
  update   = session.new_job("main", "update", :update, usecase)

  micro.with_each_present(entries, people, options, log_starter: true) do |entry, person|
    micro.with_supervisor(entry.supervisor_id, people) do |supervisor|
      if supervisor
        update.add(person)
        person.supervisor_id = supervisor.id unless options.dig(:exclude, :supervisor)
      else
        unless !entry.supervisor_id
          session.logger.warn("Supervisor '#{entry.supervisor_id}' does not exist. Entry: #{entry.to_s(:identify)}")
        end
      end
    end
  end
end

#processObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/eco/api/usecases/backup/set_supervisor_case.rb', line 7

def process
  @cases.define("set-supervisor", type: :sync) do |entries, people, session, options, usecase|
    job = session.job_group("main").new("update", usecase: usecase, type: :update, sets: :core)

    strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))

    entries.each.with_index do |entry, i|
      person = people.find(entry, strict: strict_search)

      if !person
        session.logger.error("This person does not exist: #{entry.to_s(:identify)}")
      else
        sup_id = entry.supervisor_id
        supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)

        if !supervisor
          if entry.supervisor_id
            msg = "Entry(#{i}) - supervisor id #{entry.supervisor_id} does not exist for person: #{entry.to_s(:identify)}"
            session.logger.warn(msg)
          end
        else
          # set internal id of the supervisor (detect if actually changed)
          person.supervisor_id = supervisor.id
          job.add(person)
        end
      end
    end
  end
end