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) => {
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();

View File

@ -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;