refactoring promises

This commit is contained in:
Francesco Esposito 2019-02-12 22:15:43 +01:00
parent 9c600000d3
commit 5824f3abc2
2 changed files with 15 additions and 44 deletions

View File

@ -24,15 +24,8 @@ bot.on('/help', (msg) => {
bot.on('/scan', (msg) => { bot.on('/scan', (msg) => {
if (utils.isAuthorizedUser(msg.from.id)) { if (utils.isAuthorizedUser(msg.from.id)) {
scanner.scanNetwork(msg.from.id) scanner.scanNetwork(msg.from.id)
.then(() => { .then((devices) => {
scanner.getDevicesNetwork(msg.from.id) bot.sendMessage(msg.from.id, utils.templateDevicesList(devices), { parseMode: 'Markdown' } );
.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);
});
}) })
.catch((err) => { .catch((err) => {
msg.reply.text('Error, check the error.log'); msg.reply.text('Error, check the error.log');
@ -70,8 +63,8 @@ bot.on(/^\/remove (.+)$/, (msg, props) => {
} }
}); });
bot.on('/getUID', (msg) => { bot.on('/me', (msg) => {
msg.reply.text(`⚠️ UID: ${msg.from.id}`); msg.reply.text(`⚠️ My UID: ${msg.from.id}`);
}); });
scanner.initScannerDB(); scanner.initScannerDB();

View File

@ -2,52 +2,31 @@ const arpScanner = require('arpscan/promise');
const appConfig = require('./config.json'); const appConfig = require('./config.json');
const db = require('./db'); 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) { function scanNetwork(userId) {
db.initScan(userId); db.initScan(userId);
return ( return (
arpScanner(appConfig.scanner) arpScanner(appConfig.scanner)
.then(scannerResults) .then(data => {
.then(data => db.addDevices(userId, data)) db.addDevices(userId, data)
.catch(scannerError) return userId
})
.then(getDevicesNetwork)
.catch((err) => { return err})
); );
} }
function getDevicesNetwork(userId) { function getDevicesNetwork(userId) {
return (db.getDevices(userId) return (db.getDevices(userId)
.then(returnDevices) .then(data => {return data})
.catch(returnDevicesError) .catch(err => {return err})
); );
} }
function getInventoryNetwork() { function getInventoryNetwork() {
return ( return (
db.getInventory() db.getInventory()
.then(returnInventory) .then(data => {return data})
.catch(returnInventoryError) .catch(err => {return err})
); );
} }
@ -60,7 +39,7 @@ function addDeviceToTrack(data) {
const devArg = { const devArg = {
device: data[0], device: data[0],
owner: data[1], owner: data[1],
mac: data[2], mac: data[2].toUpperCase(),
}; };
db.addDeviceToInventory(devArg); db.addDeviceToInventory(devArg);
} }
@ -71,7 +50,6 @@ function removeDeviceToTrack(mac) {
module.exports.initScannerDB = initScannerDB; module.exports.initScannerDB = initScannerDB;
module.exports.scanNetwork = scanNetwork; module.exports.scanNetwork = scanNetwork;
module.exports.getDevicesNetwork = getDevicesNetwork;
module.exports.getInventoryNetwork = getInventoryNetwork; module.exports.getInventoryNetwork = getInventoryNetwork;
module.exports.addDeviceToTrack = addDeviceToTrack; module.exports.addDeviceToTrack = addDeviceToTrack;
module.exports.removeDeviceToTrack = removeDeviceToTrack; module.exports.removeDeviceToTrack = removeDeviceToTrack;