Sha256: 452736a5ea127054e0663cfd9312484e8565a8e722cff4b092d28e602f56472b

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

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)
          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|
              if tree
                logger.info("Live tree in use: '#{tree.name}'")
              end
            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
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eco-helpers-2.2.3 lib/eco/api/session/config/tagtree.rb