lib/eco/cli_default/people.rb in eco-helpers-2.7.24 vs lib/eco/cli_default/people.rb in eco-helpers-2.7.25

- old
+ new

@@ -1,32 +1,70 @@ +MAX_GET_PARTIAL = 12_000 + ASSETS.cli.config do |cnf| cnf.people do |input, session, options| - get = options.dig(:people, :get) || {} - case - when get == false + get = options.dig(:people, :get) + get = {} if get.nil? + + from_remote = get && get[:from] == :remote + from_local = get && get[:from] == :local + + get_full = from_remote && get[:type] == :full + get_partial = from_remote && get[:type] == :partial + get_by_file = from_local && get[:type] == :file + + # -get-partial: validate input present and under max + if get_partial + msg = "To use -get-partial (partial updates), you need to use -entries-from" + raise msg unless input.is_a?(Enumerable) + + if input.count > MAX_GET_PARTIAL + get_full = true + get_partial = false + + msg = "(Optimization) " + msg << "Switching from partial to full people download. " + msg << "Input (#{input.count}) surpases MAX_GET_PARTIAL (#{MAX_GET_PARTIAL}) entries." + session.logger.info(msg) + + options.deep_merge!(people: { + get: { + from: :remote, + type: :full + } + }) + end + end + + if get == false Eco::API::Organization::People.new([]) - when (get[:from] == :remote) && get[:type] == :full + elsif get_full # -get-people session.micro.people_cache - when (get[:from] == :remote) && get[:type] == :partial + elsif get_partial # -get-partial - unless (input && input.is_a?(Enumerable)) - raise "To use -get-partial (partial updates), you need to use -entries-from" - end session.micro.people_search(input, options: options) - when (get[:from] == :local) && get[:type] == :file + elsif get_by_file # -people-from-backup session.micro.people_load(get[:file], modifier: :file) #people = JSON.parse(File.read(get[:file])) #Eco::API::Organization::People.new(people) else options.deep_merge!(people: { - get: {from: :local, type: :full} + get: { + from: :local, + type: :full + } }) - people = session.micro.people_load(modifier: [:newest, :save]) + + people = session.micro.people_load(modifier: %i[newest save]) + if people.empty? options.deep_merge!(people: { - get: {from: :remote, type: :full} + get: { + from: :remote, + type: :full + } }) people = session.micro.people_cache end people end