add rest API for update Device note (#222)

* add rest API for update Device note

Signed-off-by: Jussi Vatjus-Anttila <jussiva@gmail.com>

* improve failure message

Signed-off-by: Jussi Vatjus-Anttila <jussiva@gmail.com>

Co-authored-by: Karol Wrótniak <karol.wrotniak@droidsonroids.pl>
This commit is contained in:
Jussi Vatjus-Anttila
2021-04-06 12:21:38 +03:00
committed by GitHub
parent 38cb66b1bd
commit 3370f679bc
2 changed files with 76 additions and 0 deletions

View File

@@ -459,6 +459,32 @@ function removeOriginGroupDevice(req, res) {
apiutil.redirectApiWrapper('serial', removeOriginGroupDevices, req, res)
}
function putDeviceInfoBySerial(req, res) {
const serial = req.swagger.params.serial.value
const body = req.swagger.params.device.value.device
dbapi.loadDeviceBySerial(serial)
.then((data) => {
if (!data) {
return apiutil.respond(res, 404, `Not Found (${serial})`)
}
var updates = []
// Update fields based on given body
if (_.has(body, 'note')) {
updates.push(dbapi.setDeviceNote(serial, body.note))
}
if (updates.length === 0) {
return apiutil.respond(res, 400, 'No content to update')
}
return Promise.all(updates)
.then(function() {
apiutil.respond(res, 200)
})
})
.catch(function(err) {
apiutil.internalError(res, 'Failed to update device: ', err.stack)
})
}
function deleteDevices(req, res) {
const serials = apiutil.getBodyParameter(req.body, 'serials')
const target = apiutil.getQueryParameter(req.swagger.params.redirected) ? 'device' : 'devices'
@@ -509,12 +535,17 @@ function deleteDevices(req, res) {
})
}
function putDeviceBySerial(req, res) {
apiutil.redirectApiWrapper('serial', putDeviceInfoBySerial, req, res)
}
function deleteDevice(req, res) {
apiutil.redirectApiWrapper('serial', deleteDevices, req, res)
}
module.exports = {
getDevices: getDevices
, putDeviceBySerial: putDeviceBySerial
, getDeviceBySerial: getDeviceBySerial
, getDeviceGroups: getDeviceGroups
, getDeviceBookings: getDeviceBookings