8000 feat: add count endpoint for users, enabling better pagination by presleyp · Pull Request #4848 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: add count endpoint for users, enabling better pagination #4848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix some test bugs
  • Loading branch information
presleyp committed Nov 1, 2022
commit 3a8e4bb70798649c7900b57af2f1df2ee51c4a56
13 changes: 4 additions & 9 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,9 +1272,9 @@ func TestGetFilteredUserCount(t *testing.T) {
OrganizationID: user.OrganizationID,
})
// No params is all users
count, err := client.UserCount(ctx, codersdk.UserCountRequest{})
response, err := client.UserCount(ctx, codersdk.UserCountRequest{})
require.NoError(t, err)
require.Equal(t, count, 2)
require.Equal(t, 2, int(response.Count))
})
t.Run("ActiveUsers", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -1310,19 +1310,14 @@ func TestGetFilteredUserCount(t *testing.T) {
_, err = client.UpdateUserStatus(ctx, alice.Username, codersdk.UserStatusSuspended)
require.NoError(t, err)

count, err := client.UserCount(ctx, codersdk.UserCountRequest{
response, err := client.UserCount(ctx, codersdk.UserCountRequest{
Status: codersdk.UserStatusActive,
})
require.NoError(t, err)
require.Equal(t, count, 1)
require.Equal(t, 1, int(response.Count))
})
}

func TestFilteredUserCount(t *testing.T) {
t.Parallel()

}

func TestPostTokens(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down
17 changes: 16 additions & 1 deletion codersdk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ type User struct {
}

type UserCountRequest struct {
Search string `json:"search,omitempty" typescript:"-"`
// Filter users by status.
Status UserStatus `json:"status,omitempty" typescript:"-"`
// Filter users that have the given role.
Role string `json:"role,omitempty" typescript:"-"`

SearchQuery string `json:"q,omitempty"`
}

Expand Down Expand Up @@ -358,6 +364,15 @@ func (c *Client) UserCount(ctx context.Context, req UserCountRequest) (UserCount
func(r *http.Request) {
q := r.URL.Query()
var params []string
if req.Search != "" {
params = append(params, req.Search)
}
if req.Status != "" {
params = append(params, "status:"+string(req.Status))
}
if req.Role != "" {
params = append(params, "role:"+req.Role)
}
if req.SearchQuery != "" {
params = append(params, req.SearchQuery)
}
Expand All @@ -375,7 +390,7 @@ func (c *Client) UserCount(ctx context.Context, req UserCountRequest) (UserCount
}

var count UserCountResponse
return count, nil
return count, json.NewDecoder(res.Body).Decode(&count)
}

// OrganizationsByUser returns all organizations the user is a member of.
Expand Down
0