Skip to content

Commit

Permalink
fix(groups): fix listingg groups hierarchy with tree
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Gateru <[email protected]>
  • Loading branch information
felixgateru committed Dec 16, 2024
1 parent e612ba7 commit 07a2817
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions groups/api/http/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 07a2817

Please sign in to comment.