Skip to content

Commit

Permalink
fix: failing to return empty list with member count flag (#443)
Browse files Browse the repository at this point in the history
Signed-off-by: Kush Sharma <[email protected]>
  • Loading branch information
kushsharma authored Jan 2, 2024
1 parent bdeea29 commit dac6be3
Show file tree
Hide file tree
Showing 10 changed files with 930 additions and 891 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TAG := $(shell git rev-list --tags --max-count=1)
VERSION := $(shell git describe --tags ${TAG})
.PHONY: build check fmt lint test test-race vet test-cover-html help install proto ui
.DEFAULT_GOAL := build
PROTON_COMMIT := "4646cb07b52e095ce32b968aafd19a55226e25bf"
PROTON_COMMIT := "41581c2eb9697b65ce4f3cc1cbd98bd8eaf47034"

ui:
@echo " > generating ui build"
Expand Down
2 changes: 1 addition & 1 deletion core/group/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (s Service) List(ctx context.Context, flt Filter) ([]Group, error) {
if err != nil {
return nil, err
}
if flt.WithMemberCount {
if flt.WithMemberCount && len(groups) > 0 {
memberCounts, err := s.policyService.GroupMemberCount(ctx, utils.Map(groups, func(grp Group) string {
return grp.ID
}))
Expand Down
8 changes: 3 additions & 5 deletions core/organization/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type PolicyService interface {
Create(ctx context.Context, policy policy.Policy) (policy.Policy, error)
List(ctx context.Context, flt policy.Filter) ([]policy.Policy, error)
Delete(ctx context.Context, id string) error
Count(ctx context.Context, flt policy.Filter) (int64, error)
OrgMemberCount(ctx context.Context, id string) (policy.MemberCount, error)
}

type PreferencesService interface {
Expand Down Expand Up @@ -312,8 +312,6 @@ func (s Service) DeleteModel(ctx context.Context, id string) error {
}

func (s Service) MemberCount(ctx context.Context, orgID string) (int64, error) {
return s.policyService.Count(ctx, policy.Filter{
OrgID: orgID,
PrincipalType: schema.UserPrincipal,
})
mc, err := s.policyService.OrgMemberCount(ctx, orgID)
return int64(mc.Count), err
}
1 change: 1 addition & 0 deletions core/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Repository interface {
Delete(ctx context.Context, id string) error
GroupMemberCount(ctx context.Context, IDs []string) ([]MemberCount, error)
ProjectMemberCount(ctx context.Context, IDs []string) ([]MemberCount, error)
OrgMemberCount(ctx context.Context, ID string) (MemberCount, error)
}

type Policy struct {
Expand Down
4 changes: 4 additions & 0 deletions core/policy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,7 @@ func (s Service) GroupMemberCount(ctx context.Context, ids []string) ([]MemberCo
func (s Service) ProjectMemberCount(ctx context.Context, ids []string) ([]MemberCount, error) {
return s.repository.ProjectMemberCount(ctx, ids)
}

func (s Service) OrgMemberCount(ctx context.Context, id string) (MemberCount, error) {
return s.repository.OrgMemberCount(ctx, id)
}
2 changes: 1 addition & 1 deletion core/project/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s Service) List(ctx context.Context, f Filter) ([]Project, error) {
return nil, err
}

if f.WithMemberCount {
if f.WithMemberCount && len(projects) > 0 {
// get member count for each project
projectIDs := utils.Map(projects, func(p Project) string {
return p.ID
Expand Down
40 changes: 38 additions & 2 deletions internal/store/postgres/policy_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ func (r PolicyRepository) GroupMemberCount(ctx context.Context, groupIDs []strin
Select(goqu.I("resource_id").As("id"), goqu.COUNT(goqu.DISTINCT(goqu.I("principal_id"))).As("count")).
Where(goqu.Ex{
"resource_type": schema.GroupNamespace,
"resource_id": groupIDs,
"principal_type": []string{
schema.UserPrincipal,
schema.ServiceUserPrincipal,
},
"resource_id": groupIDs,
}).
GroupBy("resource_id")

Expand Down Expand Up @@ -330,12 +330,12 @@ func (r PolicyRepository) ProjectMemberCount(ctx context.Context, projectIDs []s
Select(goqu.I("resource_id").As("id"), goqu.COUNT(goqu.DISTINCT(goqu.I("principal_id"))).As("count")).
Where(goqu.Ex{
"resource_type": schema.ProjectNamespace,
"resource_id": projectIDs,
"principal_type": []string{
schema.UserPrincipal,
schema.ServiceUserPrincipal,
schema.GroupPrincipal,
},
"resource_id": projectIDs,
}).
GroupBy("resource_id")

Expand All @@ -359,3 +359,39 @@ func (r PolicyRepository) ProjectMemberCount(ctx context.Context, projectIDs []s

return result, nil
}

func (r PolicyRepository) OrgMemberCount(ctx context.Context, id string) (policy.MemberCount, error) {
if len(id) == 0 {
return policy.MemberCount{}, policy.ErrInvalidID
}
stmt := goqu.From("policies").
Select(goqu.I("resource_id").As("id"), goqu.COUNT(goqu.DISTINCT(goqu.I("principal_id"))).As("count")).
Where(goqu.Ex{
"resource_type": schema.OrganizationNamespace,
"resource_id": id,
"principal_type": []string{
schema.UserPrincipal,
},
}).
GroupBy("resource_id")

query, params, err := stmt.ToSQL()
if err != nil {
return policy.MemberCount{}, fmt.Errorf("%w: %s", queryErr, err)
}

var result policy.MemberCount
if err = r.dbc.WithTimeout(ctx, TABLE_POLICIES, "OrgMemberCount", func(ctx context.Context) error {
return r.dbc.SelectContext(ctx, &result, query, params...)
}); err != nil {
err = checkPostgresError(err)
switch {
case errors.Is(err, sql.ErrNoRows):
return result, nil
default:
return result, err
}
}

return result, nil
}
Loading

0 comments on commit dac6be3

Please sign in to comment.