chore: build

This commit is contained in:
2026-06-14 17:50:35 +02:00
parent 3c92a0371f
commit 3a0f725159
93 changed files with 5311 additions and 4224 deletions
+98 -5
View File
@@ -6,6 +6,7 @@ import (
"gestion/services"
"log"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
@@ -91,9 +92,29 @@ func handleLinkAccount(c *gin.Context, token string, chatID int64) {
}
log.Printf("✅ [TELEGRAM_LINK] Compte %s (%s) lié au chat_id %d", username, role, chatID)
// Enrollment lbtelegram (best effort — n'empêche pas l'envoi du bouton)
if services.LBTelegram != nil && services.LBTelegram.IsConfigured() {
if err := services.LBTelegram.EnrollUser(chatID, username, role); err != nil {
log.Printf("⚠️ [LB] enrollment échoué pour %s: %v", username, err)
}
}
// Message de confirmation — bouton vers BOT1 si lbtelegram configuré, sinon texte simple
if services.TelegramBot != nil {
services.TelegramBot.SendMessage(chatID,
"✅ <b>Compte lié avec succès !</b>\n\nVous recevrez désormais vos notifications ici.")
if services.LBTelegram != nil && services.LBTelegram.Bot1Username != "" {
if err := services.TelegramBot.SendMessageWithButtons(chatID,
"✅ <b>Compte lié avec succès !</b>\n\nPour activer vos notifications, démarrez le bot ci-dessous :",
[][2]string{{"🔔 Activer les notifications", "https://t.me/" + services.LBTelegram.Bot1Username}},
); err != nil {
log.Printf("⚠️ [TELEGRAM] Envoi bouton BOT1 échoué pour %s: %v", username, err)
services.TelegramBot.SendMessage(chatID,
"✅ <b>Compte lié avec succès !</b>\n\nVous recevrez désormais vos notifications ici.")
}
} else {
services.TelegramBot.SendMessage(chatID,
"✅ <b>Compte lié avec succès !</b>\n\nVous recevrez désormais vos notifications ici.")
}
}
c.Status(http.StatusOK)
@@ -118,9 +139,11 @@ func GenerateClientLinkToken(c *gin.Context) {
return
}
botUsername := services.TelegramBot.BotUsername
c.JSON(http.StatusOK, gin.H{
"token": token,
"link_url": "https://t.me/" + services.TelegramBot.BotUsername + "?start=" + token,
"link_url": "https://t.me/" + botUsername + "?start=" + token,
"message": "/start " + token,
"expires_in": 600,
})
@@ -145,9 +168,11 @@ func GenerateLivreurLinkToken(c *gin.Context) {
return
}
botUsername := services.TelegramBot.BotUsername
c.JSON(http.StatusOK, gin.H{
"token": token,
"link_url": "https://t.me/" + services.TelegramBot.BotUsername + "?start=" + token,
"link_url": "https://t.me/" + botUsername + "?start=" + token,
"message": "/start " + token,
"expires_in": 600,
})
@@ -180,9 +205,11 @@ func GenerateAdminLinkToken(c *gin.Context) {
return
}
botUsername := services.TelegramBot.BotUsername
c.JSON(http.StatusOK, gin.H{
"token": token,
"link_url": "https://t.me/" + services.TelegramBot.BotUsername + "?start=" + token,
"link_url": "https://t.me/" + botUsername + "?start=" + token,
"message": "/start " + token,
"expires_in": 600,
})
@@ -242,13 +269,20 @@ func UnlinkClientTelegram(c *gin.Context) {
return
}
clientID := c.GetInt("client_id")
database := c.MustGet("database").(*db.Database)
if err := database.DeleteClientTelegramChatID(username); err != nil {
log.Printf("❌ [TELEGRAM_UNLINK] Erreur pour %s: %v", username, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erreur déliaison"})
return
}
// Désactiver la 2FA si Telegram est délié
if clientID > 0 {
_ = database.SetClientTwoFAEnabled(clientID, false)
}
log.Printf("✅ [TELEGRAM_UNLINK] Compte client %s délié", username)
c.JSON(http.StatusOK, gin.H{"success": true})
}
@@ -290,3 +324,62 @@ func UnlinkAdminTelegram(c *gin.Context) {
log.Printf("✅ [TELEGRAM_UNLINK] Compte admin %s délié", username)
c.JSON(http.StatusOK, gin.H{"success": true})
}
// ============================================
// LIAISON INTERNE (appelée par LBTelegram)
// ============================================
// POST /api/internal/telegram/link
// Appelée par LBTelegram quand Bot1 reçoit /start TOKEN.
// Valide le token, enregistre le chat_id, déclenche l'enrollment.
func InternalTelegramLink(c *gin.Context) {
secret := c.GetHeader("X-Internal-Secret")
expected := os.Getenv("BACKEND_LINK_SECRET")
if expected == "" || secret != expected {
c.Status(http.StatusUnauthorized)
return
}
var req struct {
ChatID int64 `json:"chat_id" binding:"required"`
Token string `json:"token" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
database := c.MustGet("database").(*db.Database)
username, role, err := db.ValidateAndConsumeLinkToken(req.Token)
if err != nil {
log.Printf("⚠️ [TELEGRAM_LINK_INTERNAL] Token invalide: %v", err)
c.Status(http.StatusUnauthorized)
return
}
var saveErr error
switch role {
case "client":
saveErr = database.SaveClientTelegramChatID(username, req.ChatID)
default:
saveErr = database.SaveUserTelegramChatID(username, req.ChatID)
}
if saveErr != nil {
log.Printf("❌ [TELEGRAM_LINK_INTERNAL] Erreur sauvegarde pour %s: %v", username, saveErr)
c.Status(http.StatusInternalServerError)
return
}
log.Printf("✅ [TELEGRAM_LINK_INTERNAL] Compte %s (%s) lié via Bot1 (chat_id %d)", username, role, req.ChatID)
if services.LBTelegram != nil && services.LBTelegram.IsConfigured() {
if err := services.LBTelegram.EnrollUser(req.ChatID, username, role); err != nil {
log.Printf("⚠️ [LB] enrollment échoué pour %s: %v", username, err)
c.Status(http.StatusInternalServerError)
return
}
}
c.Status(http.StatusOK)
}