bridge-node-server/node_server/utils/device/device.js
Martin Donnelly 57bd6c8e6a init
2018-06-24 21:15:03 +01:00

39 lines
1.0 KiB
JavaScript

/**
* Helpers functions for dealing with devices
*/
const _ = require('lodash');
const mainDB = require(global.pathPrefix + 'mainDB.js');
const mainDBP = require(global.pathPrefix + 'mainDB-promises.js');
module.exports = {
archiveDevice
};
/**
* Archives a copy of the device object, after appropriate anonymisation
*
* @param {Object} device - The device to delete
*
* @returns {Promise} - Promise for the result of archiving the device
*/
function archiveDevice(device) {
/**
* Store the old object _id as DeviceIndex
*/
const archivedDevice = _.clone(device);
archivedDevice.DeviceIndex = device._id.toString();
delete archivedDevice._id;
archivedDevice.DeviceAuthorisation = '';
archivedDevice.DeviceSalt = '';
archivedDevice.CurrentHMAC = '';
archivedDevice.PendingHMAC = '';
archivedDevice.LastUpdate = new Date();
/**
* Write the object to the Archive.
*/
return mainDBP.addObject(mainDB.collectionDeviceArchive, archivedDevice, undefined, false);
}