module Eco module API class Session class Config class TagTree < BaseConfig attr_key :file def scope_tree(enviro: nil) return @tagtree if instance_variable_defined?(:@tagtree) && @tagtree.enviro == enviro if tree_file = self.file if (tree = file_manager.load_json(tree_file)) && !tree.empty? @tagtree = Eco::API::Organization::TagTree.new(tree, enviro: enviro) end end @tagtree ||= live_tree(enviro: enviro).tap do |tree| unless tree && !tree.empty? raise "Could not find a locations structure." end end end # Among all the locations structures it selects the one with more location nodes # If `id` is provided, it only retrieves this locations structure. def live_tree(id: nil, enviro: nil, include_archived: false, **kargs, &block) return @live_tree if instance_variable_defined?(:@live_tree) && @live_tree.enviro == enviro if id args = {id: id, enviro: enviro, include_archived: include_archived}.merge(kargs) @live_tree = live_tree_get(**args, &block) else # note that `include_archived` nodes is NOT the same as including archived structures # => In `live_tree` the paramter refers to nodes trees = live_trees(enviro: enviro, &block) @live_tree = trees.reject do |tree| tree.empty? end.max do |a,b| a.count <=> b.count end end.tap do |tree| if tree msg = "Using LIVE LOCATIONS Structure: '#{tree.name}' (#{tree.count} nodes)" session_logger.info(msg) end end end # Gets a single locations structure # @note it does not memoize def live_tree_get(id: nil, enviro: nil, include_archived: false, **kargs, &block) return nil unless apis.active_api.version_available?(:graphql) return nil unless graphql = apis.api(version: :graphql) #kargs = { includeArchived: include_archived }.merge(kargs).slice(:includeArchived) # For now, this endpoint only accepts `id` as a parameter. It is pending to # expose further parameters via query return nil unless tree = graphql.currentOrganization.locationStructure(id: id, &block) args = { enviro: enviro, id: tree.id, name: tree.name } Eco::API::Organization::TagTree.new(tree.treeify, **args) end # Retrieves all the location structures of the organisation def live_trees(enviro: nil, include_archived: false, **kargs, &block) [].tap do |eco_trees| next unless apis.active_api.version_available?(:graphql) next unless graphql = apis.api(version: :graphql) kargs = { includeArchived: include_archived, includeUnpublished: false }.merge(kargs).slice(:includeArchived, :includeUnpublished) next unless trees = graphql.currentOrganization.locationStructures(**kargs, &block) trees.each do |tree| args = { enviro: enviro, id: tree.id, name: tree.name } eco_tree = Eco::API::Organization::TagTree.new(tree.treeify, **args) eco_trees.push(eco_tree) end end end private def session ASSETS.session end def session_logger session.logger end end end end end end