From c9cfa7a32f68faa9d812e132706a752194a78d53 Mon Sep 17 00:00:00 2001
From: Guillaume Vagner <guillaume.vagner@supelec.fr>
Date: Sat, 23 Feb 2019 23:39:23 +0100
Subject: [PATCH] can remove groups

---
 connection-db.js |  7 ++++++-
 telegram.js      | 22 +++++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/connection-db.js b/connection-db.js
index a405361..1d42911 100644
--- a/connection-db.js
+++ b/connection-db.js
@@ -95,6 +95,11 @@ function addGroup(chatId, groupId) {
     `);
 }
 
+function removeGroup(chatId, groupId) {
+    return query(`
+    DELETE FROM groups WHERE chatId = ${chatId} AND grp = ${groupId}`)
+}
+
 function getGroups(chatId) {
     return query(`
     SELECT grp
@@ -114,4 +119,4 @@ function getSchedules() {
 }
 
 
-module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, getGroups, getSchedules };
\ No newline at end of file
+module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, removeGroup, getGroups, getSchedules };
\ No newline at end of file
diff --git a/telegram.js b/telegram.js
index 602723b..6b090db 100644
--- a/telegram.js
+++ b/telegram.js
@@ -4,7 +4,7 @@ process.env["NTBA_FIX_319"] = 1;
 var TelegramBot = require('node-telegram-bot-api');
 
 // Modules propres
-var { getChanByChatId, createChan, deleteChanByChatId, modifyChan, addGroup, getGroups } = require('./connection-db');
+var { getChanByChatId, createChan, deleteChanByChatId, modifyChan, addGroup, removeGroup, getGroups } = require('./connection-db');
 var { getBirthdays, searchGroups, getGroupById } = require('./requests');
 var { schedules, addSchedule, deleteSchedule } = require('./schedule');
 
@@ -167,6 +167,26 @@ bot.onText(/\/add (.+)/, (msg, match) => {
     })
 })
 
+bot.onText(/\/del (.+)/, (msg, match) => {
+    const chatId = msg.chat.id;
+    getChanByChatId(chatId).then(chan => {
+        const id = parseInt(match[1].split(' ')[0]);
+        return Promise.all([
+            getGroupById(chan.token, id),
+            getGroups(chatId),
+            id
+        ]);
+    }).then(([group, groups, id]) => {
+        // si pas de groupe trouvé
+        if (!group) return bot.sendMessage(chatId, 'Pas de groupe trouvé ayant cette ID');
+        // si le groupe y est déjà
+        if (groups.indexOf(id) === -1) return bot.sendMessage(chatId, 'Ce groupe n\'est pas dans votre liste d\'anniversaire.');
+        // sinon on le rajoute
+        return removeGroup(chatId, id).then(_ => {
+            bot.sendMessage(chatId, `Retrait du groupe \`${group}\` de la liste des anniversaires.`, { parse_mode: 'Markdown' });
+        })
+    })
+})
 
 // Ajout d'un rappel
 bot.onText(/\/schedule (.+)/, (msg, match) => {
-- 
GitLab