class Eco::API::UseCases::DefaultCases::HrisCase

def append_created

def append_created
  options.dig(:people, :append_created)
end

def leavers_callback(person)

def leavers_callback(person)
  person.supervisor_id = nil
  person.account       = nil if person.account
end

def main(entries, people, session, options, usecase)

def main(entries, people, session, options, usecase)
  @people = people
  require_only_one_schema!
  micro     = session.micro
  @leavers  = session.new_job("pre", "leavers",  :update, usecase, :account)
  @creation = session.new_job("main", "create",  :create, usecase)
  @update   = session.new_job("main", "update",  :update, usecase)
  @supers   = session.new_job("post", "supers",  :update, usecase, :core)
  micro.with_each_leaver(entries, people, options) do |person|
    leavers.add(person, &method(:leavers_callback))
  end
  micro.with_each(entries, people, options, append_created: append_created) do |entry, person|
    person.new? ? creation.add(person) : update.add(person)
    micro.set_core_with_supervisor(entry, person, people, supers, options)
    entry.set_details(person) unless options.dig(:exclude, :details)
    micro.set_account(entry, person, options)
  end
end

def require_only_one_schema!

def require_only_one_schema!
  return if options.dig(:people, :filter, :details, :schema_id)
  active_schema = session.schema
  other_schemas = session.schemas.map(&:id) - [active_schema.id]
  other_people  = people.group_by_schema.values_at(*other_schemas).map(&:to_a).flatten
  return unless other_people.length > 3
  msg  = "There are #{other_people.length} people in schemas other than #{active_schema.name}."
  msg << " Please, use the filter option '-schema-id SchemaName' for the 'hris' case"
  msg << " to only include those of that schema in the current update."
  msg << " The HRIS case identifies people that are not in the file as leavers"
  msg << " (as it will remove the account of all the people of other schemas"
  msg << " if they are not in the input file)."
  msg << "\n For example: -schema-id '#{active_schema.name.downcase}'"
  log(:error) { msg }
  raise msg
end