Skip to content

Commit

Permalink
Merge pull request opf#17139 from opf/implementation/58868-introduce-…
Browse files Browse the repository at this point in the history
…aggregate-for-fetching-hierarchy-items-for-the-api

[58868] Reworked depth calculation for ItemsAPI for hierarchical custom field
  • Loading branch information
apfohl authored Nov 8, 2024
2 parents 26c9344 + aaf7fed commit 139a011
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/api/v3/custom_fields/hierarchy/items_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,44 @@ module API
module V3
module CustomFields
module Hierarchy
class HierarchicalItemAggregate
attr_accessor :depth

delegate :id, :label, :short, :parent, :children, :root?, to: :item

def initialize(item:, depth:)
@item = item
@depth = depth
end

private

attr_accessor :item
end

class ItemsAPI < ::API::OpenProjectAPI
include Dry::Monads[:result]

helpers do
def flatten_tree_hash(hash)
flat_list = []
queue = [hash]
queue = [hash.merge({ depth: 0 })]

# From the service we get a hashed tree like this:
# {:a => {:b => {:c1 => {:d1 => {}}, :c2 => {:d2 => {}}}, :b2 => {}}}
#
# We flatten it depth first to this result list:
# [:a, :b, :c1, :d1, :c2, :d2, :b2]

while queue.any?
current = queue.shift
depth = current[:depth]
item, children = current.shift
flat_list << item
queue.unshift(current) unless current.empty?
queue.unshift(children) unless children.empty?

flat_list << HierarchicalItemAggregate.new(item:, depth:)

queue.unshift(current) unless current.keys == [:depth]
queue.unshift(children.merge({ depth: depth + 1 })) unless children.empty?
end

flat_list
Expand Down

0 comments on commit 139a011

Please sign in to comment.