module Eco module API module UseCases class DefaultCases class CreateDetailsWithSupervisorCase < UseCases::BaseCase def process # good candidate to do @cases.case("create-details").use.chain(@cases.case("set-supervisor").use) @cases.define("create-details-with-supervisor", type: :sync) do |entries, people, session, options| creation = session.job_group("main").new("create", type: :create, sets: [:core, :details]) supers = session.job_group("post").new("supers", 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| if person = people.find(entry, strict: strict_search) session.logger.error("Entry(#{i}) - this person (id: '#{person.id}') already exists: #{entry.to_s(:identify)}") else person = session.new_person entry.set_core(person, exclude: "supervisor_id") entry.set_details(person) creation.add(person) # set supervisor if !(sup_id = entry.supervisor_id) person.supervisor_id = nil else if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id) person.supervisor_id = supervisor.id else # delay setting supervisor if does not exit supers.add(person) do |person| #person = session.new_person(person: person.doc) person.sync person.supervisor_id = sup_id person end end end end end end end end end end end end