feat(backend): add individual org query endpoint

This commit is contained in:
2026-01-02 19:31:25 +00:00
parent efd93f099e
commit 37056db77f
3 changed files with 131 additions and 4 deletions

View File

@@ -17,6 +17,7 @@ import (
"github.com/get-drexa/drexa/internal/database" "github.com/get-drexa/drexa/internal/database"
"github.com/get-drexa/drexa/internal/organization" "github.com/get-drexa/drexa/internal/organization"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/testcontainers/testcontainers-go/modules/postgres" "github.com/testcontainers/testcontainers-go/modules/postgres"
) )
@@ -187,6 +188,58 @@ func TestRegistrationFlow(t *testing.T) {
} }
}) })
t.Run("organizations/:orgSlug", func(t *testing.T) {
var myOrg struct {
ID string `json:"id"`
Kind string `json:"kind"`
Name string `json:"name"`
Slug string `json:"slug"`
}
doJSON(t, s.app, http.MethodGet, "/api/organizations/my", reg.AccessToken, nil, http.StatusOK, &myOrg)
if myOrg.Kind != string(organization.KindPersonal) {
t.Fatalf("unexpected personal org kind: %q", myOrg.Kind)
}
orgID, err := uuid.NewV7()
if err != nil {
t.Fatalf("uuid: %v", err)
}
accountID, err := uuid.NewV7()
if err != nil {
t.Fatalf("uuid: %v", err)
}
_, err = s.db.ExecContext(ctx,
`INSERT INTO organizations (id, kind, name, slug) VALUES (?, ?, ?, ?)`,
orgID, organization.KindTeam, "Acme", "acme",
)
if err != nil {
t.Fatalf("insert org: %v", err)
}
_, err = s.db.ExecContext(ctx,
`INSERT INTO accounts (id, org_id, user_id, role, status) VALUES (?, ?, ?, ?, ?)`,
accountID, orgID, reg.User.ID, "member", "active",
)
if err != nil {
t.Fatalf("insert account: %v", err)
}
var got struct {
ID string `json:"id"`
Kind string `json:"kind"`
Name string `json:"name"`
Slug string `json:"slug"`
}
doJSON(t, s.app, http.MethodGet, "/api/organizations/acme", reg.AccessToken, nil, http.StatusOK, &got)
if got.Slug != "acme" {
t.Fatalf("unexpected org slug: %q", got.Slug)
}
if got.Kind != string(organization.KindTeam) {
t.Fatalf("unexpected org kind: %q", got.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

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

View File

@@ -1,6 +1,10 @@
package organization package organization
import ( import (
"errors"
"strings"
"github.com/get-drexa/drexa/internal/account"
"github.com/get-drexa/drexa/internal/httperr" "github.com/get-drexa/drexa/internal/httperr"
"github.com/get-drexa/drexa/internal/reqctx" "github.com/get-drexa/drexa/internal/reqctx"
"github.com/get-drexa/drexa/internal/user" "github.com/get-drexa/drexa/internal/user"
@@ -10,18 +14,29 @@ import (
type HTTPHandler struct { type HTTPHandler struct {
service *Service service *Service
accountService *account.Service
db *bun.DB db *bun.DB
authMiddleware fiber.Handler authMiddleware fiber.Handler
} }
func NewHTTPHandler(service *Service, db *bun.DB, authMiddleware fiber.Handler) *HTTPHandler { func NewHTTPHandler(service *Service, accountService *account.Service, db *bun.DB, authMiddleware fiber.Handler) *HTTPHandler {
return &HTTPHandler{service: service, db: db, authMiddleware: authMiddleware} return &HTTPHandler{service: service, accountService: accountService, db: db, authMiddleware: authMiddleware}
} }
func (h *HTTPHandler) RegisterRoutes(users fiber.Router) { // RegisterUserRoutes mounts user-scoped organization routes onto an existing
// `/users` router group (which should already be protected by auth middleware).
func (h *HTTPHandler) RegisterUserRoutes(users fiber.Router) {
users.Get("/me/organizations", h.listAuthenticatedUserOrganizations) users.Get("/me/organizations", h.listAuthenticatedUserOrganizations)
} }
// RegisterRoutes mounts organization routes under `/organizations` and applies
// auth middleware to the group.
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) {
orgs := api.Group("/organizations")
orgs.Use(h.authMiddleware)
orgs.Get("/:orgSlug", h.getOrganizationBySlug)
}
// listAuthenticatedUserOrganizations returns the organizations the current user is a member of // listAuthenticatedUserOrganizations returns the organizations the current user is a member of
// @Summary List current user's organizations // @Summary List current user's organizations
// @Description Retrieve the organizations the authenticated user belongs to // @Description Retrieve the organizations the authenticated user belongs to
@@ -44,3 +59,59 @@ func (h *HTTPHandler) listAuthenticatedUserOrganizations(c *fiber.Ctx) error {
return c.JSON(orgs) return c.JSON(orgs)
} }
// getOrganizationBySlug returns an organization by slug (only if the user is a member)
// @Summary Get organization by slug
// @Description Retrieve organization information by slug (membership required)
// @Tags organizations
// @Produce json
// @Param orgSlug path string true "Organization slug"
// @Security BearerAuth
// @Success 200 {object} Organization "Organization"
// @Failure 401 {string} string "Not authenticated"
// @Failure 404 {string} string "Organization not found"
// @Router /organizations/{orgSlug} [get]
func (h *HTTPHandler) getOrganizationBySlug(c *fiber.Ctx) error {
u, _ := reqctx.AuthenticatedUser(c).(*user.User)
if u == nil {
return c.SendStatus(fiber.StatusUnauthorized)
}
rawSlug := strings.ToLower(strings.TrimSpace(c.Params("orgSlug")))
if rawSlug == ReservedSlug {
org, err := h.service.PersonalOrganizationForUser(c.Context(), h.db, u.ID)
if err != nil {
if errors.Is(err, ErrOrganizationNotFound) {
return c.SendStatus(fiber.StatusNotFound)
}
return httperr.Internal(err)
}
return c.JSON(org)
}
slug, err := NormalizeSlug(rawSlug)
if err != nil {
return c.SendStatus(fiber.StatusNotFound)
}
org, err := h.service.OrganizationBySlug(c.Context(), h.db, slug)
if err != nil {
if errors.Is(err, ErrOrganizationNotFound) {
return c.SendStatus(fiber.StatusNotFound)
}
return httperr.Internal(err)
}
acc, err := h.accountService.FindUserAccountInOrg(c.Context(), h.db, org.ID, u.ID)
if err != nil {
if errors.Is(err, account.ErrAccountNotFound) {
return c.SendStatus(fiber.StatusNotFound)
}
return httperr.Internal(err)
}
if acc.Status != account.StatusActive {
return c.SendStatus(fiber.StatusNotFound)
}
return c.JSON(org)
}