@@ -36,6 +36,30 @@ let context = {
36
36
op : 'IoTAgentNGSI.MongoDBGroupRegister'
37
37
} ;
38
38
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
+
39
63
/**
40
64
* Generates a handler for the save device group operations. The handler will take the customary error and the saved
41
65
* device group as the parameters (and pass the serialized DAO as the callback value).
@@ -58,33 +82,10 @@ function saveGroupHandler(groupDAO, callback) {
58
82
function createGroup ( group , callback ) {
59
83
/* eslint-disable-next-line new-cap */
60
84
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
+ } ) ;
88
89
89
90
logger . debug (
90
91
context ,
@@ -163,7 +164,7 @@ function getById(id, callback) {
163
164
const query = Group . model . findOne ( { _id : id } ) ;
164
165
query . select ( { __v : 0 } ) ;
165
166
166
- query . exec ( function handleGet ( error , data ) {
167
+ query . lean ( ) . exec ( function handleGet ( error , data ) {
167
168
if ( error ) {
168
169
logger . debug ( context , 'Internal MongoDB Error getting group: %s' , error ) ;
169
170
@@ -210,6 +211,24 @@ function find(service, subservice, callback) {
210
211
} ) ;
211
212
}
212
213
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
+
213
232
function findBy ( fields ) {
214
233
return function ( ) {
215
234
const queryObj = { } ;
@@ -228,24 +247,7 @@ function findBy(fields) {
228
247
229
248
context = fillService ( context , { service : 'n/a' , subservice : 'n/a' } ) ;
230
249
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 ) ;
249
251
} ;
250
252
}
251
253
@@ -255,36 +257,15 @@ function update(id, body, callback) {
255
257
if ( error ) {
256
258
callback ( error ) ;
257
259
} 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 ] ;
283
263
}
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 ) ) ;
288
269
}
289
270
} ) ;
290
271
}
@@ -303,7 +284,6 @@ function remove(id, callback) {
303
284
callback ( new errors . InternalDbError ( error ) ) ;
304
285
} else {
305
286
logger . debug ( context , 'Device [%s] successfully removed.' , id ) ;
306
-
307
287
callback ( null , deviceGroup ) ;
308
288
}
309
289
} ) ;
0 commit comments