ffffng/server/services/monitoringService.js

54 lines
1.9 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('ffffng')
.service('MonitoringService', function (NodeService, ErrorTypes) {
return {
2016-05-23 22:36:27 +02:00
confirm: function (token, callback) {
NodeService.getNodeDataByMonitoringToken(token, function (err, node, nodeSecrets) {
if (err) {
return callback(err);
}
if (!node.monitoring || !nodeSecrets.monitoringToken || nodeSecrets.monitoringToken !== token) {
return callback({data: 'Invalid token.', type: ErrorTypes.badRequest});
}
if (node.monitoringConfirmed) {
return callback(null, node);
}
node.monitoringConfirmed = true;
2016-05-18 23:15:43 +02:00
NodeService.internalUpdateNode(node.token, node, nodeSecrets, function (err, token, node) {
if (err) {
return callback(err);
}
callback(null, node);
});
});
},
2016-05-23 22:36:27 +02:00
disable: function (token, callback) {
NodeService.getNodeDataByMonitoringToken(token, function (err, node, nodeSecrets) {
2016-05-18 23:15:43 +02:00
if (err) {
return callback(err);
}
if (!node.monitoring || !nodeSecrets.monitoringToken || nodeSecrets.monitoringToken !== token) {
return callback({data: 'Invalid token.', type: ErrorTypes.badRequest});
}
node.monitoring = false;
node.monitoringConfirmed = false;
nodeSecrets.monitoringToken = '';
NodeService.internalUpdateNode(node.token, node, nodeSecrets, function (err, token, node) {
if (err) {
return callback(err);
}
callback(null, node);
});
});
}
};
});