@@ -193,7 +193,7 @@ async function createGroup(currentUser, data) {
193
193
await tx . commit ( )
194
194
195
195
// set the cache
196
- const cache = helper . getCacheInstance ( )
196
+ const cache = await helper . getCacheInstance ( )
197
197
cache . set ( group . id , group )
198
198
cache . set ( `${ group . id } -members` , [ ] )
199
199
@@ -276,7 +276,7 @@ async function updateGroup(currentUser, groupId, data) {
276
276
await tx . commit ( )
277
277
278
278
// update the cache
279
- const cache = helper . getCacheInstance ( )
279
+ const cache = await helper . getCacheInstance ( )
280
280
cache . set ( group . id , updateGroup )
281
281
282
282
return updatedGroup
@@ -350,7 +350,6 @@ async function getGroup(currentUser, groupId, criteria) {
350
350
'oldId'
351
351
]
352
352
353
-
354
353
if ( _ . uniq ( fieldNames ) . length !== fieldNames . length ) {
355
354
throw new errors . BadRequestError ( `duplicate field names are not allowed` )
356
355
}
@@ -367,110 +366,110 @@ async function getGroup(currentUser, groupId, criteria) {
367
366
}
368
367
}
369
368
370
- let session = undefined ;
369
+ const session = helper . createDBSession ( )
370
+ const cache = await helper . getCacheInstance ( )
371
371
372
- const cache = helper . getCacheInstance ( )
373
- const flattenGroupIdTree = [ ]
372
+ let groupToReturn
374
373
375
374
try {
376
- let cachedGroup
377
375
if ( criteria . isCache ) {
378
376
// check for the availibility of the group in cache
379
- cachedGroup = cache . get ( groupId )
377
+ groupToReturn = cache . get ( groupId )
380
378
}
381
379
382
- if ( criteria . isCache && cachedGroup ) {
383
- if ( ! isAdmin ) delete cachedGroup . status
380
+ if ( criteria . isCache && groupToReturn ) {
381
+ if ( ! isAdmin ) delete groupToReturn . status
384
382
385
383
// if the group is private, the user needs to be a member of the group, or an admin
386
- if ( cachedGroup . privateGroup && currentUser !== 'M2M' && ! helper . hasAdminRole ( currentUser ) ) {
384
+ if ( groupToReturn . privateGroup && currentUser !== 'M2M' && ! helper . hasAdminRole ( currentUser ) ) {
387
385
const cachedGroupMembers = cache . get ( `${ groupId } -members` )
388
- if ( ! cachedGroupMembers ) {
389
- session = helper . createDBSession ( )
386
+
387
+ if ( ! _ . includes ( cachedGroupMembers , currentUser . userId ) ) {
390
388
await helper . ensureGroupMember ( session , group . id , currentUser . userId )
391
389
392
390
cachedGroupMembers . push ( currentUser . userId )
391
+ cache . set ( `${ groupId } -members` , cachedGroupMembers )
393
392
}
394
393
}
395
- if ( fieldNames ) {
396
- fieldNames . push ( 'subGroups' )
397
- fieldNames . push ( 'parentGroups' )
398
- fieldNames . push ( 'flattenGroupIdTree' )
399
- cachedGroup = _ . pick ( cachedGroup , fieldNames )
400
- }
401
- return cachedGroup
402
394
} else {
403
- const session = helper . createDBSession ( )
404
-
405
- let group = await helper . ensureExists ( session , 'Group' , groupId , isAdmin )
395
+ groupToReturn = await helper . ensureExists ( session , 'Group' , groupId , isAdmin )
396
+ cache . set ( groupId , groupToReturn )
406
397
407
- if ( ! isAdmin ) delete group . status
398
+ if ( ! isAdmin ) delete groupToReturn . status
408
399
409
400
// if the group is private, the user needs to be a member of the group, or an admin
410
- if ( group . privateGroup && currentUser !== 'M2M' && ! helper . hasAdminRole ( currentUser ) ) {
411
- await helper . ensureGroupMember ( session , group . id , currentUser . userId )
401
+ if ( groupToReturn . privateGroup && currentUser !== 'M2M' && ! helper . hasAdminRole ( currentUser ) ) {
402
+ await helper . ensureGroupMember ( session , groupToReturn . id , currentUser . userId )
403
+
404
+ cache . set ( `${ groupId } -members` , [ currentUser . userId ] )
412
405
}
406
+ }
413
407
414
- // get parent or sub groups using breadth first search algorithm,
415
- // this is equivalent to recursive algorithm, but more efficient than latter,
416
- // see https://en.wikipedia.org/wiki/Breadth-first_search
417
- // handled group will be reused, won't be handled duplicately
418
-
419
- // pending group to expand
420
- const pending = [ ]
421
- const expanded = [ ]
422
- if ( criteria . includeSubGroups || criteria . includeParentGroup ) {
423
- pending . push ( group )
424
- while ( pending . length > 0 ) {
425
- const groupToExpand = pending . shift ( )
426
- const found = _ . find ( expanded , ( g ) => g . id === groupToExpand . id )
427
- if ( found ) {
428
- // this group was already expanded, so re-use the fields
429
- groupToExpand . subGroups = found . subGroups
430
- groupToExpand . parentGroups = found . parentGroups
431
- continue
432
- }
433
- expanded . push ( groupToExpand )
434
- if ( criteria . includeSubGroups ) {
435
- // find child groups
436
- groupToExpand . subGroups = await helper . getChildGroups ( session , groupToExpand . id )
408
+ // get parent or sub groups using breadth first search algorithm,
409
+ // this is equivalent to recursive algorithm, but more efficient than latter,
410
+ // see https://en.wikipedia.org/wiki/Breadth-first_search
411
+ // handled group will be reused, won't be handled duplicately
412
+
413
+ // pending group to expand
414
+ const pending = [ ]
415
+ const expanded = [ ]
416
+ if ( criteria . includeSubGroups || criteria . includeParentGroup || criteria . flattenGroupIdTree ) {
417
+ pending . push ( groupToReturn )
418
+ while ( pending . length > 0 ) {
419
+ const groupToExpand = pending . shift ( )
420
+ const found = _ . find ( expanded , ( g ) => g . id === groupToExpand . id )
421
+ if ( found ) {
422
+ // this group was already expanded, so re-use the fields
423
+ groupToExpand . subGroups = found . subGroups
424
+ groupToExpand . parentGroups = found . parentGroups
425
+ continue
426
+ }
427
+ expanded . push ( groupToExpand )
428
+ if ( ( criteria . includeSubGroups && ! groupToReturn . subGroups ) || ( criteria . flattenGroupIdTree && ! groupToReturn . flattenGroupIdTree ) ) {
429
+ const flattenGroupIdTree = [ ]
430
+
431
+ // find child groups
432
+ groupToExpand . subGroups = await helper . getChildGroups ( session , groupToExpand . id )
433
+ // add child groups to pending if needed
434
+ if ( ! criteria . oneLevel ) {
437
435
_ . forEach ( groupToExpand . subGroups , ( g ) => {
438
436
pending . push ( g )
439
437
flattenGroupIdTree . push ( g . id )
440
438
} )
441
- // add child groups to pending if needed
442
- // if (!criteria.oneLevel) {
443
- // _.forEach(groupToExpand.subGroups, (g) => pending.push(g))
444
- // }
445
- } else {
446
- // find parent groups
447
- groupToExpand . parentGroups = await helper . getParentGroups ( session , groupToExpand . id )
439
+
440
+ groupToReturn . flattenGroupIdTree = flattenGroupIdTree
441
+ cache . set ( groupId , groupToReturn )
442
+ }
443
+ } else if ( criteria . includeParentGroup && ! groupToReturn . parentGroups ) {
444
+ // find parent groups
445
+ groupToExpand . parentGroups = await helper . getParentGroups ( session , groupToExpand . id )
446
+ // add parent groups to pending if needed
447
+ if ( ! criteria . oneLevel ) {
448
448
_ . forEach ( groupToExpand . parentGroups , ( g ) => pending . push ( g ) )
449
- // add parent groups to pending if needed
450
- // if (!criteria.oneLevel) {
451
- // _.forEach(groupToExpand.parentGroups, (g) => pending.push(g))
452
- // }
453
449
}
454
450
}
455
451
}
456
- if ( fieldNames ) {
457
- fieldNames . push ( 'subGroups' )
458
- fieldNames . push ( 'parentGroups' )
459
- group = _ . pick ( group , fieldNames )
460
- }
452
+ }
461
453
462
- group . flattenGroupIdTree = flattenGroupIdTree
463
454
464
- cache . set ( groupId , group )
465
- return group
455
+ if ( fieldNames ) {
456
+ fieldNames . push ( 'subGroups' )
457
+ fieldNames . push ( 'parentGroups' )
458
+
459
+ groupToReturn = _ . pick ( groupToReturn , fieldNames )
466
460
}
461
+
462
+ if ( ! criteria . includeSubGroups ) delete groupToReturn . subGroups
463
+ if ( ! criteria . includeParentGroup ) delete groupToReturn . parentGroups
464
+ if ( ! criteria . flattenGroupIdTree ) delete groupToReturn . flattenGroupIdTree
465
+
466
+ return groupToReturn
467
467
} catch ( error ) {
468
468
logger . error ( error )
469
469
throw error
470
470
} finally {
471
471
logger . debug ( 'Session Close' )
472
- if ( session )
473
- await session . close ( )
472
+ await session . close ( )
474
473
}
475
474
}
476
475
@@ -480,6 +479,7 @@ getGroup.schema = {
480
479
criteria : Joi . object ( ) . keys ( {
481
480
includeSubGroups : Joi . boolean ( ) . default ( false ) ,
482
481
includeParentGroup : Joi . boolean ( ) . default ( false ) ,
482
+ flattenGroupIdTree : Joi . boolean ( ) . default ( false ) ,
483
483
oneLevel : Joi . boolean ( ) ,
484
484
fields : Joi . string ( )
485
485
} )
@@ -506,7 +506,7 @@ async function deleteGroup(groupId, isAdmin) {
506
506
await tx . commit ( )
507
507
508
508
// delete the cache
509
- const cache = helper . getCacheInstance ( )
509
+ const cache = await helper . getCacheInstance ( )
510
510
cache . del ( group . id )
511
511
cache . del ( `${ group . id } -members` )
512
512
0 commit comments