]> git.digitality.be Git - pdw25-26/commitdiff
Connexion au module alerte
authorSacheat <sacha.libion@outlook.com>
Sat, 28 Feb 2026 21:17:09 +0000 (22:17 +0100)
committerSacheat <sacha.libion@outlook.com>
Sat, 28 Feb 2026 21:17:09 +0000 (22:17 +0100)
Wallette/server/modules/strategy/repositories/StrategyRepo.js
Wallette/server/modules/strategy/services/BotService.js

index 5a353ae8828878abe53e492300927a28b4cccfff..b0cdc80b23464855ce80d01e0cd6ffa0c88843e7 100644 (file)
@@ -8,13 +8,18 @@ class StrategyRepo {
      */
     async getActiveStrategies() {
         const sql = `
-            SELECT user_strategy_id,
-                   user_id,
-                   pair_id,
-                   mode,
-                   params
-            FROM user_strategies
-            WHERE is_active = TRUE
+            SELECT 
+                us.user_strategy_id,
+                us.user_id,
+                us.pair_id,
+                us.mode,
+                us.params,
+                p.pair_code,    -- C'est le nom lisible (ex: BTC/USDT)
+                p.base_symbol,  -- Le symbole de base (ex: BTC)
+                p.quote_symbol  -- La monnaie de cotation (ex: USDT)
+            FROM user_strategies us
+            JOIN pairs p ON us.pair_id = p.pair_id
+            WHERE us.is_active = TRUE
         `;
 
         try {
index 599d08916cde3eb527d73f1ad02276457a7ae0c3..66352718bca1bbd2f88124722dec404e7c72af31 100644 (file)
@@ -2,6 +2,7 @@ import strategyRepo from '../repositories/StrategyRepo.js';
 import marketRepo from '../repositories/MarketDataRepo.js';
 import signalRepo from '../repositories/SignalRepo.js';
 import StrategyFactory from '../factories/StrategyFactory.js';
+const ALERTS_URL = process.env.ALERTS_BASE_URL || 'http://127.0.0.1:3003';
 
 class BotService {
 
@@ -32,7 +33,7 @@ class BotService {
      * Traite un utilisateur spécifique
      */
     async processUserStrategy(userConfig) {
-        const { user_strategy_id, pair_id, mode, params } = userConfig;
+        const { user_strategy_id, user_id, pair_id, pair_code, mode, params } = userConfig;
 
         try {
             // A. Récupérer les données de marché
@@ -47,14 +48,14 @@ class BotService {
             const strategyAlgo = StrategyFactory.getStrategy(mode);
 
             // C. Lancer l'analyse
-            // Note : params est un JSON qui vient de la DB (ex: {rsi_period: 14})
             const signal = strategyAlgo.analyze(candles, params);
 
             // D. Si un signal est trouvé, sauvegarde
             if (signal) {
                 console.log(`SIGNAL DÉTECTÉ pour ${user_strategy_id} (${mode}) !`);
 
-                await signalRepo.saveSignal({
+                // On stocke le retour de saveSignal dans la variable signalId
+                const signalId = await signalRepo.saveSignal({
                     userStrategyId: user_strategy_id,
                     action: signal.action,
                     confidence: signal.confidence,
@@ -62,12 +63,55 @@ class BotService {
                     price: candles[candles.length - 1].close_price,
                     indicators: signal.indicators
                 });
+
+                // Envoi au module alerte
+                await this.sendToAlertsModule({
+                    userId: user_id,
+                    pairId: pair_id,
+                    pair: pair_code || `PAIR_${pair_id}`,
+                    action: signal.action,
+                    confidence: signal.confidence,
+                    criticality: this.getCriticality(signal.confidence),
+                    reason: signal.reason,
+                    correlationId: signalId
+                });
             }
 
         } catch (error) {
             console.error(`Erreur sur la stratégie ${user_strategy_id} :`, error.message);
         }
     }
+
+    async sendToAlertsModule(signalPayload) {
+        try {
+            const response = await fetch(
+                `${ALERTS_URL}/api/alerts/process-signal`,
+                {
+                    method: 'POST',
+                    headers: { 'Content-Type': 'application/json' },
+                    body: JSON.stringify(signalPayload)
+                }
+            );
+            if (!response.ok) {
+                const err = await response.json();
+                console.error('Module Alertes a refusé le signal :', err);
+                return;
+            }
+            const result = await response.json();
+            console.log('Signal envoyé au module Alertes :', result.data);
+        } catch (error) {
+            // Si alertes-service est éteint, le bot continue quand même
+            console.error('Impossible de joindre le module Alertes :', error.message);
+        }
+    }
+
+    getCriticality(confidence) {
+        if (confidence >= 0.85) return 'CRITICAL';
+        if (confidence >= 0.65) return 'WARNING';
+        return 'INFO';
+    }
+
+
 }
 
 const botService = new BotService();