diff --git a/groups/api/http/endpoints.go b/groups/api/http/endpoints.go index 4197c98e0ba..c41b87df0f7 100644 --- a/groups/api/http/endpoints.go +++ b/groups/api/http/endpoints.go @@ -200,6 +200,9 @@ func retrieveGroupHierarchyEndpoint(svc groups.Service) endpoint.Endpoint { if err != nil { return retrieveGroupHierarchyRes{}, err } + if req.HierarchyPageMeta.Tree { + return buildGroupsResponseTree(hp), nil + } groups := []viewGroupRes{} for _, g := range hp.Groups { @@ -342,3 +345,42 @@ func toViewGroupRes(group groups.Group) viewGroupRes { } return view } + +func buildGroupsResponseTree(page groups.HierarchyPage) retrieveGroupHierarchyRes { + groupsMap := map[string]*groups.Group{} + parentsMap := map[string][]*groups.Group{} + for i := range page.Groups { + if _, ok := groupsMap[page.Groups[i].ID]; !ok { + groupsMap[page.Groups[i].ID] = &page.Groups[i] + parentsMap[page.Groups[i].ID] = make([]*groups.Group, 0) + } + } + + for _, group := range groupsMap { + if children, ok := parentsMap[group.Parent]; ok { + children = append(children, group) + parentsMap[group.Parent] = children + } + } + + res := retrieveGroupHierarchyRes{ + Level: page.Level, + Direction: page.Direction, + Groups: []viewGroupRes{}, + } + + for _, group := range groupsMap { + if children, ok := parentsMap[group.ID]; ok { + group.Children = children + } + } + + for _, group := range groupsMap { + view := toViewGroupRes(*group) + if children, ok := parentsMap[group.Parent]; len(children) == 0 || !ok { + res.Groups = append(res.Groups, view) + } + } + + return res +}