Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create or update device & deviceType
- Loading branch information
1 parent
b5a4027
commit c945c61
Showing
4 changed files
with
69 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,34 @@ | ||
const Promise = require('bluebird'); | ||
const queries = require('./deviceType.queries.js'); | ||
|
||
module.exports = function create(type){ | ||
return DeviceType.create(type); | ||
}; | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters