feat: add and delete amende and point by admin

This commit is contained in:
2026-04-22 14:00:43 +02:00
parent 8f617e04d1
commit 7605fce9f4
5 changed files with 323 additions and 2 deletions
@@ -975,6 +975,86 @@ func ResetClientPenaltiesAdmin(c *gin.Context) {
})
}
// AddClientPointsAdmin ajoute des points à un client dans un pool donné (Admin/Cabine)
// POST /api/v2/admin/protected/client/:username/points/add
// Body: {"pool_key": "pool_0", "points": 10}
func AddClientPointsAdmin(c *gin.Context) {
userRole := c.GetString("role")
if userRole != "admin" && userRole != "cabine" {
c.JSON(http.StatusForbidden, gin.H{"error": "Accès refusé"})
return
}
username := c.Param("username")
if username == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Username requis"})
return
}
var req struct {
PoolKey string `json:"pool_key" binding:"required"`
Points int `json:"points" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Données invalides"})
return
}
if req.Points <= 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Le nombre de points doit être positif"})
return
}
database := c.MustGet("database").(*db.Database)
// Vérifier que le pool_key existe dans les settings
settings, err := database.GetSettings()
if err != nil {
utils.ServerErr(c, "Erreur récupération paramètres", err)
return
}
poolExists := false
for _, pool := range settings.PointsPools {
if pool.Key == req.PoolKey {
poolExists = true
break
}
}
if !poolExists {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Pool de points invalide",
"pools_valides": func() []string {
keys := make([]string, 0, len(settings.PointsPools))
for _, p := range settings.PointsPools {
keys = append(keys, p.Key)
}
return keys
}(),
})
return
}
if err := database.AddClientPointsByCategory(username, req.Points, req.PoolKey); err != nil {
if strings.Contains(err.Error(), "non trouvé") {
c.JSON(http.StatusNotFound, gin.H{"error": "Client non trouvé"})
} else {
utils.ServerErr(c, "Erreur ajout de points", err)
}
return
}
log.Printf("✅ [ADD_POINTS] %d points (pool=%s) ajoutés à %s par %s",
req.Points, req.PoolKey, username, c.GetString("username"))
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "Points ajoutés avec succès",
"username": username,
"pool_key": req.PoolKey,
"points": req.Points,
})
}
// ============================================
// STATISTIQUES TEMPS RÉEL
// ============================================