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 def live_tree(enviro: nil) return @live_tree if instance_variable_defined?(:@live_tree) && @live_tree.enviro == enviro trees = live_trees(enviro: enviro) @live_tree = trees.reject do |tree| tree.empty? end.max do |a,b| a.count <=> b.count end.tap do |tree| session_logger.info("Using LIVE LOCATIONS Structure: '#{tree.name}'") if tree end end # Retrieves all the location structures of the organisation def live_trees(enviro: nil) [].tap do |eco_trees| next unless apis.active_api.version_available?(:graphql) next unless graphql = apis.api(version: :graphql) kargs = { includeArchived: false, includeUnpublished: false } next unless trees = graphql.currentOrganization.locationStructures(**kargs) 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