2016-05-18 22:50:06 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const _ = require('lodash')
|
2016-05-18 22:50:06 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const Constraints = require('../../shared/validation/constraints')
|
|
|
|
const ErrorTypes = require('../utils/errorTypes')
|
|
|
|
const Logger = require('../logger')
|
|
|
|
const MonitoringService = require('../services/monitoringService')
|
|
|
|
const Resources = require('../utils/resources')
|
|
|
|
const Strings = require('../utils/strings')
|
|
|
|
const Validator = require('../validation/validator')
|
|
|
|
|
|
|
|
const isValidToken = Validator.forConstraint(Constraints.token);
|
2016-06-11 15:31:57 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
module.exports = {
|
|
|
|
getAll (req, res) {
|
|
|
|
Resources.getValidRestParams('list', null, req, function (err, restParams) {
|
|
|
|
if (err) {
|
|
|
|
return Resources.error(res, err);
|
|
|
|
}
|
2016-06-11 15:31:57 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
return MonitoringService.getAll(
|
|
|
|
restParams,
|
|
|
|
function (err, monitoringStates, total) {
|
|
|
|
if (err) {
|
|
|
|
Logger.tag('monitoring', 'admin').error('Could not get monitoring states:', err);
|
|
|
|
return Resources.error(res, {data: 'Internal error.', type: ErrorTypes.internalError});
|
2016-06-11 15:31:57 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
res.set('X-Total-Count', total);
|
|
|
|
return Resources.success(res, _.map(monitoringStates, function (state) {
|
|
|
|
state.mapId = _.toLower(state.mac).replace(/:/g, '');
|
|
|
|
return state;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
2016-05-18 22:50:06 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
confirm (req, res) {
|
|
|
|
const data = Resources.getData(req);
|
2016-05-18 22:50:06 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const token = Strings.normalizeString(data.token);
|
|
|
|
if (!isValidToken(token)) {
|
|
|
|
return Resources.error(res, {data: 'Invalid token.', type: ErrorTypes.badRequest});
|
|
|
|
}
|
|
|
|
|
|
|
|
return MonitoringService.confirm(token, function (err, node) {
|
|
|
|
if (err) {
|
|
|
|
return Resources.error(res, err);
|
|
|
|
}
|
|
|
|
return Resources.success(res, {
|
|
|
|
hostname: node.hostname,
|
|
|
|
mac: node.mac,
|
|
|
|
email: node.email,
|
|
|
|
monitoring: node.monitoring,
|
|
|
|
monitoringConfirmed: node.monitoringConfirmed
|
2016-05-18 22:50:06 +02:00
|
|
|
});
|
2018-12-17 22:49:54 +01:00
|
|
|
});
|
|
|
|
},
|
2016-05-18 23:15:43 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
disable (req, res) {
|
|
|
|
const data = Resources.getData(req);
|
2016-05-18 23:15:43 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const token = Strings.normalizeString(data.token);
|
|
|
|
if (!isValidToken(token)) {
|
|
|
|
return Resources.error(res, {data: 'Invalid token.', type: ErrorTypes.badRequest});
|
|
|
|
}
|
2016-05-18 23:15:43 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
return MonitoringService.disable(token, function (err, node) {
|
|
|
|
if (err) {
|
|
|
|
return Resources.error(res, err);
|
|
|
|
}
|
|
|
|
return Resources.success(res, {
|
|
|
|
hostname: node.hostname,
|
|
|
|
mac: node.mac,
|
|
|
|
email: node.email,
|
|
|
|
monitoring: node.monitoring
|
2016-05-18 23:15:43 +02:00
|
|
|
});
|
2018-12-17 22:49:54 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|