From b5788818e9b9065700cd4f36ac11fe78520f327a Mon Sep 17 00:00:00 2001 From: Joshua Tan Date: Mon, 13 May 2024 22:38:57 +0800 Subject: [PATCH 1/2] feat: get memberships of current user --- backend/src/server/handlers.go | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/backend/src/server/handlers.go b/backend/src/server/handlers.go index b77dba5b..45770c22 100644 --- a/backend/src/server/handlers.go +++ b/backend/src/server/handlers.go @@ -80,6 +80,7 @@ func (s *ServerHandler) registerRoutes(r *mux.Router) { r.Handle("/api/organization", isAuthenticated(getOrgIdFromRequestBody(isOrgOwner(s.psqlClient, handleDeleteOrganization(s.logger, s.psqlClient), s.logger), s.logger), s.logger)).Methods("DELETE").Headers("Content-Type", "application/json") // 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") @@ -908,3 +909,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) + }) +} From 78de5a1e4a365b33433a976ef8af723d7cbead00 Mon Sep 17 00:00:00 2001 From: Joshua Tan Date: Mon, 13 May 2024 22:39:18 +0800 Subject: [PATCH 2/2] feat: get membership api request --- flowforge_api_bruno/user/user by id.bru | 2 +- flowforge_api_bruno/user/user memberships.bru | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 flowforge_api_bruno/user/user memberships.bru diff --git a/flowforge_api_bruno/user/user by id.bru b/flowforge_api_bruno/user/user by id.bru index 8697dd34..6f09c714 100644 --- a/flowforge_api_bruno/user/user by id.bru +++ b/flowforge_api_bruno/user/user by id.bru @@ -7,5 +7,5 @@ meta { get { url: {{HOST}}/user/{{user_id}} body: none - auth: none + auth: inherit } diff --git a/flowforge_api_bruno/user/user memberships.bru b/flowforge_api_bruno/user/user memberships.bru new file mode 100644 index 00000000..0c22af6b --- /dev/null +++ b/flowforge_api_bruno/user/user memberships.bru @@ -0,0 +1,11 @@ +meta { + name: user memberships + type: http + seq: 2 +} + +get { + url: {{HOST}}/membership + body: none + auth: inherit +}