227 lines
5.5 KiB
Go
227 lines
5.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"gestion/db"
|
|
"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 {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Notifier tous les admins/cabines en temps réel
|
|
go database.NotifyAllAdminCabineAlert(alert.ID, usernameStr, req.Message)
|
|
|
|
c.JSON(200, 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
|
|
}
|
|
err = database.DeleteAlertPolicy(alertID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(200, 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.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(200, 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)
|
|
|
|
// Vérifier que l'alerte appartient bien à ce livreur
|
|
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
|
|
}
|
|
|
|
err = database.EndAlert(alertID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Impossible de mettre fin à l'alerte", "details": err.Error()})
|
|
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 {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Impossible de récupérer les alertes", "details": err.Error()})
|
|
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 {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Impossible de récupérer les alertes", "details": err.Error()})
|
|
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 {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Impossible de récupérer les alertes", "details": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"alerts": alerts,
|
|
"count": len(alerts),
|
|
})
|
|
}
|