@@ -42,6 +42,10 @@ const USER_ATTRIBUTE = {
4242 esDocumentId : 'attributes.id'
4343}
4444
45+ const USER_ORGANIZATION = {
46+ esOrganizationQuery : 'externalProfiles.organizationId.keyword'
47+ }
48+
4549const USER_FILTER_TO_MODEL = {
4650 skill : {
4751 name : 'skill' ,
@@ -51,9 +55,7 @@ const USER_FILTER_TO_MODEL = {
5155 esDocumentQuery : 'skills.skillId.keyword' ,
5256 values : [ ]
5357 } ,
54- get skills ( ) {
55- return this . skill
56- } ,
58+ get skills ( ) { return this . skill } ,
5759 achievement : {
5860 name : 'achievement' ,
5961 isAttribute : false ,
@@ -63,9 +65,7 @@ const USER_FILTER_TO_MODEL = {
6365 esDocumentQuery : 'achievements.id.keyword' ,
6466 values : [ ]
6567 } ,
66- get achievements ( ) {
67- return this . achievement
68- } ,
68+ get achievements ( ) { return this . achievement } ,
6969 location : {
7070 name : 'location' ,
7171 isAttribute : true ,
@@ -76,9 +76,7 @@ const USER_FILTER_TO_MODEL = {
7676 defaultSortOrder : 'asc' ,
7777 values : [ ]
7878 } ,
79- get locations ( ) {
80- return this . location
81- } ,
79+ get locations ( ) { return this . location } ,
8280 isavailable : {
8381 name : 'isAvailable' ,
8482 isAttribute : true ,
@@ -280,19 +278,19 @@ const FILTER_CHAIN = {
280278 * @param modelName the model name
281279 * @returns {string|* } the resource
282280 */
283- function getResource ( modelName ) {
281+ function getResource ( modelName ) {
284282 if ( MODEL_TO_RESOURCE [ modelName ] ) {
285283 return MODEL_TO_RESOURCE [ modelName ]
286284 } else {
287285 return modelName . toLowerCase ( )
288286 }
289287}
290288
291- function getTotalCount ( total ) {
289+ function getTotalCount ( total ) {
292290 return typeof total === 'number' ? total : total . value
293291}
294292
295- async function getOrganizationId ( handle ) {
293+ async function getOrganizationId ( handle ) {
296294 const DBHelper = require ( '../models/index' ) . DBHelper
297295
298296 // TODO Use the service method instead of raw query
@@ -309,7 +307,7 @@ async function getOrganizationId(handle) {
309307 throw new Error ( 'User doesn\'t belong to any organization' )
310308}
311309
312- async function getAttributeId ( organizationId , attributeName ) {
310+ async function getAttributeId ( organizationId , attributeName ) {
313311 const DBHelper = require ( '../models/index' ) . DBHelper
314312
315313 // TODO Use the service method instead of raw query
@@ -327,7 +325,7 @@ async function getAttributeId(organizationId, attributeName) {
327325 throw new Error ( `Attribute ${ attributeName } is invalid for the current users organization` )
328326}
329327
330- function parseUserFilter ( params ) {
328+ function parseUserFilter ( params ) {
331329 const filters = { }
332330 _ . forOwn ( params , ( value , key ) => {
333331 key = key . toLowerCase ( )
@@ -349,7 +347,7 @@ function parseUserFilter(params) {
349347 * resources which are not in user index.
350348 * @returns {{} } the resource filters keyed by resource
351349 */
352- function parseEnrichFilter ( params ) {
350+ function parseEnrichFilter ( params ) {
353351 const filters = { }
354352 _ . forOwn ( params , ( value , key ) => {
355353 if ( key . includes ( '.' ) ) {
@@ -383,7 +381,7 @@ function parseEnrichFilter(params) {
383381 * @param item the parent object
384382 * @returns {Promise<void> } the promise of enriched parent object
385383 */
386- async function enrichResource ( resource , enrichIdProp , item ) {
384+ async function enrichResource ( resource , enrichIdProp , item ) {
387385 const subDoc = DOCUMENTS [ resource ]
388386 const filterChain = FILTER_CHAIN [ resource ]
389387 const subResult = await esClient . getSource ( {
@@ -409,7 +407,7 @@ async function enrichResource(resource, enrichIdProp, item) {
409407 * @param user the user object to enrich
410408 * @returns {Promise<*> } the promise of enriched user
411409 */
412- async function enrichUser ( user ) {
410+ async function enrichUser ( user ) {
413411 for ( const subProp of Object . keys ( SUB_DOCUMENTS ) ) {
414412 const subDoc = SUB_DOCUMENTS [ subProp ]
415413 const subData = user [ subDoc . userField ]
@@ -429,7 +427,7 @@ async function enrichUser(user) {
429427 * @param users list of users from ES search
430428 * @returns {Promise<*> } the enriched users
431429 */
432- async function enrichUsers ( users ) {
430+ async function enrichUsers ( users ) {
433431 const enrichedUsers = [ ]
434432 for ( const user of users ) {
435433 const enriched = await enrichUser ( user )
@@ -444,7 +442,7 @@ async function enrichUsers(users) {
444442 * @param args the request path and query parameters
445443 * @returns {Promise<*> } the promise of retrieved resource object from ES
446444 */
447- async function getFromElasticSearch ( resource , ...args ) {
445+ async function getFromElasticSearch ( resource , ...args ) {
448446 logger . debug ( `Get from ES first: args ${ JSON . stringify ( args , null , 2 ) } ` )
449447 const id = args [ 0 ]
450448 // path and query parameters
@@ -500,7 +498,7 @@ async function getFromElasticSearch(resource, ...args) {
500498 * @param itself true mean the filter is applied the resource itself
501499 * @returns {[] } parsed filter array
502500 */
503- function parseResourceFilter ( resource , params , itself ) {
501+ function parseResourceFilter ( resource , params , itself ) {
504502 const resDefinedFilters = RESOURCE_FILTER [ resource ]
505503 const resFilters = [ ]
506504 if ( resDefinedFilters ) {
@@ -535,7 +533,7 @@ function parseResourceFilter(resource, params, itself) {
535533 * @param resFilters the resource filters
536534 * @param esQuery the ES query
537535 */
538- function setResourceFilterToEsQuery ( resFilters , esQuery ) {
536+ function setResourceFilterToEsQuery ( resFilters , esQuery ) {
539537 // TODO only current res filter
540538 if ( resFilters . length > 0 ) {
541539 for ( const filter of resFilters ) {
@@ -561,7 +559,7 @@ function setResourceFilterToEsQuery(resFilters, esQuery) {
561559 * @param queryField the field that the filter applies
562560 * @returns {* } the ES query
563561 */
564- function setFilterValueToEsQuery ( esQuery , matchField , filterValue , queryField ) {
562+ function setFilterValueToEsQuery ( esQuery , matchField , filterValue , queryField ) {
565563 if ( queryField !== 'name' ) {
566564 matchField = matchField + '.keyword'
567565 }
@@ -590,7 +588,7 @@ function setFilterValueToEsQuery(esQuery, matchField, filterValue, queryField) {
590588 * @param filterClause the filter clause of the ES query
591589 * @param attributes array of attribute id and value objects
592590 */
593- function setUserAttributesFiltersToEsQuery ( filterClause , attributes ) {
591+ function setUserAttributesFiltersToEsQuery ( filterClause , attributes ) {
594592 for ( const attribute of attributes ) {
595593 if ( typeof attribute . value !== 'object' ) {
596594 attribute . value = [ attribute . value ]
@@ -622,7 +620,15 @@ function setUserAttributesFiltersToEsQuery(filterClause, attributes) {
622620 }
623621}
624622
625- function hasNonAlphaNumeric ( text ) {
623+ function setUserOrganizationFiilterToEsQuery ( filterClause , organizationId ) {
624+ filterClause . push ( {
625+ term : {
626+ [ USER_ORGANIZATION . esOrganizationQuery ] : organizationId
627+ }
628+ } )
629+ }
630+
631+ function hasNonAlphaNumeric ( text ) {
626632 const regex = / ^ [ A - Z a - z 0 - 9 ] + $ /
627633 return ! regex . test ( text )
628634}
@@ -633,7 +639,7 @@ function hasNonAlphaNumeric(text) {
633639 * @param keyword the search keyword
634640 * @returns array of skillIds
635641 */
636- async function searchSkills ( keyword ) {
642+ async function searchSkills ( keyword ) {
637643 const queryDoc = DOCUMENTS . skill
638644
639645 const query = hasNonAlphaNumeric ( keyword ) ? `\\*${ keyword } \\*` : `*${ keyword } *`
@@ -658,7 +664,7 @@ async function searchSkills(keyword) {
658664 return results . hits . hits . map ( hit => hit . _source . id )
659665}
660666
661- async function setUserSearchClausesToEsQuery ( boolClause , keyword ) {
667+ async function setUserSearchClausesToEsQuery ( boolClause , keyword ) {
662668 const skillIds = await searchSkills ( keyword )
663669
664670 boolClause . should . push ( {
@@ -701,7 +707,7 @@ async function setUserSearchClausesToEsQuery(boolClause, keyword) {
701707 * @param filter
702708 * @returns {{} } created ES query object
703709 */
704- function buildEsQueryFromFilter ( filter ) {
710+ function buildEsQueryFromFilter ( filter ) {
705711 const queryDoc = DOCUMENTS [ filter . resource ]
706712 const esQuery = {
707713 index : queryDoc . index ,
@@ -728,7 +734,7 @@ function buildEsQueryFromFilter(filter) {
728734 * @param size the number of attributes to fetch
729735 * @return {{} } created ES query object
730736 */
731- function buildEsQueryToGetAttributeValues ( attributeId , attributeValue , size ) {
737+ function buildEsQueryToGetAttributeValues ( attributeId , attributeValue , size ) {
732738 const queryDoc = DOCUMENTS . user
733739
734740 const esQuery = {
@@ -777,9 +783,7 @@ function buildEsQueryToGetAttributeValues(attributeId, attributeValue, size) {
777783 return esQuery
778784}
779785
780- async function resolveUserFilterFromDb ( filter , {
781- handle
782- } , organizationId ) {
786+ async function resolveUserFilterFromDb ( filter , { handle } , organizationId ) {
783787 const DBHelper = require ( '../models/index' ) . DBHelper
784788
785789 if ( filter . isAttribute ) {
@@ -855,9 +859,7 @@ async function resolveUserFilterFromDb(filter, {
855859
856860 const results = await DBHelper . find ( model , dbQueries )
857861 if ( results . length > 0 ) {
858- for ( const {
859- id
860- } of results ) {
862+ for ( const { id } of results ) {
861863 esQueryClause . bool . should . push ( {
862864 term : {
863865 [ filter . esDocumentQuery ] : id
@@ -872,9 +874,7 @@ async function resolveUserFilterFromDb(filter, {
872874 }
873875}
874876
875- async function resolveSortClauseFromDb ( orderBy , {
876- handle
877- } , organizationId ) {
877+ async function resolveSortClauseFromDb ( orderBy , { handle } , organizationId ) {
878878 if ( orderBy === 'name' ) {
879879 return [ {
880880 'firstName.keyword' : 'asc'
@@ -913,7 +913,7 @@ async function resolveSortClauseFromDb(orderBy, {
913913 * @param initialRes the resource that initial request comes in
914914 * @returns {Promise<*> } the resolved value
915915 */
916- async function resolveResFilter ( filter , initialRes ) {
916+ async function resolveResFilter ( filter , initialRes ) {
917917 const doc = DOCUMENTS [ filter . resource ]
918918 const filterChain = FILTER_CHAIN [ filter . resource ]
919919
@@ -961,7 +961,7 @@ async function resolveResFilter(filter, initialRes) {
961961 * @param perPage
962962 * @returns {* }
963963 */
964- function applySubResFilters ( results , preResFilterResults , ownResFilters , perPage ) {
964+ function applySubResFilters ( results , preResFilterResults , ownResFilters , perPage ) {
965965 let count = 0
966966 const filtered = results . filter ( item => {
967967 for ( const filter of preResFilterResults ) {
@@ -989,7 +989,7 @@ function applySubResFilters(results, preResFilterResults, ownResFilters, perPage
989989 * @param args the request path and query parameters
990990 * @returns {Promise<*> } the promise of searched results
991991 */
992- async function searchElasticSearch ( resource , ...args ) {
992+ async function searchElasticSearch ( resource , ...args ) {
993993 const {
994994 checkIfExists,
995995 getAuthUser
@@ -1171,10 +1171,8 @@ async function searchElasticSearch(resource, ...args) {
11711171 }
11721172}
11731173
1174- async function publishMessage ( op , resource , result ) {
1175- const {
1176- postEvent
1177- } = require ( './helper' )
1174+ async function publishMessage ( op , resource , result ) {
1175+ const { postEvent } = require ( './helper' )
11781176
11791177 if ( ! OP_TO_TOPIC [ op ] ) {
11801178 logger . warn ( `Invalid operation: ${ op } ` )
@@ -1184,9 +1182,7 @@ async function publishMessage(op, resource, result) {
11841182 logger . debug ( `Publishing message to bus: resource ${ resource } , data ${ JSON . stringify ( result , null , 2 ) } ` )
11851183
11861184 // Send Kafka message using bus api
1187- await postEvent ( OP_TO_TOPIC [ op ] , _ . assign ( {
1188- resource : resource
1189- } , result ) )
1185+ await postEvent ( OP_TO_TOPIC [ op ] , _ . assign ( { resource } , result ) )
11901186}
11911187
11921188/**
@@ -1196,7 +1192,7 @@ async function publishMessage(op, resource, result) {
11961192 * @param Model the model to access
11971193 * @returns {{} } ES wrapped CRUD methods
11981194 */
1199- function wrapElasticSearchOp ( methods , Model ) {
1195+ function wrapElasticSearchOp ( methods , Model ) {
12001196 logger . info ( 'Decorating ES to API methods' )
12011197
12021198 // methods: create, search, patch, get, remove
@@ -1213,10 +1209,7 @@ function wrapElasticSearchOp(methods, Model) {
12131209 throw errors . elasticSearchEnrichError ( err . message )
12141210 }
12151211 logger . logFullError ( err )
1216- const {
1217- items,
1218- meta
1219- } = await func ( ...args )
1212+ const { items, meta } = await func ( ...args )
12201213 // return fromDB:true to indicate it is got from db,
12211214 // and response headers ('X-Total', 'X-Page', etc.) are not set in this case
12221215 return {
@@ -1227,9 +1220,7 @@ function wrapElasticSearchOp(methods, Model) {
12271220 }
12281221 }
12291222 } else if ( func . name === 'get' ) {
1230- const {
1231- permissionCheck
1232- } = require ( './helper' )
1223+ const { permissionCheck } = require ( './helper' )
12331224 return async ( ...args ) => {
12341225 if ( args [ 3 ] ) {
12351226 // merge query to params if exists. req.query was added at the end not to break the existing QLDB code.
@@ -1275,11 +1266,8 @@ function wrapElasticSearchOp(methods, Model) {
12751266 } )
12761267}
12771268
1278- async function searchUsers ( authUser , filter , params ) {
1279- const {
1280- checkIfExists,
1281- getAuthUser
1282- } = require ( './helper' )
1269+ async function searchUsers ( authUser , filter , params ) {
1270+ const { checkIfExists, getAuthUser } = require ( './helper' )
12831271 const queryDoc = DOCUMENTS . user
12841272
12851273 if ( ! params . page ) {
@@ -1340,6 +1328,10 @@ async function searchUsers(authUser, filter, params) {
13401328 setUserAttributesFiltersToEsQuery ( esQuery . body . query . bool . filter , filter . attributes )
13411329 }
13421330
1331+ if ( filter . organizationId != null ) {
1332+ setUserOrganizationFiilterToEsQuery ( esQuery . body . query . bool . filter , filter . organizationId )
1333+ }
1334+
13431335 logger . debug ( `ES query for searching users: ${ JSON . stringify ( esQuery , null , 2 ) } ` )
13441336
13451337 const docs = await esClient . search ( esQuery )
@@ -1362,10 +1354,7 @@ async function searchUsers(authUser, filter, params) {
13621354 }
13631355}
13641356
1365- async function searchAttributeValues ( {
1366- attributeId,
1367- attributeValue
1368- } ) {
1357+ async function searchAttributeValues ( { attributeId, attributeValue } ) {
13691358 const esQuery = buildEsQueryToGetAttributeValues ( attributeId , attributeValue , 5 )
13701359 logger . debug ( `ES query for searching attribute values: ${ JSON . stringify ( esQuery , null , 2 ) } ` )
13711360
@@ -1387,13 +1376,11 @@ async function searchAttributeValues({
13871376 }
13881377 }
13891378
1390- return {
1391- result
1392- }
1379+ return { result }
13931380}
13941381
13951382module . exports = {
13961383 wrapElasticSearchOp,
13971384 searchUsers,
13981385 searchAttributeValues
1399- }
1386+ }
0 commit comments