Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 494aca2

Browse files
committed
feat: add organization_ids in the user(s) response
1 parent 441ffd6 commit 494aca2

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

coderd/users.go

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/coder/coder/cryptorand"
2525
)
2626

27+
type OrganizationsByUserId = map[string][]database.Organization
28+
2729
// Returns whether the initial user has been created or not.
2830
func (api *api) firstUser(rw http.ResponseWriter, r *http.Request) {
2931
userCount, err := api.Database.GetUserCount(r.Context())
@@ -145,8 +147,14 @@ func (api *api) users(rw http.ResponseWriter, r *http.Request) {
145147
return
146148
}
147149

150+
organizationsByUserId := OrganizationsByUserId{}
151+
for _, user := range users {
152+
userOrganizations := getUserOrganizations(api, rw, r, user)
153+
organizationsByUserId[user.ID.String()] = userOrganizations
154+
}
155+
148156
render.Status(r, http.StatusOK)
149-
render.JSON(rw, r, convertUsers(users))
157+
render.JSON(rw, r, convertUsers(users, organizationsByUserId))
150158
}
151159

152160
// Creates a new user.
@@ -213,15 +221,17 @@ func (api *api) postUser(rw http.ResponseWriter, r *http.Request) {
213221
return
214222
}
215223

216-
httpapi.Write(rw, http.StatusCreated, convertUser(user))
224+
organizations := getUserOrganizations(api, rw, r, user)
225+
226+
httpapi.Write(rw, http.StatusCreated, convertUser(user, organizations))
217227
}
218228

219229
// Returns the parameterized user requested. All validation
220230
// is completed in the middleware for this route.
221-
func (*api) userByName(rw http.ResponseWriter, r *http.Request) {
231+
func (api *api) userByName(rw http.ResponseWriter, r *http.Request) {
222232
user := httpmw.UserParam(r)
223-
224-
httpapi.Write(rw, http.StatusOK, convertUser(user))
233+
organizations := getUserOrganizations(api, rw, r, user)
234+
httpapi.Write(rw, http.StatusOK, convertUser(user, organizations))
225235
}
226236

227237
func (api *api) putUserProfile(rw http.ResponseWriter, r *http.Request) {
@@ -278,7 +288,9 @@ func (api *api) putUserProfile(rw http.ResponseWriter, r *http.Request) {
278288
return
279289
}
280290

281-
httpapi.Write(rw, http.StatusOK, convertUser(updatedUserProfile))
291+
organizations := getUserOrganizations(api, rw, r, user)
292+
293+
httpapi.Write(rw, http.StatusOK, convertUser(updatedUserProfile, organizations))
282294
}
283295

284296
func (api *api) putUserSuspend(rw http.ResponseWriter, r *http.Request) {
@@ -297,7 +309,9 @@ func (api *api) putUserSuspend(rw http.ResponseWriter, r *http.Request) {
297309
return
298310
}
299311

300-
httpapi.Write(rw, http.StatusOK, convertUser(suspendedUser))
312+
organizations := getUserOrganizations(api, rw, r, user)
313+
314+
httpapi.Write(rw, http.StatusOK, convertUser(suspendedUser, organizations))
301315
}
302316

303317
// Returns organizations the parameterized user has access to.
@@ -626,20 +640,42 @@ func (api *api) createUser(ctx context.Context, req codersdk.CreateUserRequest)
626640
})
627641
}
628642

629-
func convertUser(user database.User) codersdk.User {
643+
func convertUser(user database.User, organizations []database.Organization) codersdk.User {
644+
orgIds := make([]uuid.UUID, 0, len(organizations))
645+
for _, o := range organizations {
646+
orgIds = append(orgIds, o.ID)
647+
}
648+
630649
return codersdk.User{
631-
ID: user.ID,
632-
Email: user.Email,
633-
CreatedAt: user.CreatedAt,
634-
Username: user.Username,
635-
Status: codersdk.UserStatus(user.Status),
650+
ID: user.ID,
651+
Email: user.Email,
652+
CreatedAt: user.CreatedAt,
653+
Username: user.Username,
654+
Status: codersdk.UserStatus(user.Status),
655+
OrganizationIds: orgIds,
636656
}
637657
}
638658

639-
func convertUsers(users []database.User) []codersdk.User {
659+
func convertUsers(users []database.User, organizationsByUserId OrganizationsByUserId) []codersdk.User {
640660
converted := make([]codersdk.User, 0, len(users))
641661
for _, u := range users {
642-
converted = append(converted, convertUser(u))
662+
userOrganizations := organizationsByUserId[u.ID.String()]
663+
converted = append(converted, convertUser(u, userOrganizations))
643664
}
644665
return converted
645666
}
667+
668+
func getUserOrganizations(api *api, rw http.ResponseWriter, r *http.Request, user database.User) []database.Organization {
669+
organizations, err := api.Database.GetOrganizationsByUserID(r.Context(), user.ID)
670+
if errors.Is(err, sql.ErrNoRows) {
671+
err = nil
672+
organizations = []database.Organization{}
673+
}
674+
if err != nil {
675+
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
676+
Message: fmt.Sprintf("get organizations: %s", err.Error()),
677+
})
678+
return []database.Organization{}
679+
}
680+
return organizations
681+
}

coderd/users_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,11 @@ func TestPutUserSuspend(t *testing.T) {
321321
func TestUserByName(t *testing.T) {
322322
t.Parallel()
323323
client := coderdtest.New(t, nil)
324-
_ = coderdtest.CreateFirstUser(t, client)
325-
_, err := client.User(context.Background(), codersdk.Me)
324+
firstUser := coderdtest.CreateFirstUser(t, client)
325+
user, err := client.User(context.Background(), codersdk.Me)
326+
326327
require.NoError(t, err)
328+
require.Equal(t, firstUser.OrganizationID, user.OrganizationIds[0])
327329
}
328330

329331
func TestGetUsers(t *testing.T) {
@@ -340,6 +342,7 @@ func TestGetUsers(t *testing.T) {
340342
users, err := client.Users(context.Background(), codersdk.UsersRequest{})
341343
require.NoError(t, err)
342344
require.Len(t, users, 2)
345+
require.Len(t, users[0].OrganizationIds, 1)
343346
}
344347

345348
func TestOrganizationsByUser(t *testing.T) {

codersdk/users.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ const (
3737

3838
// User represents a user in Coder.
3939
type User struct {
40-
ID uuid.UUID `json:"id" validate:"required"`
41-
Email string `json:"email" validate:"required"`
42-
CreatedAt time.Time `json:"created_at" validate:"required"`
43-
Username string `json:"username" validate:"required"`
44-
Status UserStatus `json:"status"`
40+
ID uuid.UUID `json:"id" validate:"required"`
41+
Email string `json:"email" validate:"required"`
42+
CreatedAt time.Time `json:"created_at" validate:"required"`
43+
Username string `json:"username" validate:"required"`
44+
Status UserStatus `json:"status"`
45+
OrganizationIds []uuid.UUID `json:"organization_ids"`
4546
}
4647

4748
type CreateFirstUserRequest struct {

0 commit comments

Comments
 (0)