From 5824f3abc238fdbf32e86916359787f7fe68fdd7 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+frab1t@users.noreply.github.com> Date: Tue, 12 Feb 2019 22:15:43 +0100 Subject: [PATCH] refactoring promises --- index.js | 15 ++++----------- scanner.js | 44 +++++++++++--------------------------------- 2 files changed, 15 insertions(+), 44 deletions(-) diff --git a/index.js b/index.js index a5b5790..f8fb1da 100644 --- a/index.js +++ b/index.js @@ -24,15 +24,8 @@ bot.on('/help', (msg) => { bot.on('/scan', (msg) => { if (utils.isAuthorizedUser(msg.from.id)) { scanner.scanNetwork(msg.from.id) - .then(() => { - scanner.getDevicesNetwork(msg.from.id) - .then((devices) => { - bot.sendMessage(msg.from.id, utils.templateDevicesList(devices), { parseMode: 'Markdown' }); - }) - .catch((err) => { - msg.reply.text('Error, check the error.log'); - logger.logError(err); - }); + .then((devices) => { + bot.sendMessage(msg.from.id, utils.templateDevicesList(devices), { parseMode: 'Markdown' } ); }) .catch((err) => { msg.reply.text('Error, check the error.log'); @@ -70,8 +63,8 @@ bot.on(/^\/remove (.+)$/, (msg, props) => { } }); -bot.on('/getUID', (msg) => { - msg.reply.text(`⚠️ UID: ${msg.from.id}`); +bot.on('/me', (msg) => { + msg.reply.text(`⚠️ My UID: ${msg.from.id}`); }); scanner.initScannerDB(); diff --git a/scanner.js b/scanner.js index 638bec3..59f768a 100644 --- a/scanner.js +++ b/scanner.js @@ -2,52 +2,31 @@ const arpScanner = require('arpscan/promise'); const appConfig = require('./config.json'); const db = require('./db'); -function scannerResults(data) { - return data; -} - -function returnDevices(data) { - return data; -} - -function returnDevicesError(err) { - return err; -} - -function returnInventory(data) { - return data; -} - -function returnInventoryError(err) { - return err; -} - -function scannerError(err) { - return err; -} - function scanNetwork(userId) { db.initScan(userId); return ( arpScanner(appConfig.scanner) - .then(scannerResults) - .then(data => db.addDevices(userId, data)) - .catch(scannerError) + .then(data => { + db.addDevices(userId, data) + return userId + }) + .then(getDevicesNetwork) + .catch((err) => { return err}) ); } function getDevicesNetwork(userId) { return (db.getDevices(userId) - .then(returnDevices) - .catch(returnDevicesError) + .then(data => {return data}) + .catch(err => {return err}) ); } function getInventoryNetwork() { return ( db.getInventory() - .then(returnInventory) - .catch(returnInventoryError) + .then(data => {return data}) + .catch(err => {return err}) ); } @@ -60,7 +39,7 @@ function addDeviceToTrack(data) { const devArg = { device: data[0], owner: data[1], - mac: data[2], + mac: data[2].toUpperCase(), }; db.addDeviceToInventory(devArg); } @@ -71,7 +50,6 @@ function removeDeviceToTrack(mac) { module.exports.initScannerDB = initScannerDB; module.exports.scanNetwork = scanNetwork; -module.exports.getDevicesNetwork = getDevicesNetwork; module.exports.getInventoryNetwork = getInventoryNetwork; module.exports.addDeviceToTrack = addDeviceToTrack; module.exports.removeDeviceToTrack = removeDeviceToTrack;