class Eco::API::UseCases::DefaultCases::HrisCase < Eco::API::Common::Loaders::UseCase name "hris" type :sync attr_reader :creation, :update, :supers, :leavers attr_reader :people 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 private def append_created options.dig(:people, :append_created) end def leavers_callback(person) person.supervisor_id = nil person.account = nil if person.account end def require_only_one_schema! unless schema_id = 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 if 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 to only include those of that schema" msg << " in the current update. 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 if they are not in the input file)." msg << "\n For example: -schema-id '#{active_schema.name.downcase}'" logger.error(msg) raise msg end end end end