chore: refacto
This commit is contained in:
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"fmt"
|
||||
"gestion/db"
|
||||
"gestion/utils"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -42,9 +43,6 @@ func checkRateLimit(key string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// GESTION ADRESSE & ADMIN
|
||||
// ============================================
|
||||
func safeGetUsername(c *gin.Context) (string, error) {
|
||||
username, exists := c.Get("username")
|
||||
if !exists {
|
||||
@@ -71,13 +69,11 @@ func validateAddress(address string) error {
|
||||
}
|
||||
|
||||
// UpdateCommandAddress met à jour l'adresse de livraison d'une commande
|
||||
// PUT /api/v1/admin/commands/:id/address
|
||||
func UpdateCommandAddress(c *gin.Context) {
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
|
||||
// ✅ Vérification du rôle
|
||||
if c.GetString("role") != "admin" {
|
||||
log.Printf("❌ [UPD_ADDR] Accès refusé - role=%s", c.GetString("role"))
|
||||
userRole := c.GetString("role")
|
||||
if !utils.CheckRoleAdmin(c, userRole) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Accès refusé"})
|
||||
return
|
||||
}
|
||||
@@ -111,13 +107,11 @@ func UpdateCommandAddress(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// ✅ Validation de l'adresse
|
||||
if err := validateAddress(req.DeliveryAddress); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// ✅ Logs sanitizés
|
||||
log.Printf("📝 [UPD_ADDR] Admin %s modifie cmd %d", adminUsername, commandID)
|
||||
|
||||
command, err := database.GetCommandByID(commandID)
|
||||
@@ -156,13 +150,11 @@ func UpdateCommandAddress(c *gin.Context) {
|
||||
}
|
||||
|
||||
// ProposeAddressChange propose une nouvelle adresse au client pour validation
|
||||
// POST /api/v2/admin/protected/orders/:id/propose-address
|
||||
// POST /api/v1/cabine/commands/:id/propose-address
|
||||
func ProposeAddressChange(c *gin.Context) {
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
|
||||
userRole := c.GetString("role")
|
||||
if userRole != "admin" && userRole != "cabine" {
|
||||
if !utils.CheckRoleAdmin(c, userRole) && !utils.CheckRoleCabine(c, userRole) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Accès refusé"})
|
||||
return
|
||||
}
|
||||
@@ -218,7 +210,6 @@ func ProposeAddressChange(c *gin.Context) {
|
||||
database.NotifyClient(clientUsername, commandID, "address_proposal", msg)
|
||||
}
|
||||
|
||||
log.Printf("✅ [PROPOSE_ADDR] Commande %d - nouvelle adresse proposée par %s", commandID, staffUsername)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "Nouvelle adresse proposée au client",
|
||||
@@ -231,6 +222,11 @@ func ProposeAddressChange(c *gin.Context) {
|
||||
func RespondToAddressProposal(c *gin.Context) {
|
||||
database := c.MustGet("database").(*db.Database)
|
||||
|
||||
userRole := c.GetString("role")
|
||||
if !utils.CheckRoleClient(c, userRole) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Accès refusé"})
|
||||
return
|
||||
}
|
||||
clientUsername, err := safeGetUsername(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentification requise"})
|
||||
@@ -276,8 +272,7 @@ func GetAllCommands(c *gin.Context) {
|
||||
username := c.Query("username")
|
||||
|
||||
userRole := c.GetString("role")
|
||||
if userRole != "admin" && userRole != "cabine" {
|
||||
log.Printf("❌ [VALIDATE] Accès refusé - role=%s", userRole)
|
||||
if !utils.CheckRoleAdmin(c, userRole) && !utils.CheckRoleCabine(c, userRole) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Accès refusé"})
|
||||
return
|
||||
}
|
||||
@@ -725,8 +720,7 @@ func GetClientCommandsHistory(c *gin.Context) {
|
||||
} else if client == nil {
|
||||
log.Printf("⚠️ [HISTORY] client est NIL!")
|
||||
} else {
|
||||
log.Printf("✅ [HISTORY] Client récupéré: username=%s, point=%d, point_zipette=%d",
|
||||
client.Username, client.Point, client.PointZipette)
|
||||
log.Printf("✅ [HISTORY] Client récupéré: username=%s", client.Username)
|
||||
}
|
||||
|
||||
resp := gin.H{
|
||||
@@ -760,7 +754,6 @@ func GetClientCommandsHistory(c *gin.Context) {
|
||||
"prenom": client.Prenom,
|
||||
"telephone": client.Telephone,
|
||||
"total_commands": client.Command,
|
||||
"points": client.Point,
|
||||
"pool_points": poolPoints,
|
||||
"pool_names": poolNames,
|
||||
"penalties": client.Amende,
|
||||
|
||||
Reference in New Issue
Block a user