Skip to content

Commit

Permalink
Merge pull request #141 from joshtyf/feat/get-memberships-api
Browse files Browse the repository at this point in the history
Get current user memberships API
  • Loading branch information
joshtyf authored May 19, 2024
2 parents db6c32b + 78de5a1 commit 7390171
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
35 changes: 35 additions & 0 deletions backend/src/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (s *ServerHandler) registerRoutes(r *mux.Router) {
r.Handle("/api/organization/{orgId}/members", isAuthenticated(handleGetOrganizationMembers(s.logger, s.psqlClient), s.logger)).Methods("GET")

// Membership
r.Handle("/api/membership", isAuthenticated(handleGetMembershipsForUser(s.logger, s.psqlClient), s.logger)).Methods("GET")
r.Handle("/api/membership", isAuthenticated(getOrgIdFromRequestBody(validateMembershipChange(s.psqlClient, isOrgAdmin(s.psqlClient, handleCreateMembership(s.logger, s.psqlClient), s.logger), s.logger), s.logger), s.logger)).Methods("POST").Headers("Content-Type", "application/json")
r.Handle("/api/membership", isAuthenticated(getOrgIdFromRequestBody(validateMembershipChange(s.psqlClient, isOrgAdmin(s.psqlClient, handleUpdateMembership(s.logger, s.psqlClient), s.logger), s.logger), s.logger), s.logger)).Methods("PATCH").Headers("Content-Type", "application/json")
r.Handle("/api/membership", isAuthenticated(getOrgIdFromRequestBody(validateMembershipChange(s.psqlClient, isOrgOwner(s.psqlClient, handleDeleteMembership(s.logger, s.psqlClient), s.logger), s.logger), s.logger), s.logger)).Methods("DELETE").Headers("Content-Type", "application/json")
Expand Down Expand Up @@ -937,3 +938,37 @@ func handleDeleteOrganization(logger logger.ServerLogger, client *sql.DB) http.H
encode[any](w, r, http.StatusOK, nil)
})
}

func handleGetMembershipsForUser(logger logger.ServerLogger, client *sql.DB) http.Handler {
type ResponseBodyMembership struct {
OrgId int `json:"org_id"`
Role models.Role `json:"role"`
JoinedOn time.Time `json:"joined_on"`
}
type ResponseBody struct {
UserId string `json:"user_id"`
Memberships []ResponseBodyMembership `json:"memberships"`
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims)
userId := token.RegisteredClaims.Subject

memberships, err := database.NewMembership(client).GetUserMemberships(userId)
if err != nil {
logger.Error(fmt.Sprintf("error encountered while handling API request: %s", err))
encode(w, r, http.StatusInternalServerError, newHandlerError(ErrMembershipRetrieve, http.StatusInternalServerError))
return
}
response := ResponseBody{
UserId: userId,
}
for _, membership := range memberships {
response.Memberships = append(response.Memberships, ResponseBodyMembership{
OrgId: membership.OrgId,
Role: membership.Role,
JoinedOn: membership.JoinedOn,
})
}
encode(w, r, http.StatusOK, response)
})
}
2 changes: 1 addition & 1 deletion flowforge_api_bruno/user/user by id.bru
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ meta {
get {
url: {{HOST}}/user/{{user_id}}
body: none
auth: none
auth: inherit
}
11 changes: 11 additions & 0 deletions flowforge_api_bruno/user/user memberships.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
meta {
name: user memberships
type: http
seq: 2
}

get {
url: {{HOST}}/membership
body: none
auth: inherit
}

0 comments on commit 7390171

Please sign in to comment.