From 888932454ca275364ae35c34d3d3136f4c82327e Mon Sep 17 00:00:00 2001 From: Xor290 Date: Fri, 12 Jun 2026 09:52:12 +0200 Subject: [PATCH] chore: build --- backend/gestion/db/db_telegram.go | 39 ++++++++++++++++++++++++++++++ backend/gestion/handlers/points.go | 1 - backend/gestion/main.go | 20 +++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/backend/gestion/db/db_telegram.go b/backend/gestion/db/db_telegram.go index 37c5863f..c521def1 100644 --- a/backend/gestion/db/db_telegram.go +++ b/backend/gestion/db/db_telegram.go @@ -109,6 +109,45 @@ func (d *Database) DeleteUserTelegramChatID(username string) error { return d.GDB.Model(&models.User{}).Where("username = ?", username).Update("telegram_chat_id", nil).Error } +// GetAllLinkedTelegramAccounts retourne tous les comptes ayant un telegram_chat_id non-null. +func (d *Database) GetAllLinkedTelegramAccounts() ([]struct { + ChatID int64 + Username string + Role string +}, error) { + type row struct { + ChatID int64 `gorm:"column:telegram_chat_id"` + Username string `gorm:"column:username"` + Role string `gorm:"column:role"` + } + + var results []row + + var clients []row + if err := d.GDB.Raw(`SELECT telegram_chat_id, username, 'client' AS role FROM clients WHERE telegram_chat_id IS NOT NULL`).Scan(&clients).Error; err != nil { + return nil, err + } + results = append(results, clients...) + + var users []row + if err := d.GDB.Raw(`SELECT telegram_chat_id, username, role FROM users WHERE telegram_chat_id IS NOT NULL`).Scan(&users).Error; err != nil { + return nil, err + } + results = append(results, users...) + + out := make([]struct { + ChatID int64 + Username string + Role string + }, len(results)) + for i, r := range results { + out[i].ChatID = r.ChatID + out[i].Username = r.Username + out[i].Role = r.Role + } + return out, nil +} + // GetUserByTelegramChatID retrouve un utilisateur (clients + users) par chat_id func (d *Database) GetUserByTelegramChatID(chatID int64) (username, role string, err error) { var clientResult struct { diff --git a/backend/gestion/handlers/points.go b/backend/gestion/handlers/points.go index 0a1193f8..2b945949 100644 --- a/backend/gestion/handlers/points.go +++ b/backend/gestion/handlers/points.go @@ -2,7 +2,6 @@ package handlers import ( "gestion/db" - "gestion/models" "gestion/utils" "net/http" "strings" diff --git a/backend/gestion/main.go b/backend/gestion/main.go index d6ba9bd4..1c5c65b5 100644 --- a/backend/gestion/main.go +++ b/backend/gestion/main.go @@ -72,6 +72,26 @@ func main() { } } + // Ré-enrôler tous les comptes déjà liés dans lbtelegram (au cas où lbtelegram a redémarré) + if lbService.IsConfigured() { + go func() { + accounts, err := database.GetAllLinkedTelegramAccounts() + if err != nil { + log.Printf("⚠️ [LB_SYNC] Erreur lecture comptes liés: %v", err) + return + } + ok, fail := 0, 0 + for _, a := range accounts { + if err := lbService.EnrollUser(a.ChatID, a.Username, a.Role); err != nil { + fail++ + } else { + ok++ + } + } + log.Printf("✅ [LB_SYNC] Re-enrollment terminé: %d OK, %d échecs (total %d comptes)", ok, fail, len(accounts)) + }() + } + log.Println("") log.Println("🧹 Démarrage du nettoyage des commandes invalides...") removed, err := database.CleanupInvalidQueueCommands()