Skip to content

Commit 414fec8

Browse files
authored
fix(group): List group options (#2207)
1 parent 9b97aee commit 414fec8

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

app/controlplane/pkg/biz/group_integration_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,40 @@ func (s *groupListIntegrationTestSuite) TestList() {
449449
s.Contains(groups[0].Description, "A team")
450450
})
451451

452+
s.Run("list groups with name OR description filter", func() {
453+
// Clear existing groups
454+
_, _ = s.Data.DB.Group.Delete().Exec(ctx)
455+
456+
// Create groups with different names and descriptions
457+
_, err := s.Group.Create(ctx, uuid.MustParse(s.org.ID), "frontend-team", "Frontend development team", uuid.MustParse(s.user.ID))
458+
require.NoError(s.T(), err)
459+
_, err = s.Group.Create(ctx, uuid.MustParse(s.org.ID), "backend-team", "Backend development team", uuid.MustParse(s.user.ID))
460+
require.NoError(s.T(), err)
461+
_, err = s.Group.Create(ctx, uuid.MustParse(s.org.ID), "qa-team", "Quality Assurance team", uuid.MustParse(s.user.ID))
462+
require.NoError(s.T(), err)
463+
_, err = s.Group.Create(ctx, uuid.MustParse(s.org.ID), "devops-team", "Team responsible for infrastructure", uuid.MustParse(s.user.ID))
464+
require.NoError(s.T(), err)
465+
466+
// Filter with a term that appears in one group's name and another group's description
467+
// Should match both the "frontend-team" (name contains "front") and "backend-team" (description contains "development")
468+
filterOpts := &biz.ListGroupOpts{
469+
Name: "front",
470+
Description: "development",
471+
}
472+
473+
groups, count, err := s.Group.List(ctx, uuid.MustParse(s.org.ID), filterOpts, nil)
474+
s.NoError(err)
475+
476+
// Should match 2 groups due to OR filtering
477+
s.Equal(2, count)
478+
s.Equal(2, len(groups))
479+
480+
// Verify that both expected groups are in the results
481+
groupNames := []string{groups[0].Name, groups[1].Name}
482+
s.Contains(groupNames, "frontend-team")
483+
s.Contains(groupNames, "backend-team")
484+
})
485+
452486
s.Run("list groups with member email filter", func() {
453487
// Create a second user
454488
user2, err := s.User.UpsertByEmail(ctx, "[email protected]", nil)

app/controlplane/pkg/data/group.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/membership"
3232
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/organization"
3333
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/orginvitation"
34+
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/predicate"
3435
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/user"
3536
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/workflow"
3637
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/pagination"
@@ -60,16 +61,23 @@ func (g GroupRepo) List(ctx context.Context, orgID uuid.UUID, filterOpts *biz.Li
6061
Where(group.DeletedAtIsNil(), group.OrganizationIDEQ(orgID)).
6162
WithMembers().WithOrganization()
6263

64+
// Apply filters as ORs if any filter is provided
65+
var predicates []predicate.Group
6366
if filterOpts.Name != "" {
64-
query.Where(group.NameContains(filterOpts.Name))
67+
predicates = append(predicates, group.NameContains(filterOpts.Name))
6568
}
6669

6770
if filterOpts.Description != "" {
68-
query.Where(group.DescriptionContains(filterOpts.Description))
71+
predicates = append(predicates, group.DescriptionContains(filterOpts.Description))
6972
}
7073

7174
if filterOpts.MemberEmail != "" {
72-
query.Where(group.HasMembersWith(user.EmailContains(filterOpts.MemberEmail)))
75+
predicates = append(predicates, group.HasMembersWith(user.EmailContains(filterOpts.MemberEmail)))
76+
}
77+
78+
// Apply OR predicates if any exist
79+
if len(predicates) > 0 {
80+
query.Where(group.Or(predicates...))
7381
}
7482

7583
// Get the count of all filtered rows without the limit and offset

0 commit comments

Comments
 (0)