@@ -463,6 +463,72 @@ _.assignIn(util, {
463
463
} ) ;
464
464
} ,
465
465
466
+ /**
467
+ * Lookup user handles from multiple emails
468
+ * @param {Object } req request
469
+ * @param {Array } userEmails user emails
470
+ * @param {Number } maximumRequests limit number of request on one batch
471
+ * @param {Boolean } isPattern flag to indicate that pattern matching is required or not
472
+ * @return {Promise } promise
473
+ */
474
+ lookupMultipleUserEmails ( req , userEmails , maximumRequests , isPattern = false ) {
475
+ req . log . debug ( `identityServiceEndpoint: ${ config . get ( 'identityServiceEndpoint' ) } ` ) ;
476
+
477
+ const httpClient = util . getHttpClient ( { id : req . id , log : req . log } ) ;
478
+ // request generator function
479
+ const generateRequest = ( { token, email } ) => {
480
+ let filter = `email=${ email } ` ;
481
+ if ( isPattern ) {
482
+ filter += '&like=true' ;
483
+ }
484
+ return httpClient . get ( `${ config . get ( 'identityServiceEndpoint' ) } users` , {
485
+ headers : {
486
+ Authorization : `Bearer ${ token } ` ,
487
+ Accept : 'application/json' ,
488
+ 'Content-Type' : 'application/json' ,
489
+ } ,
490
+ params : {
491
+ fields : 'handle,id,email' ,
492
+ filter,
493
+ } ,
494
+ // set longer timeout as default 3000 could be not enough for identity service response
495
+ timeout : 15000 ,
496
+ } ) . catch ( ( ) => {
497
+ // in case of any error happens during getting user by email
498
+ // we treat such users as not found and don't return error
499
+ // as per discussion in issue #334
500
+ } ) ;
501
+ } ;
502
+ // send batch of requests, one batch at one time
503
+ const sendBatch = ( options ) => {
504
+ const token = options . token ;
505
+ const emails = options . emails ;
506
+ const users = options . users || [ ] ;
507
+ const batch = options . batch || 0 ;
508
+ const start = batch * maximumRequests ;
509
+ const end = ( batch + 1 ) * maximumRequests ;
510
+ const requests = emails . slice ( start , end ) . map ( userEmail =>
511
+ generateRequest ( { token, email : userEmail } ) ) ;
512
+ return Promise . all ( requests )
513
+ . then ( ( responses ) => {
514
+ const data = responses . reduce ( ( contents , response ) => {
515
+ const content = _ . get ( response , 'data.result.content' , [ ] ) ;
516
+ return _ . concat ( contents , content ) ;
517
+ } , users ) ;
518
+ req . log . debug ( `UserHandle response batch-${ batch } ` , data ) ;
519
+ if ( end < emails . length ) {
520
+ return sendBatch ( { token, users : data , emails, batch : batch + 1 } ) ;
521
+ }
522
+ return data ;
523
+ } ) ;
524
+ } ;
525
+ return util . getM2MToken ( )
526
+ . then ( ( m2mToken ) => {
527
+ req . log . debug ( `Bearer ${ m2mToken } ` ) ;
528
+ return sendBatch ( { token : m2mToken , emails : userEmails } ) ;
529
+ } ) ;
530
+ } ,
531
+
466
532
/**
467
533
* Filter only members of topcoder team
468
534
* @param {Array } members project members
0 commit comments