224 lines
5.2 KiB
Go
224 lines
5.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"gestion/db"
|
|
"gestion/utils"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func AlertPolice(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
username, exists := c.Get("username")
|
|
if !exists {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Non authentifié"})
|
|
return
|
|
}
|
|
|
|
userRole := c.GetString("role")
|
|
if userRole != "livreur" {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Accès réservé aux livreurs"})
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Message string `json:"message"`
|
|
}
|
|
// message optionnel — on ignore l'erreur de bind
|
|
_ = c.ShouldBindJSON(&req)
|
|
|
|
usernameStr := username.(string)
|
|
alert, err := database.CreateAlert(usernameStr, req.Message)
|
|
if err != nil {
|
|
utils.ServerErr(c, "Impossible de créer l'alerte", err)
|
|
return
|
|
}
|
|
|
|
go database.NotifyAllAdminCabineAlert(alert.ID, usernameStr, req.Message)
|
|
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
"success": true,
|
|
"message": "Police alert created",
|
|
"alert_id": alert.ID,
|
|
"user": alert.Username,
|
|
})
|
|
}
|
|
|
|
func DeleteAlert(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
alertIDStr := c.Param("id")
|
|
alertID, err := strconv.Atoi(alertIDStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID d'alerte invalide"})
|
|
return
|
|
}
|
|
userRole := c.GetString("role")
|
|
if userRole != "livreur" && userRole != "admin" {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Accès réservé aux livreurs"})
|
|
return
|
|
}
|
|
if err = database.DeleteAlertPolicy(alertID); err != nil {
|
|
utils.ServerErr(c, "Impossible de supprimer l'alerte", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "Alert deleted",
|
|
})
|
|
}
|
|
|
|
func GetAlert(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
alertIDStr := c.Param("id")
|
|
alertID, err := strconv.Atoi(alertIDStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID d'alerte invalide"})
|
|
return
|
|
}
|
|
userRole := c.GetString("role")
|
|
if userRole != "livreur" && userRole != "admin" && userRole != "cabine" {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Accès réservé aux livreurs et aux administrateurs"})
|
|
return
|
|
}
|
|
alert, err := database.GetAlertPolicy(alertID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Alerte non trouvée"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"alert": alert,
|
|
})
|
|
}
|
|
|
|
// EndAlert permet au livreur de mettre fin à son alerte active
|
|
func EndAlert(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
username, exists := c.Get("username")
|
|
if !exists {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Non authentifié"})
|
|
return
|
|
}
|
|
|
|
userRole := c.GetString("role")
|
|
if userRole != "livreur" {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Accès réservé aux livreurs"})
|
|
return
|
|
}
|
|
|
|
alertIDStr := c.Param("id")
|
|
alertID, err := strconv.Atoi(alertIDStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID d'alerte invalide"})
|
|
return
|
|
}
|
|
|
|
usernameStr := username.(string)
|
|
|
|
alert, err := database.GetAlertPolicy(alertID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Alerte non trouvée"})
|
|
return
|
|
}
|
|
|
|
if alert.Username != usernameStr {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Cette alerte ne vous appartient pas"})
|
|
return
|
|
}
|
|
|
|
if err = database.EndAlert(alertID); err != nil {
|
|
utils.ServerErr(c, "Impossible de mettre fin à l'alerte", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "Alerte terminée avec succès",
|
|
"alert_id": alertID,
|
|
"user": usernameStr,
|
|
})
|
|
}
|
|
|
|
func GetMyAlerts(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
username, exists := c.Get("username")
|
|
if !exists {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Non authentifié"})
|
|
return
|
|
}
|
|
|
|
userRole := c.GetString("role")
|
|
if userRole != "livreur" {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Accès réservé aux livreurs"})
|
|
return
|
|
}
|
|
|
|
usernameStr := username.(string)
|
|
|
|
alerts, err := database.GetAlertsByUsername(usernameStr)
|
|
if err != nil {
|
|
utils.ServerErr(c, "Impossible de récupérer les alertes", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"alerts": alerts,
|
|
"count": len(alerts),
|
|
"user": usernameStr,
|
|
})
|
|
}
|
|
|
|
func GetAllAlerts(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
userRole := c.GetString("role")
|
|
if userRole != "admin" && userRole != "cabine" {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Accès réservé aux administrateurs"})
|
|
return
|
|
}
|
|
|
|
alerts, err := database.GetAllAlerts()
|
|
if err != nil {
|
|
utils.ServerErr(c, "Impossible de récupérer les alertes", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"alerts": alerts,
|
|
"count": len(alerts),
|
|
})
|
|
}
|
|
|
|
func GetActiveAlerts(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
userRole := c.GetString("role")
|
|
if userRole != "admin" && userRole != "cabine" {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "Accès réservé aux administrateurs"})
|
|
return
|
|
}
|
|
|
|
alerts, err := database.GetActiveAlerts()
|
|
if err != nil {
|
|
utils.ServerErr(c, "Impossible de récupérer les alertes", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"alerts": alerts,
|
|
"count": len(alerts),
|
|
})
|
|
}
|