166 lines
3.6 KiB
Go
166 lines
3.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"gestion/db"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func GetLivreurPosition(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
livreurUsername := c.Param("username")
|
|
|
|
userRole := c.GetString("role")
|
|
if userRole != "admin" && userRole != "cabine" {
|
|
c.JSON(http.StatusForbidden, gin.H{
|
|
"error": "Accès refusé - Réservé aux administrateurs et cabines",
|
|
})
|
|
return
|
|
}
|
|
|
|
if livreurUsername == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Username livreur requis"})
|
|
return
|
|
}
|
|
|
|
position, err := database.GetLivreurPosition(livreurUsername)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"livreur": livreurUsername,
|
|
"message": "Position non disponible (GPS désactivé ou livraison terminée)",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"livreur": livreurUsername,
|
|
"position": position,
|
|
})
|
|
}
|
|
|
|
func GetDeliveryIssues(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
status := c.Query("status")
|
|
|
|
userRole := c.GetString("role")
|
|
if userRole != "admin" && userRole != "cabine" {
|
|
c.JSON(http.StatusForbidden, gin.H{
|
|
"error": "Accès refusé - Réservé aux administrateurs et cabines",
|
|
})
|
|
return
|
|
}
|
|
issues, err := database.GetDeliveryIssues(status)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "Erreur récupération problèmes",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"issues": issues,
|
|
"count": len(issues),
|
|
})
|
|
}
|
|
|
|
func CreateDeliveryIssue(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
var req struct {
|
|
CommandID int `json:"command_id" binding:"required"`
|
|
IssueType string `json:"issue_type" binding:"required"`
|
|
Description string `json:"description" binding:"required"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Données invalides"})
|
|
return
|
|
}
|
|
|
|
cabineUsername, _ := c.Get("username")
|
|
|
|
issue, err := database.CreateDeliveryIssue(
|
|
req.CommandID,
|
|
req.IssueType,
|
|
req.Description,
|
|
cabineUsername.(string),
|
|
)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "Erreur création problème",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
"success": true,
|
|
"message": "Problème enregistré",
|
|
"issue": issue,
|
|
})
|
|
}
|
|
|
|
func UpdateDeliveryIssue(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
issueID, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalide"})
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Status string `json:"status"`
|
|
Resolution string `json:"resolution"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Données invalides"})
|
|
return
|
|
}
|
|
|
|
cabineUsername, _ := c.Get("username")
|
|
|
|
err = database.UpdateDeliveryIssue(issueID, req.Status, req.Resolution, cabineUsername.(string))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "Erreur mise à jour",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "Problème mis à jour",
|
|
})
|
|
}
|
|
|
|
func GetCommandLogs(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
commandID, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID commande invalide"})
|
|
return
|
|
}
|
|
|
|
logs, err := database.GetCommandLogs(commandID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "Erreur récupération logs",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"logs": logs,
|
|
"count": len(logs),
|
|
})
|
|
}
|