Skip to content

Commit 6fe4cc0

Browse files
committed
chore: implement filters for the organizations query
1 parent f24cb5c commit 6fe4cc0

File tree

10 files changed

+64
-23
lines changed

10 files changed

+64
-23
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,9 +1700,9 @@ func (q *querier) GetOrganizationIDsByMemberIDs(ctx context.Context, ids []uuid.
17001700
return fetchWithPostFilter(q.auth, policy.ActionRead, q.db.GetOrganizationIDsByMemberIDs)(ctx, ids)
17011701
}
17021702

1703-
func (q *querier) GetOrganizations(ctx context.Context) ([]database.Organization, error) {
1703+
func (q *querier) GetOrganizations(ctx context.Context, args database.GetOrganizationsParams) ([]database.Organization, error) {
17041704
fetch := func(ctx context.Context, _ interface{}) ([]database.Organization, error) {
1705-
return q.db.GetOrganizations(ctx)
1705+
return q.db.GetOrganizations(ctx, args)
17061706
}
17071707
return fetchWithPostFilter(q.auth, policy.ActionRead, fetch)(ctx, nil)
17081708
}

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ func (s *MethodTestSuite) TestOrganization() {
635635
def, _ := db.GetDefaultOrganization(context.Background())
636636
a := dbgen.Organization(s.T(), db, database.Organization{})
637637
b := dbgen.Organization(s.T(), db, database.Organization{})
638-
check.Args().Asserts(def, policy.ActionRead, a, policy.ActionRead, b, policy.ActionRead).Returns(slice.New(def, a, b))
638+
check.Args(database.GetOrganizationsParams{}).Asserts(def, policy.ActionRead, a, policy.ActionRead, b, policy.ActionRead).Returns(slice.New(def, a, b))
639639
}))
640640
s.Run("GetOrganizationsByUserID", s.Subtest(func(db database.Store, check *expects) {
641641
u := dbgen.User(s.T(), db, database.User{})

coderd/database/dbmem/dbmem.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,14 +3034,24 @@ func (q *FakeQuerier) GetOrganizationIDsByMemberIDs(_ context.Context, ids []uui
30343034
return getOrganizationIDsByMemberIDRows, nil
30353035
}
30363036

3037-
func (q *FakeQuerier) GetOrganizations(_ context.Context) ([]database.Organization, error) {
3037+
func (q *FakeQuerier) GetOrganizations(_ context.Context, args database.GetOrganizationsParams) ([]database.Organization, error) {
30383038
q.mutex.RLock()
30393039
defer q.mutex.RUnlock()
30403040

3041-
if len(q.organizations) == 0 {
3042-
return nil, sql.ErrNoRows
3041+
tmp := make([]database.Organization, 0)
3042+
for _, org := range q.organizations {
3043+
if len(args.IDs) > 0 {
3044+
if !slices.Contains(args.IDs, org.ID) {
3045+
continue
3046+
}
3047+
}
3048+
if args.Name != "" && !strings.EqualFold(org.Name, args.Name) {
3049+
continue
3050+
}
3051+
tmp = append(tmp, org)
30433052
}
3044-
return q.organizations, nil
3053+
3054+
return tmp, nil
30453055
}
30463056

30473057
func (q *FakeQuerier) GetOrganizationsByUserID(_ context.Context, userID uuid.UUID) ([]database.Organization, error) {
@@ -3060,9 +3070,7 @@ func (q *FakeQuerier) GetOrganizationsByUserID(_ context.Context, userID uuid.UU
30603070
organizations = append(organizations, organization)
30613071
}
30623072
}
3063-
if len(organizations) == 0 {
3064-
return nil, sql.ErrNoRows
3065-
}
3073+
30663074
return organizations, nil
30673075
}
30683076

coderd/database/dbmem/dbmem_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestInTx(t *testing.T) {
4646
go func() {
4747
<-inTx
4848
for i := 0; i < 20; i++ {
49-
orgs, err := uut.GetOrganizations(context.Background())
49+
orgs, err := uut.GetOrganizations(context.Background(), database.GetOrganizationsParams{})
5050
if err != nil {
5151
assert.ErrorIs(t, err, sql.ErrNoRows)
5252
}

coderd/database/dbmock/dbmock.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ func TestDefaultOrg(t *testing.T) {
516516
ctx := context.Background()
517517

518518
// Should start with the default org
519-
all, err := db.GetOrganizations(ctx)
519+
all, err := db.GetOrganizations(ctx, database.GetOrganizationsParams{})
520520
require.NoError(t, err)
521521
require.Len(t, all, 1)
522522
require.True(t, all[0].IsDefault, "first org should always be default")
@@ -1211,7 +1211,7 @@ func TestExpectOne(t *testing.T) {
12111211
dbgen.Organization(t, db, database.Organization{})
12121212

12131213
// Organizations is an easy table without foreign key dependencies
1214-
_, err = database.ExpectOne(db.GetOrganizations(ctx))
1214+
_, err = database.ExpectOne(db.GetOrganizations(ctx, database.GetOrganizationsParams{}))
12151215
require.ErrorContains(t, err, "too many rows returned")
12161216
})
12171217
}

coderd/database/queries.sql.go

Lines changed: 21 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/organizations.sql

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,21 @@ LIMIT
1212
SELECT
1313
*
1414
FROM
15-
organizations;
15+
organizations
16+
WHERE
17+
true
18+
-- Filter by ids
19+
AND CASE
20+
WHEN array_length(@ids :: uuid[], 1) > 0 THEN
21+
id = ANY(@ids)
22+
ELSE true
23+
END
24+
AND CASE
25+
WHEN @name::text != '' THEN
26+
LOWER("name") = LOWER(@name)
27+
ELSE true
28+
END
29+
;
1630

1731
-- name: GetOrganizationByID :one
1832
SELECT

coderd/organizations.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package coderd
33
import (
44
"net/http"
55

6+
"github.com/coder/coder/v2/coderd/database"
67
"github.com/coder/coder/v2/coderd/database/db2sdk"
78
"github.com/coder/coder/v2/coderd/httpapi"
89
"github.com/coder/coder/v2/coderd/httpmw"
@@ -18,7 +19,7 @@ import (
1819
// @Router /organizations [get]
1920
func (api *API) organizations(rw http.ResponseWriter, r *http.Request) {
2021
ctx := r.Context()
21-
organizations, err := api.Database.GetOrganizations(ctx)
22+
organizations, err := api.Database.GetOrganizations(ctx, database.GetOrganizationsParams{})
2223
if httpapi.Is404Error(err) {
2324
httpapi.ResourceNotFound(rw)
2425
return

0 commit comments

Comments
 (0)