mirror of
https://github.com/get-drexa/drive.git
synced 2026-02-02 10:21:16 +00:00
feat(backend): add list orgs endpoint
This commit is contained in:
@@ -171,6 +171,22 @@ func TestRegistrationFlow(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("users/me/organizations", func(t *testing.T) {
|
||||
var orgs []struct {
|
||||
ID string `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
doJSON(t, s.app, http.MethodGet, "/api/users/me/organizations", reg.AccessToken, nil, http.StatusOK, &orgs)
|
||||
if len(orgs) != 1 {
|
||||
t.Fatalf("expected 1 organization, got %d", len(orgs))
|
||||
}
|
||||
if orgs[0].Kind != string(organization.KindPersonal) {
|
||||
t.Fatalf("unexpected organization kind: %q", orgs[0].Kind)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("accounts/:id", func(t *testing.T) {
|
||||
var got struct {
|
||||
ID string `json:"id"`
|
||||
|
||||
@@ -124,8 +124,9 @@ func NewServer(c Config) (*Server, error) {
|
||||
api := app.Group("/api")
|
||||
auth.NewHTTPHandler(authService, organizationService, db, cookieConfig).RegisterRoutes(api)
|
||||
registration.NewHTTPHandler(registrationService, authService, db, cookieConfig).RegisterRoutes(api)
|
||||
user.NewHTTPHandler(userService, db, authMiddleware).RegisterRoutes(api)
|
||||
usersAPI := user.NewHTTPHandler(userService, db, authMiddleware).RegisterRoutes(api)
|
||||
account.NewHTTPHandler(accountService, db, authMiddleware).RegisterRoutes(api)
|
||||
organization.NewHTTPHandler(organizationService, db, authMiddleware).RegisterRoutes(usersAPI)
|
||||
|
||||
orgAPI := api.Group("/:orgSlug", authMiddleware, organization.NewMiddleware(organizationService, accountService, db))
|
||||
|
||||
|
||||
46
apps/backend/internal/organization/http.go
Normal file
46
apps/backend/internal/organization/http.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package organization
|
||||
|
||||
import (
|
||||
"github.com/get-drexa/drexa/internal/httperr"
|
||||
"github.com/get-drexa/drexa/internal/reqctx"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type HTTPHandler struct {
|
||||
service *Service
|
||||
db *bun.DB
|
||||
authMiddleware fiber.Handler
|
||||
}
|
||||
|
||||
func NewHTTPHandler(service *Service, db *bun.DB, authMiddleware fiber.Handler) *HTTPHandler {
|
||||
return &HTTPHandler{service: service, db: db, authMiddleware: authMiddleware}
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) RegisterRoutes(users fiber.Router) {
|
||||
users.Get("/me/organizations", h.listAuthenticatedUserOrganizations)
|
||||
}
|
||||
|
||||
// listAuthenticatedUserOrganizations returns the organizations the current user is a member of
|
||||
// @Summary List current user's organizations
|
||||
// @Description Retrieve the organizations the authenticated user belongs to
|
||||
// @Tags users
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {array} Organization "Array of organizations"
|
||||
// @Failure 401 {string} string "Not authenticated"
|
||||
// @Router /users/me/organizations [get]
|
||||
func (h *HTTPHandler) listAuthenticatedUserOrganizations(c *fiber.Ctx) error {
|
||||
u, _ := reqctx.AuthenticatedUser(c).(*user.User)
|
||||
if u == nil {
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
orgs, err := h.service.ListOrganizationsForUser(c.Context(), h.db, u.ID)
|
||||
if err != nil {
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
return c.JSON(orgs)
|
||||
}
|
||||
@@ -16,10 +16,11 @@ func NewHTTPHandler(service *Service, db *bun.DB, authMiddleware fiber.Handler)
|
||||
return &HTTPHandler{service: service, db: db, authMiddleware: authMiddleware}
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) {
|
||||
user := api.Group("/users")
|
||||
user.Use(h.authMiddleware)
|
||||
user.Get("/me", h.getAuthenticatedUser)
|
||||
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) fiber.Router {
|
||||
users := api.Group("/users")
|
||||
users.Use(h.authMiddleware)
|
||||
users.Get("/me", h.getAuthenticatedUser)
|
||||
return users
|
||||
}
|
||||
|
||||
// getAuthenticatedUser returns the currently authenticated user
|
||||
|
||||
Reference in New Issue
Block a user