From 1102f2249da3d75482ab7e7a87fbab0696f1792a Mon Sep 17 00:00:00 2001
From: Guillaume Vagner <guillaume.vagner@supelec.fr>
Date: Sat, 23 Feb 2019 22:44:34 +0100
Subject: [PATCH] rebootproof

---
 connection-db.js | 10 +++++++++-
 schedule.js      | 19 +++++++++----------
 telegram.js      |  9 +++++----
 3 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/connection-db.js b/connection-db.js
index 3a20002..a405361 100644
--- a/connection-db.js
+++ b/connection-db.js
@@ -105,5 +105,13 @@ function getGroups(chatId) {
     })
 }
 
+function getSchedules() {
+    return query(`
+    SELECT *
+    FROM channel
+    WHERE schedule <> ""
+    `)
+}
+
 
-module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, getGroups };
\ No newline at end of file
+module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, getGroups, getSchedules };
\ No newline at end of file
diff --git a/schedule.js b/schedule.js
index b0cc48a..0a14c2c 100644
--- a/schedule.js
+++ b/schedule.js
@@ -2,23 +2,22 @@
 var schedule = require('node-schedule');
 
 // Modules propres
-var { modifyChan, getGroups } = require('./connection-db');
+var { modifyChan, getGroups, getSchedules } = require('./connection-db');
 var { getBirthdays, getNewToken } = require('./requests');
+var bot = require('./telegram');
 
 // Création de variables
 var schedules = {};
 
+getSchedules().then(chans => {
+    chans.forEach(chan => {
+        addSchedule(chan, chan.schedule)
+    })
+    console.log(`[schdles] reload schedules`)
+})
 
 
-// En cas de redémarrage de l'appli
-// query(`SELECT chatId, schedule FROM channel WHERE schedule <> ""`).then(rep => {
-//     rep.forEach(chan => {
-// 
-//     })
-// })
-
-
-function addSchedule(chan, time, bot) {
+function addSchedule(chan, time) {
     const hour = parseInt(time.split(':')[0]);
     const minute = parseInt(time.split(':')[1]);
     chan.schedule = time;
diff --git a/telegram.js b/telegram.js
index ffbe4f1..602723b 100644
--- a/telegram.js
+++ b/telegram.js
@@ -168,7 +168,7 @@ bot.onText(/\/add (.+)/, (msg, match) => {
 })
 
 
-// Ajout du temps de schedule
+// Ajout d'un rappel
 bot.onText(/\/schedule (.+)/, (msg, match) => {
     const chatId = msg.chat.id;
     const time = match[1];
@@ -180,19 +180,20 @@ bot.onText(/\/schedule (.+)/, (msg, match) => {
     bot.sendMessage(chatId, `Votre rappel est configuré pour tous les jours à ${time}`);
 })
 
+
+// Suppression du rappel
 bot.onText(/\/unschedule/, msg => {
     const chatId = msg.chat.id;
     getChanByChatId(chatId).then(chan => {
         if (!chan) return bot.sendMessage(chatId, 'Pas de compte enregistré, faites /start pour commencer');
-        if (chan.time == '') return bot.sendMessage(chatId, 'Pas de rappel défini...');
+        if (chan.schedule == '') return bot.sendMessage(chatId, 'Pas de rappel défini...');
 
         deleteSchedule(chatId)
-        chan.time = ''
+        chan.schedule = ''
         return modifyChan(chan)
     }).then(_ => {
         bot.sendMessage(chatId, 'Votre rappel a été supprimé.')
     })
-
 })
 
 // J'étais bien obligé (en vrai c'est pour tester)
-- 
GitLab