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 b887960 commit 254d660
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
29 changes: 27 additions & 2 deletions groups/api/http/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ func TestRetrieveGroupHierarchyEndpoint(t *testing.T) {
gs, svc, authn := newGroupsServer()
defer gs.Close()

retrieveHierarchRes := groups.HierarchyPage{
retrieveHierarchyRes := groups.HierarchyPage{
Groups: []groups.Group{validGroupResp},
HierarchyPageMeta: groups.HierarchyPageMeta{
Level: 1,
Expand All @@ -1075,6 +1075,15 @@ func TestRetrieveGroupHierarchyEndpoint(t *testing.T) {
},
}

treeHierarchyRes := groups.HierarchyPage{
Groups: []groups.Group{validGroupResp},
HierarchyPageMeta: groups.HierarchyPageMeta{
Level: 1,
Direction: -1,
Tree: true,
},
}

cases := []struct {
desc string
token string
Expand All @@ -1100,7 +1109,23 @@ func TestRetrieveGroupHierarchyEndpoint(t *testing.T) {
Direction: -1,
Tree: false,
},
svcRes: retrieveHierarchRes,
svcRes: retrieveHierarchyRes,
svcErr: nil,
status: http.StatusOK,
err: nil,
},
{
desc: "retrieve group hierarchy successfully with tree",
token: validToken,
domainID: validID,
groupID: validID,
query: "level=1&dir=-1&tree=true",
pageMeta: groups.HierarchyPageMeta{
Level: 1,
Direction: -1,
Tree: true,
},
svcRes: treeHierarchyRes,
svcErr: nil,
status: http.StatusOK,
err: nil,
Expand Down
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
}

Check warning on line 363 in groups/api/http/endpoints.go

View check run for this annotation

Codecov / codecov/patch

groups/api/http/endpoints.go#L361-L363

Added lines #L361 - L363 were not covered by tests
}

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 254d660

Please sign in to comment.