diff --git a/lib/roles/app.js b/lib/roles/app.js index 4b650816..82957450 100644 --- a/lib/roles/app.js +++ b/lib/roles/app.js @@ -119,11 +119,7 @@ module.exports = function(options) { return Promise.promisify(cursor.toArray, cursor)() .then(function(list) { list.forEach(function(device) { - datautil.applyData(device) - datautil.applyOwner(device, req.user) - if (!device.present) { - device.owner = null - } + datautil.normalize(device, req.user) }) res.json({ success: true @@ -145,11 +141,7 @@ module.exports = function(options) { return Promise.promisify(cursor.toArray, cursor)() .then(function(list) { list.forEach(function(device) { - datautil.applyData(device) - datautil.applyOwner(device, req.user) - if (!device.present) { - device.owner = null - } + datautil.normalize(device, req.user) }) res.json({ success: true @@ -169,11 +161,7 @@ module.exports = function(options) { dbapi.loadDevice(req.params.serial) .then(function(device) { if (device) { - datautil.applyData(device) - datautil.applyOwner(device, req.user) - if (!device.present) { - device.owner = null - } + datautil.normalize(device, req.user) res.json({ success: true , device: device diff --git a/lib/util/datautil.js b/lib/util/datautil.js index 0dafb3f0..7c8c8187 100644 --- a/lib/util/datautil.js +++ b/lib/util/datautil.js @@ -10,7 +10,9 @@ var aliases = { 'KYY22': 'L02' } -module.exports.applyData = function(device) { +var datautil = module.exports = Object.create(null) + +datautil.applyData = function(device) { var model = device.model if (model) { @@ -50,11 +52,19 @@ module.exports.applyData = function(device) { return device } -module.exports.applyOwner = function(device, user) { +datautil.applyOwner = function(device, user) { device.using = !!device.owner && device.owner.email === user.email return device } -module.exports.middleware = function() { +datautil.normalize = function(device, user) { + datautil.applyData(device) + datautil.applyOwner(device, user) + if (!device.present) { + device.owner = null + } +} + +datautil.middleware = function() { return express.static(pathutil.root('node_modules/stf-devices-db/data/small')) }