Backend - Build & Lint / Build (push) Has been cancelled
Backend - Build & Lint / Docker Build & Push (push) Has been cancelled
Backend - Build & Lint / SSH Deploy (push) Has been cancelled
Backend - Build & Lint / Static Analysis (golangci-lint + gosec) (push) Has been cancelled
107 lines
3.8 KiB
Go
107 lines
3.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"gestion/db"
|
|
"gestion/models"
|
|
"gestion/services"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// GET /api/v1/app-settings — public, sans auth
|
|
// Retourne uniquement les flags visibles par clients/cabine (pas les détails de catégories)
|
|
func GetPublicSettings(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
settings, err := database.GetSettings()
|
|
if err != nil {
|
|
// En cas d'erreur, retourner les valeurs par défaut
|
|
settings = db.DefaultSettings()
|
|
}
|
|
|
|
poolNames := make([]string, len(settings.PointsPools))
|
|
poolKeys := make([]string, len(settings.PointsPools))
|
|
for i, p := range settings.PointsPools {
|
|
poolNames[i] = p.Name
|
|
poolKeys[i] = p.Key
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"penalties_enabled": settings.PenaltiesEnabled,
|
|
"show_amende_score": settings.ShowAmendeScore,
|
|
"points_enabled": settings.PointsEnabled,
|
|
"points_separated": len(settings.PointsPools) > 1,
|
|
"pool_names": poolNames,
|
|
"pool_keys": poolKeys,
|
|
"referral_enabled": settings.ReferralEnabled,
|
|
"referral_amount": settings.ReferralAmount,
|
|
"delivery_schedule": settings.DeliverySchedule,
|
|
"crypto_payment_enabled": settings.CryptoPaymentEnabled,
|
|
"crypto_only": settings.CryptoOnly,
|
|
"nowpayments_currencies": settings.NowPaymentsCurrencies,
|
|
"telegram_notifications_enabled": settings.TelegramNotificationsEnabled,
|
|
"shop_name": settings.ShopName,
|
|
"two_fa_enabled": settings.Telegram2FAEnabled,
|
|
"contact_telegram": settings.ContactTelegram,
|
|
"client_color_primary": settings.ClientColorPrimary,
|
|
"client_color_secondary": settings.ClientColorSecondary,
|
|
"client_color_success": settings.ClientColorSuccess,
|
|
"client_color_danger": settings.ClientColorDanger,
|
|
"client_color_warning": settings.ClientColorWarning,
|
|
})
|
|
}
|
|
|
|
// GET /api/v2/admin/protected/settings
|
|
func GetSettings(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
settings, err := database.GetSettings()
|
|
if err != nil {
|
|
log.Printf("❌ [SETTINGS] Erreur lecture: %v", err)
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erreur lecture paramètres"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "settings": settings})
|
|
}
|
|
|
|
// PUT /api/v2/admin/protected/settings
|
|
func UpdateSettings(c *gin.Context) {
|
|
database := c.MustGet("database").(*db.Database)
|
|
|
|
var req models.AppSettings
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Paramètres invalides"})
|
|
return
|
|
}
|
|
|
|
if err := database.UpdateSettings(req); err != nil {
|
|
log.Printf("❌ [SETTINGS] Erreur mise à jour: %v", err)
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erreur mise à jour paramètres"})
|
|
return
|
|
}
|
|
|
|
// Recharger le service Telegram si le token/username a changé
|
|
if services.TelegramBot != nil {
|
|
services.TelegramBot.Reload(req.TelegramBotToken, req.TelegramBotUsername)
|
|
services.TelegramBot.SetNotificationsEnabled(req.TelegramNotificationsEnabled)
|
|
if req.TelegramBotToken != "" {
|
|
log.Printf("✅ [SETTINGS] Service Telegram rechargé (username: %s)", req.TelegramBotUsername)
|
|
if webhookURL := os.Getenv("TELEGRAM_WEBHOOK_URL"); webhookURL != "" {
|
|
if err := services.TelegramBot.SetWebhook(webhookURL); err != nil {
|
|
log.Printf("⚠️ [SETTINGS] Erreur enregistrement webhook Telegram: %v", err)
|
|
} else {
|
|
log.Printf("✅ [SETTINGS] Webhook Telegram enregistré: %s", strings.NewReplacer("\n", "", "\r", "").Replace(webhookURL))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "settings": req})
|
|
}
|