mirror of
https://github.com/openstf/stf
synced 2025-10-04 18:29:17 +02:00
Normalize device data in one place.
This commit is contained in:
parent
ea13232f10
commit
5601fe2829
2 changed files with 16 additions and 18 deletions
|
@ -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
|
||||
|
|
|
@ -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'))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue