feat(backend): add list orgs endpoint

This commit is contained in:
2026-01-02 16:57:20 +00:00
parent b40a80e4b6
commit efd93f099e
4 changed files with 69 additions and 5 deletions

View File

@@ -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) { t.Run("accounts/:id", func(t *testing.T) {
var got struct { var got struct {
ID string `json:"id"` ID string `json:"id"`

View File

@@ -124,8 +124,9 @@ func NewServer(c Config) (*Server, error) {
api := app.Group("/api") api := app.Group("/api")
auth.NewHTTPHandler(authService, organizationService, db, cookieConfig).RegisterRoutes(api) auth.NewHTTPHandler(authService, organizationService, db, cookieConfig).RegisterRoutes(api)
registration.NewHTTPHandler(registrationService, authService, 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) account.NewHTTPHandler(accountService, db, authMiddleware).RegisterRoutes(api)
organization.NewHTTPHandler(organizationService, db, authMiddleware).RegisterRoutes(usersAPI)
orgAPI := api.Group("/:orgSlug", authMiddleware, organization.NewMiddleware(organizationService, accountService, db)) orgAPI := api.Group("/:orgSlug", authMiddleware, organization.NewMiddleware(organizationService, accountService, db))

View 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)
}

View File

@@ -16,10 +16,11 @@ func NewHTTPHandler(service *Service, db *bun.DB, authMiddleware fiber.Handler)
return &HTTPHandler{service: service, db: db, authMiddleware: authMiddleware} return &HTTPHandler{service: service, db: db, authMiddleware: authMiddleware}
} }
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) { func (h *HTTPHandler) RegisterRoutes(api fiber.Router) fiber.Router {
user := api.Group("/users") users := api.Group("/users")
user.Use(h.authMiddleware) users.Use(h.authMiddleware)
user.Get("/me", h.getAuthenticatedUser) users.Get("/me", h.getAuthenticatedUser)
return users
} }
// getAuthenticatedUser returns the currently authenticated user // getAuthenticatedUser returns the currently authenticated user