From c945c61631e697f68ff7d43b63c4a6dc3531957b Mon Sep 17 00:00:00 2001 From: Pierre-Gilles Leymarie Date: Sun, 25 Sep 2016 09:43:38 +0200 Subject: [PATCH] Create or update device & deviceType --- api/core/device/device.create.js | 40 ++++++++----------- api/core/devicetype/deviceType.create.js | 34 +++++++++++++++- api/core/devicetype/deviceType.queries.js | 1 + .../core/devicetype/deviceType.create.test.js | 20 ++++++++++ 4 files changed, 69 insertions(+), 26 deletions(-) diff --git a/api/core/device/device.create.js b/api/core/device/device.create.js index 929d191c1b..001ab33d89 100644 --- a/api/core/device/device.create.js +++ b/api/core/device/device.create.js @@ -14,35 +14,27 @@ function create(param) { if(devices.length){ - // if device already exist, we don't create it again - return param; + // if device already exist, we update it + return Device.udpate({id: devices[0].id}, param.device) + .then((rows) => rows[0]); } else { // if not, we create the device - return createDevice(param); + return Device.create(param.device); } - }); -} + }) + .then((device) => { -function createDevice(param){ - - // first, we create the device - return Device.create(param.device) - .then(function(device) { + // foreach deviceType, we create it if not exist + return Promise.map(param.types, function(type){ + type.device = device.id; + return gladys.deviceType.create(type); + }) + .then((types) => { - // we create all the types - return Promise.map(param.types, function(type) { - type.device = device.id; - return DeviceType.create(type); - }) + // we return device and types + return {device: device, types: types}; + }); - // we return the results - .then(function(types) { - var result = { - device: device, - types: types - }; - return Promise.resolve(result); - }); - }); + }); } diff --git a/api/core/devicetype/deviceType.create.js b/api/core/devicetype/deviceType.create.js index 2104e7240d..112dc55398 100644 --- a/api/core/devicetype/deviceType.create.js +++ b/api/core/devicetype/deviceType.create.js @@ -1,4 +1,34 @@ +const Promise = require('bluebird'); +const queries = require('./deviceType.queries.js'); module.exports = function create(type){ - return DeviceType.create(type); -}; \ No newline at end of file + return deviceTypeExist(type) + .then((exist) => { + + // if exist, update + if(exist) { + return DeviceType.update({id: exist.id}, type) + .then((rows) => { + if(rows.length) return rows[0]; + else return null; + }); + } else { + return DeviceType.create(type); + } + }); +}; + +/** + * Returns true if the deviceType exist + */ +function deviceTypeExist(type){ + if(type.device && type.identifier){ + return gladys.utils.sql(queries.getByDeviceAndIdentifier, [type.device, type.identifier]) + .then((rows) => { + if(rows.length) return rows[0]; + else return false; + }); + } else { + return Promise.resolve(false); + } +} \ No newline at end of file diff --git a/api/core/devicetype/deviceType.queries.js b/api/core/devicetype/deviceType.queries.js index de2d0155ed..7cc629f0bc 100644 --- a/api/core/devicetype/deviceType.queries.js +++ b/api/core/devicetype/deviceType.queries.js @@ -38,6 +38,7 @@ module.exports = { JOIN devicetype dt ON (d.id = dt.device) JOIN room r ON (d.room = r.id); `, + getByDeviceAndIdentifier: 'SELECT id FROM devicetype WHERE device = ? AND identifier = ?;', delete : 'DELETE FROM devicetype WHERE id = ?;', deleteDeviceStates: 'DELETE FROM devicestate WHERE devicetype = ?;' }; \ No newline at end of file diff --git a/test/unit/api/core/devicetype/deviceType.create.test.js b/test/unit/api/core/devicetype/deviceType.create.test.js index ec3dad460e..de90b7e804 100644 --- a/test/unit/api/core/devicetype/deviceType.create.test.js +++ b/test/unit/api/core/devicetype/deviceType.create.test.js @@ -22,6 +22,26 @@ describe('DeviceType', function() { }); }); + + it('should update deviceType', function (done) { + + var obj = { + device: 1, + identifier: 'UNIQUE_IDENTIFIER', + type: 'multilevel', + min: 0, + max: 1, + sensor: false + }; + + gladys.deviceType.create(obj) + .then(function(type){ + console.log(type); + type.type.should.equal(obj.type); + done(); + }).catch(done); + + }); });