require_relative 'base/tree_tracking' require_relative 'base/classifications_parser' module Eco::API::UseCases::GraphQL::Helpers::Location module Base include Eco::API::UseCases::GraphQL::Helpers::Base include TreeTracking include ClassificationsParser # Scopes the target structure `id`. # @note it is basic that the `id` is correctly identified. def target_structure_id @target_structure_id ||= tagtree_id @target_structure_id ||= target_structure_id_const @target_structure_id ||= current_tree.id if current_tree.respond_to?(:id) return @target_structure_id if @target_structure_id msg = "Const TARGET_STRUCTURE_ID has not been defined, " msg << "nor options(:source, :structure_id). " msg << "Infering active locations structure." log(:warn) { msg } # a backup happens: return nil unless (self.current_tree = session_live_tree) @target_structure_id = current_tree.id end # Retrieves the live tree only if `current_tree` hasn't been just retrieved. # @note that `target_structure_id` can retrive the live tree (when `id` is not defined) # By checking if the current_tree changed after calling `target_structure_id` we # prevent unnecessary requests. def live_tree tree_init = current_tree target_id = target_structure_id return current_tree if current_tree != tree_init # a backup happens: self.current_tree = session_live_tree(id: target_id) end # Unique access point to retrieve the live tree # @note ensures archived nodes are retrieved. def session_live_tree(id: nil) session.live_tree(id: id, include_archived: true) end def tagtree_id %i[target_structure_id tagtree_id structure_id].map do |key| options.dig(:source, key) end.compact.first end def target_structure_id_const return nil unless self.class.const_defined?(:TARGET_STRUCTURE_ID) self.class.const_get(:TARGET_STRUCTURE_ID) end end end