Skip to content
Prev Previous commit
Next Next commit
remove count endpoint
  • Loading branch information
f0ssel committed Nov 14, 2022
commit 482b59fa5aa397a8a2aa7b70c01c12285f4b1b5f
1 change: 0 additions & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ func New(options *Options) *API {
)
r.Post("/", api.postUser)
r.Get("/", api.users)
r.Get("/count", api.userCount)
r.Post("/logout", api.postLogout)
// These routes query information about site wide roles.
r.Route("/roles", func(r chi.Router) {
Expand Down
36 changes: 0 additions & 36 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,42 +259,6 @@ func (api *API) users(rw http.ResponseWriter, r *http.Request) {
})
}

func (api *API) userCount(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
query := r.URL.Query().Get("q")
params, errs := userSearchQuery(query)
if len(errs) > 0 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid user search query.",
Validations: errs,
})
return
}

sqlFilter, err := api.HTTPAuth.AuthorizeSQLFilter(r, rbac.ActionRead, rbac.ResourceUser.Type)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error preparing sql filter.",
Detail: err.Error(),
})
return
}

count, err := api.Database.GetAuthorizedUserCount(ctx, database.GetFilteredUserCountParams{
Search: params.Search,
Status: params.Status,
RbacRole: params.RbacRole,
}, sqlFilter)
if err != nil {
httpapi.InternalServerError(rw, err)
return
}

httpapi.Write(ctx, rw, http.StatusOK, codersdk.UserCountResponse{
Count: count,
})
}

// Creates a new user.
func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down
52 changes: 0 additions & 52 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,58 +1255,6 @@ func TestGetUsers(t *testing.T) {
})
}

func TestGetFilteredUserCount(t *testing.T) {
t.Parallel()
t.Run("AllUsers", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

client.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "alice@email.com",
Username: "alice",
Password: "password",
OrganizationID: user.OrganizationID,
})
// No params is all users
response, err := client.UserCount(ctx, codersdk.UserCountRequest{})
require.NoError(t, err)
require.Equal(t, 2, int(response.Count))
})
t.Run("ActiveUsers", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
first := coderdtest.CreateFirstUser(t, client)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

_, err := client.User(ctx, first.UserID.String())
require.NoError(t, err, "")

// Alice will be suspended
alice, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "alice@email.com",
Username: "alice",
Password: "password",
OrganizationID: first.OrganizationID,
})
require.NoError(t, err)

_, err = client.UpdateUserStatus(ctx, alice.Username, codersdk.UserStatusSuspended)
require.NoError(t, err)

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

func TestGetUsersPagination(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down
48 changes: 0 additions & 48 deletions codersdk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,6 @@ type GetUsersResponse struct {
Count int `json:"count"`
}

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"`
}

type UserCountResponse struct {
Count int64 `json:"count"`
}

type CreateFirstUserRequest struct {
Email string `json:"email" validate:"required,email"`
Username string `json:"username" validate:"required,username"`
Expand Down Expand Up @@ -364,40 +350,6 @@ func (c *Client) Users(ctx context.Context, req UsersRequest) (GetUsersResponse,
return usersRes, json.NewDecoder(res.Body).Decode(&usersRes)
}

func (c *Client) UserCount(ctx context.Context, req UserCountRequest) (UserCountResponse, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/users/count", nil,
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)
}
q.Set("q", strings.Join(params, " "))
r.URL.RawQuery = q.Encode()
},
)
if err != nil {
return UserCountResponse{}, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return UserCountResponse{}, readBodyAsError(res)
}

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

// OrganizationsByUser returns all organizations the user is a member of.
func (c *Client) OrganizationsByUser(ctx context.Context, user string) ([]Organization, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/organizations", user), nil)
Expand Down
8 changes: 0 additions & 8 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,6 @@ export const getUsers = async (
return response.data
}

export const getUserCount = async (
options: TypesGen.UserCountRequest,
): Promise<TypesGen.UserCountResponse> => {
const url = getURLWithSearchParams("/api/v2/users/count", options)
const response = await axios.get(url.toString())
return response.data
}

export const getOrganization = async (
organizationId: string,
): Promise<TypesGen.Organization> => {
Expand Down
10 changes: 0 additions & 10 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,16 +754,6 @@ export interface User {
readonly avatar_url: string
}

// From codersdk/users.go
export interface UserCountRequest {
readonly q?: string
}

// From codersdk/users.go
export interface UserCountResponse {
readonly count: number
}

// From codersdk/users.go
export interface UserRoles {
readonly roles: string[]
Expand Down