Files
online-backend/internal/dashboard/handler.go
GuwandykA 28509eb4ca
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
Add backups, currencies and dashboard APIs, and scope access by guard_name.
Web users may only CRUD their own records, operators stay in assigned branches, and admin/manager/superadmin keep full access.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-31 18:14:47 +05:00

94 lines
2.4 KiB
Go

package dashboard
import (
"net/http"
"strconv"
"time"
"online-back/internal/appresult"
"online-back/internal/handlers"
"online-back/internal/identity/rbac"
"online-back/pkg/logging"
"github.com/gin-gonic/gin"
)
const baseURL = "/dashboard"
const (
defaultRecentLimit = 10
maxRecentLimit = 50
)
type handler struct {
logger *logging.Logger
repository Repository
}
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
return &handler{logger: logger, repository: repository}
}
func (h *handler) Register(router *gin.RouterGroup) {
group := router.Group(baseURL, appresult.JwtTokenCheck())
group.GET("/overview", h.overview)
}
// overview godoc
// @Summary Dashboard overview
// @Description Returns the KPI cards, loan/card trend, loan status breakdown, weekly order counts, and recent activity feed for the admin dashboard
// @Tags dashboard
// @Produce json
// @Security BearerAuth
// @Param year query int false "Year for the monthly trend chart" default(current year)
// @Param recent_limit query int false "Number of recent activity items to return" default(10)
// @Success 200 {object} appresult.AppSuccess
// @Failure 400 {object} appresult.AppError
// @Failure 500 {object} appresult.AppError
// @Router /dashboard/overview [get]
func (h *handler) overview(c *gin.Context) {
filter, ok := parseOverviewFilter(c)
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
result, err := h.repository.Overview(c.Request.Context(), filter)
if err != nil {
h.logger.Errorf("dashboard overview failed: %v", err)
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
}
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
}
func parseOverviewFilter(c *gin.Context) (OverviewFilter, bool) {
filter := OverviewFilter{
Year: time.Now().Year(),
RecentLimit: defaultRecentLimit,
}
if raw := c.Query("year"); raw != "" {
year, err := strconv.Atoi(raw)
if err != nil || year < 2000 || year > 2100 {
return OverviewFilter{}, false
}
filter.Year = year
}
if raw := c.Query("recent_limit"); raw != "" {
limit, err := strconv.Atoi(raw)
if err != nil || limit < 1 {
return OverviewFilter{}, false
}
if limit > maxRecentLimit {
limit = maxRecentLimit
}
filter.RecentLimit = limit
}
rbac.BindBranchIDs(c, &filter.BranchIds)
return filter, true
}