appresult.Localize applies to all /api/admin routes, so each handler's godoc now declares lang (tm, ru, en, all; default tm) and Swagger UI offers it as a dropdown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
169 lines
4.8 KiB
Go
169 lines
4.8 KiB
Go
package activitylog
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"online-back/internal/appresult"
|
|
"online-back/internal/handlers"
|
|
"online-back/internal/identity/rbac"
|
|
"online-back/internal/identity/rbac/perm"
|
|
"online-back/internal/shared"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
const baseURL = "/activity-logs"
|
|
|
|
type handler struct {
|
|
logger *logging.Logger
|
|
repository Repository
|
|
}
|
|
|
|
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
|
return &handler{logger: logger, repository: repository}
|
|
}
|
|
|
|
// Register only wires read endpoints: activity log entries are written by
|
|
// the Record middleware, never through the API.
|
|
func (h *handler) Register(router *gin.RouterGroup) {
|
|
group := rbac.Group(router, baseURL, rbac.RequireStaff(perm.ActivityLogs))
|
|
group.GET("", h.list)
|
|
group.GET("/:id", h.getByID)
|
|
}
|
|
|
|
// list godoc
|
|
// @Summary List activity logs
|
|
// @Description Returns a paginated audit trail of user actions (create/update/delete). Each log includes the actor's username and role code.
|
|
// @Tags activity-logs
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param user_id query int false "Filter by user ID"
|
|
// @Param module query string false "Filter by module (e.g. cards, branch-users)"
|
|
// @Param action query string false "Filter by action (create, update, delete)"
|
|
// @Param page query int false "Page number" default(1)
|
|
// @Param limit query int false "Items per page" default(10)
|
|
// @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm)
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /activity-logs [get]
|
|
func (h *handler) list(c *gin.Context) {
|
|
filter, ok := parseListFilter(c)
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
result, err := h.repository.List(c.Request.Context(), filter)
|
|
if err != nil {
|
|
h.logger.Errorf("list activity logs failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
|
}
|
|
|
|
// getByID godoc
|
|
// @Summary Get activity log by ID
|
|
// @Description Returns a single activity log entry
|
|
// @Tags activity-logs
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Activity log ID"
|
|
// @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm)
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /activity-logs/{id} [get]
|
|
func (h *handler) getByID(c *gin.Context) {
|
|
id, ok := shared.ParseID(c.Param("id"))
|
|
if !ok {
|
|
// GET /activity-logs/card-orders → list that module instead of 400.
|
|
q := c.Request.URL.Query()
|
|
if q.Get("module") == "" {
|
|
q.Set("module", c.Param("id"))
|
|
c.Request.URL.RawQuery = q.Encode()
|
|
}
|
|
h.list(c)
|
|
return
|
|
}
|
|
|
|
item, err := h.repository.GetByID(c.Request.Context(), id)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.logger.Errorf("get activity log failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
|
filter := ListFilter{
|
|
Page: shared.DefaultPage,
|
|
Limit: shared.DefaultLimit,
|
|
}
|
|
|
|
if id, ok := optionalQueryID(c, "user_id", "userId"); !ok {
|
|
return ListFilter{}, false
|
|
} else if id != nil {
|
|
filter.UserId = id
|
|
}
|
|
|
|
filter.Module = queryValue(c, "module")
|
|
filter.Action = queryValue(c, "action")
|
|
|
|
if raw := queryValue(c, "page"); raw != "" {
|
|
page, err := strconv.Atoi(raw)
|
|
if err != nil || page < 1 {
|
|
return ListFilter{}, false
|
|
}
|
|
filter.Page = page
|
|
}
|
|
|
|
if raw := queryValue(c, "limit"); raw != "" {
|
|
limit, err := strconv.Atoi(raw)
|
|
if err != nil || limit < 1 {
|
|
return ListFilter{}, false
|
|
}
|
|
if limit > shared.MaxLimit {
|
|
limit = shared.MaxLimit
|
|
}
|
|
filter.Limit = limit
|
|
}
|
|
|
|
return filter, true
|
|
}
|
|
|
|
func queryValue(c *gin.Context, keys ...string) string {
|
|
for _, key := range keys {
|
|
if raw := strings.TrimSpace(c.Query(key)); raw != "" && raw != "undefined" && raw != "null" {
|
|
return raw
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func optionalQueryID(c *gin.Context, keys ...string) (*int64, bool) {
|
|
raw := queryValue(c, keys...)
|
|
if raw == "" {
|
|
return nil, true
|
|
}
|
|
id, ok := shared.ParseID(raw)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
return &id, true
|
|
}
|