saveinmed/backend/internal/http/handler/dashboard_handler.go
Gabbriiel 90467db1ec refactor: substitui backend Medusa por backend Go e corrige testes do marketplace
- Remove backend Medusa.js (TypeScript) e substitui pelo backend Go (saveinmed-performance-core)
- Corrige testes auth.test.ts: alinha paths de API (v1/ sem barra inicial) e campo access_token
- Corrige GroupedProductCard.test.tsx: ajusta distância formatada (toFixed) e troca userEvent por fireEvent com fakeTimers
- Corrige AuthContext.test.tsx: usa vi.hoisted() para mocks e corrige parênteses no waitFor

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 04:56:37 -06:00

51 lines
1.4 KiB
Go

package handler
import (
"errors"
"net/http"
"strings"
"github.com/gofrs/uuid/v5"
)
// GetDashboard returns the dashboard data based on user role (Admin or Seller).
// @Summary Get dashboard data
// @Description Get dashboard data for the authenticated user (Admin or Seller)
// @Tags Dashboard
// @Security BearerAuth
// @Produce json
// @Success 200 {object} map[string]interface{}
// @Failure 401 {object} map[string]string
// @Failure 403 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /api/v1/dashboard [get]
func (h *Handler) GetDashboard(w http.ResponseWriter, r *http.Request) {
req, err := getRequester(r)
if err != nil {
writeError(w, http.StatusUnauthorized, errors.New("Unauthorized"))
return
}
if strings.EqualFold(req.Role, "Admin") {
stats, err := h.svc.GetAdminDashboard(r.Context())
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, stats)
return
}
// Assume Seller/User - verify they have a company
if req.CompanyID == nil || *req.CompanyID == uuid.Nil {
writeError(w, http.StatusBadRequest, errors.New("user has no associated company"))
return
}
stats, err := h.svc.GetSellerDashboard(r.Context(), *req.CompanyID)
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, stats)
}