145 lines
4.3 KiB
Go
145 lines
4.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"users_management/m/config"
|
|
"users_management/m/middleware"
|
|
"users_management/m/usecase"
|
|
"users_management/m/utils/common"
|
|
"users_management/m/utils/helper"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type ActivityLogController struct {
|
|
activityLogUC usecase.ActivityLogUseCase
|
|
rg *gin.RouterGroup
|
|
cfg *config.Config // Add config field for middleware
|
|
}
|
|
func NewActivityLogController(activityLogUC usecase.ActivityLogUseCase, rg *gin.RouterGroup, cfg *config.Config) *ActivityLogController {
|
|
return &ActivityLogController{
|
|
activityLogUC: activityLogUC,
|
|
rg: rg,
|
|
cfg: cfg, // Initialize config
|
|
}
|
|
}
|
|
|
|
|
|
func (c *ActivityLogController) Route() {
|
|
logs := c.rg.Group("/logs")
|
|
{
|
|
// Users can see their own logs (simplified response)
|
|
logs.GET("/my-logs", c.getMyLogs)
|
|
// Only admins and superadmins can see all logs (detailed response)
|
|
logs.GET("/all", middleware.ConditionalRequireAnyRole(c.cfg, "Admin", "SuperAdmin"), c.getAllLogs)
|
|
// Admins and superadmins can see teknisi logs specifically (detailed response)
|
|
logs.GET("/teknisi", middleware.ConditionalRequireAnyRole(c.cfg, "Admin", "SuperAdmin"), c.getTeknisinLogs)
|
|
}
|
|
}
|
|
|
|
func (c *ActivityLogController) getMyLogs(ctx *gin.Context) {
|
|
userID, exists := ctx.Get("userID")
|
|
if !exists {
|
|
common.ErrorResponses(ctx, http.StatusUnauthorized, "User ID not found")
|
|
return
|
|
}
|
|
|
|
uid, ok := userID.(uuid.UUID)
|
|
if !ok {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid user ID")
|
|
return
|
|
}
|
|
|
|
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
|
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
|
|
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if limit < 1 || limit > 100 {
|
|
limit = 10
|
|
}
|
|
|
|
logs, total, err := c.activityLogUC.GetUserLogs(uid, page, limit)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
// Convert to simplified response using helper
|
|
simplifiedLogs := helper.ConvertToActivityLogResponses(logs)
|
|
|
|
response := gin.H{
|
|
"logs": simplifiedLogs,
|
|
"total": total,
|
|
"page": page,
|
|
"limit": limit,
|
|
"total_pages": (total + int64(limit) - 1) / int64(limit),
|
|
}
|
|
|
|
common.SingleResponses(ctx, "User logs retrieved successfully", response)
|
|
}
|
|
|
|
func (c *ActivityLogController) getAllLogs(ctx *gin.Context) {
|
|
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
|
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
|
|
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if limit < 1 || limit > 100 {
|
|
limit = 10
|
|
}
|
|
|
|
logs, total, err := c.activityLogUC.GetAllLogs(page, limit)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
// Convert to detailed response for admins using helper
|
|
detailedLogs := helper.ConvertToActivityLogDetailResponses(logs)
|
|
|
|
response := gin.H{
|
|
"logs": detailedLogs,
|
|
"total": total,
|
|
"page": page,
|
|
"limit": limit,
|
|
"total_pages": (total + int64(limit) - 1) / int64(limit),
|
|
}
|
|
|
|
common.SingleResponses(ctx, "All logs retrieved successfully", response)
|
|
}
|
|
|
|
func (c *ActivityLogController) getTeknisinLogs(ctx *gin.Context) {
|
|
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
|
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
|
|
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if limit < 1 || limit > 100 {
|
|
limit = 10
|
|
}
|
|
|
|
logs, total, err := c.activityLogUC.GetTeknisinLogs(page, limit)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
// Convert to detailed response for admins using helper
|
|
detailedLogs := helper.ConvertToActivityLogDetailResponses(logs)
|
|
|
|
response := gin.H{
|
|
"logs": detailedLogs,
|
|
"total": total,
|
|
"page": page,
|
|
"limit": limit,
|
|
"total_pages": (total + int64(limit) - 1) / int64(limit),
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Teknisi logs retrieved successfully", response)
|
|
} |