@@ -36,6 +36,30 @@ let context = {
3636 op : 'IoTAgentNGSI.MongoDBGroupRegister'
3737} ;
3838
39+ const attributeList = [
40+ 'url' ,
41+ 'resource' ,
42+ 'apikey' ,
43+ 'type' ,
44+ 'service' ,
45+ 'subservice' ,
46+ 'description' ,
47+ 'trust' ,
48+ 'cbHost' ,
49+ 'timezone' ,
50+ 'timestamp' ,
51+ 'commands' ,
52+ 'lazy' ,
53+ 'attributes' ,
54+ 'staticAttributes' ,
55+ 'internalAttributes' ,
56+ 'autoprovision' ,
57+ 'explicitAttrs' ,
58+ 'expressionLanguage' ,
59+ 'defaultEntityNameConjunction' ,
60+ 'ngsiVersion'
61+ ] ;
62+
3963/**
4064 * Generates a handler for the save device group operations. The handler will take the customary error and the saved
4165 * device group as the parameters (and pass the serialized DAO as the callback value).
@@ -58,33 +82,10 @@ function saveGroupHandler(groupDAO, callback) {
5882function createGroup ( group , callback ) {
5983 /* eslint-disable-next-line new-cap */
6084 const groupObj = new Group . model ( ) ;
61- const attributeList = [
62- 'url' ,
63- 'resource' ,
64- 'apikey' ,
65- 'type' ,
66- 'service' ,
67- 'subservice' ,
68- 'description' ,
69- 'trust' ,
70- 'cbHost' ,
71- 'timezone' ,
72- 'timestamp' ,
73- 'commands' ,
74- 'lazy' ,
75- 'attributes' ,
76- 'staticAttributes' ,
77- 'internalAttributes' ,
78- 'autoprovision' ,
79- 'explicitAttrs' ,
80- 'expressionLanguage' ,
81- 'defaultEntityNameConjunction' ,
82- 'ngsiVersion'
83- ] ;
84-
85- for ( let i = 0 ; i < attributeList . length ; i ++ ) {
86- groupObj [ attributeList [ i ] ] = group [ attributeList [ i ] ] ;
87- }
85+
86+ attributeList . forEach ( ( key ) => {
87+ groupObj [ key ] = group [ key ] ;
88+ } ) ;
8889
8990 logger . debug (
9091 context ,
@@ -163,7 +164,7 @@ function getById(id, callback) {
163164 const query = Group . model . findOne ( { _id : id } ) ;
164165 query . select ( { __v : 0 } ) ;
165166
166- query . exec ( function handleGet ( error , data ) {
167+ query . lean ( ) . exec ( function handleGet ( error , data ) {
167168 if ( error ) {
168169 logger . debug ( context , 'Internal MongoDB Error getting group: %s' , error ) ;
169170
@@ -210,6 +211,24 @@ function find(service, subservice, callback) {
210211 } ) ;
211212}
212213
214+ function findOneInMongoDB ( queryObj , fields , callback ) {
215+ const query = Group . model . findOne ( queryObj ) ;
216+ query . select ( { __v : 0 } ) ;
217+ query . lean ( ) . exec ( function handleGet ( error , data ) {
218+ if ( error ) {
219+ logger . debug ( context , 'Internal MongoDB Error getting group: %s' , error ) ;
220+ callback ( new errors . InternalDbError ( error ) ) ;
221+ } else if ( data ) {
222+ context = fillService ( context , data ) ;
223+ logger . debug ( context , 'Device group data found: %j' , data ) ;
224+ callback ( null , data ) ;
225+ } else {
226+ logger . debug ( context , 'Device group for fields [%j] not found: [%j]' , fields , queryObj ) ;
227+ callback ( new errors . DeviceGroupNotFound ( fields , queryObj ) ) ;
228+ }
229+ } ) ;
230+ }
231+
213232function findBy ( fields ) {
214233 return function ( ) {
215234 const queryObj = { } ;
@@ -228,24 +247,7 @@ function findBy(fields) {
228247
229248 context = fillService ( context , { service : 'n/a' , subservice : 'n/a' } ) ;
230249 logger . debug ( context , 'Looking for group params %j with queryObj %j' , fields , queryObj ) ;
231- const query = Group . model . findOne ( queryObj ) ;
232-
233- query . select ( { __v : 0 } ) ;
234-
235- query . exec ( function handleGet ( error , data ) {
236- if ( error ) {
237- logger . debug ( context , 'Internal MongoDB Error getting group: %s' , error ) ;
238- callback ( new errors . InternalDbError ( error ) ) ;
239- } else if ( data ) {
240- context = fillService ( context , data ) ;
241- logger . debug ( context , 'Device group data found: %j' , data . toObject ( ) ) ;
242- callback ( null , data . toObject ( ) ) ;
243- } else {
244- logger . debug ( context , 'Device group for fields [%j] not found: [%j]' , fields , queryObj ) ;
245-
246- callback ( new errors . DeviceGroupNotFound ( fields , queryObj ) ) ;
247- }
248- } ) ;
250+ findOneInMongoDB ( queryObj , fields , callback ) ;
249251 } ;
250252}
251253
@@ -255,36 +257,15 @@ function update(id, body, callback) {
255257 if ( error ) {
256258 callback ( error ) ;
257259 } else {
258- const attributes = [
259- 'url' ,
260- 'apikey' ,
261- 'type' ,
262- 'service' ,
263- 'subservice' ,
264- 'description' ,
265- 'trust' ,
266- 'cbHost' ,
267- 'timezone' ,
268- 'timestamp' ,
269- 'commands' ,
270- 'lazy' ,
271- 'attributes' ,
272- 'staticAttributes' ,
273- 'internalAttributes' ,
274- 'explicitAttrs' ,
275- 'expressionLanguage' ,
276- 'defaultEntityNameConjunction' ,
277- 'ngsiVersion'
278- ] ;
279-
280- for ( let i = 0 ; i < attributes . length ; i ++ ) {
281- if ( body [ attributes [ i ] ] !== undefined ) {
282- group [ attributes [ i ] ] = body [ attributes [ i ] ] ;
260+ attributeList . forEach ( ( key ) => {
261+ if ( body [ key ] !== undefined ) {
262+ group [ key ] = body [ key ] ;
283263 }
284- }
285-
286- group . isNew = false ;
287- group . save ( saveGroupHandler ( group , callback ) ) ;
264+ } ) ;
265+ /* eslint-disable-next-line new-cap */
266+ const groupObj = new Group . model ( group ) ;
267+ groupObj . isNew = false ;
268+ groupObj . save ( saveGroupHandler ( groupObj , callback ) ) ;
288269 }
289270 } ) ;
290271}
@@ -303,7 +284,6 @@ function remove(id, callback) {
303284 callback ( new errors . InternalDbError ( error ) ) ;
304285 } else {
305286 logger . debug ( context , 'Device [%s] successfully removed.' , id ) ;
306-
307287 callback ( null , deviceGroup ) ;
308288 }
309289 } ) ;
0 commit comments