lib/eco/api/session/task.rb in eco-helpers-0.9.2 vs lib/eco/api/session/task.rb in eco-helpers-0.9.3

- old
+ new

@@ -13,24 +13,67 @@ file = file_manager.save_json(people, filename, :timestamp) logger.info("#{people.length} people loaded and saved locally to #{file}.") Eco::API::Organization::People.new(people) end - def search(data, silent: true) + def people_refresh(people:, include_created: true) + ini = people.length + if include_created + session.job_groups.find_jobs(type: :create).map do |job| + people = people.merge(job.people) + end + end + + created = people.length - ini + msg = "Going to refresh #{people.length} people with server data" + msg += " (including #{created} that were created)" if created > 0 + session.logger.info(msg) + status = session.batch.get_people(people, silent: true) + entries = status.people + + missing = people.length - entries.length + session.logger.error("Missed to obtain #{missing} people during the refresh") if missing > 0 + + Eco::API::Organization::People.new(status.people) + end + + def search(data, options: {}, silent: true) + strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict)) + # to scope people to be fresh data got via api - session.logger.info("going to api get entries...") + session.logger.info("going to api get #{data.length} entries...") + status = session.batch.search(data, silent: silent) people = Eco::API::Organization::People.new(status.people) + # get the supervisors - session.logger.info("going to api get supervisors...") + supers = people.each_with_object([]) do |person, sup| + if sup_id = person.supervisor_id + spr = {"id" => sup_id} + sup.push(spr) unless sup.include?(spr) || people.person(id: sup_id, external_id: sup_id) + end + end + + if supers.length > 0 + session.logger.info("going to api get #{supers.length} current supervisors...") + status = session.batch.search(supers, silent: silent) + people = people.merge(status.people, strict: strict_search) + end + supers = data.each_with_object([]) do |entry, sup| if entry.respond_to?(:supervisor_id) && !entry.supervisor_id.to_s.strip.empty? + sup_id = entry.supervisor_id spr = {"id" => entry.supervisor_id} - sup.push(spr) unless sup.include?(spr) + sup.push(spr) unless sup.include?(spr) || people.person(id: sup_id, external_id: sup_id) end end - status = session.batch.search(supers, silent: silent) - people = people.merge(status.people) + + if supers.length > 0 + session.logger.info("going to api get #{supers.length} supervisors as per entries...") + status = session.batch.search(supers, silent: silent) + people = people.merge(status.people, strict: strict_search) + end + session.logger.info("could get #{people.length} people (out of #{data.length} entries)") people end def load_people(filename = enviro.config.people.cache, modifier: [:newest, :api])