@@ -6,10 +6,14 @@ const _ = require('lodash')
6
6
const config = require ( 'config' )
7
7
const request = require ( 'superagent' )
8
8
const logger = require ( './logger' )
9
- const m2mAuth = require ( 'tc-core-library-js' ) . auth . m2m ;
10
- const m2m = m2mAuth ( config ) ;
9
+ const m2mAuth = require ( 'tc-core-library-js' ) . auth . m2m
10
+ const NodeCache = require ( 'node-cache' )
11
+
12
+ const m2m = m2mAuth ( config )
13
+ const cache = new NodeCache ( )
11
14
12
15
const logPrefix = "BroadcastAPI: "
16
+ const cachedTimeInSeconds = 300 //300 seconds
13
17
14
18
/**
15
19
* Helper Function - get m2m token
@@ -27,6 +31,11 @@ async function getMemberInfo(userId) {
27
31
"/members/_search/?" +
28
32
`query=userId%3A${ userId } ` +
29
33
`&limit=1`
34
+ if ( cachedMemberInfo = cache . get ( url ) ) {
35
+ return new Promise ( ( resolve , reject ) => {
36
+ resolve ( cachedMemberInfo )
37
+ } )
38
+ }
30
39
return new Promise ( async function ( resolve , reject ) {
31
40
let memberInfo = [ ]
32
41
logger . info ( `calling member api ${ url } ` )
@@ -37,6 +46,7 @@ async function getMemberInfo(userId) {
37
46
}
38
47
memberInfo = _ . get ( res , 'body.result.content' )
39
48
logger . info ( `BCA Memeber API: Feteched ${ memberInfo . length } record(s) from member api` )
49
+ cache . set ( url , memberInfo , cachedTimeInSeconds )
40
50
resolve ( memberInfo )
41
51
} catch ( err ) {
42
52
reject ( new Error ( `BCA Memeber API: Failed to get member ` +
@@ -102,12 +112,16 @@ async function callApi(url, machineToken) {
102
112
103
113
/**
104
114
* Helper function - check Skills and Tracks condition
115
+ *
116
+ * @param {Integer } userId
117
+ * @param {Object } bulkMessage
118
+ * @param {Object } m memberInfo
119
+ *
105
120
*/
106
- async function checkUserSkillsAndTracks ( userId , bulkMessage ) {
121
+ async function checkUserSkillsAndTracks ( userId , bulkMessage , m ) {
107
122
try {
108
123
const skills = _ . get ( bulkMessage , 'recipients.skills' )
109
124
const tracks = _ . get ( bulkMessage , 'recipients.tracks' )
110
- const m = await getMemberInfo ( userId )
111
125
let skillMatch , trackMatch = false // default
112
126
if ( skills && skills . length > 0 ) {
113
127
const ms = _ . get ( m [ 0 ] , "skills" ) // get member skills
@@ -159,25 +173,43 @@ async function checkUserSkillsAndTracks(userId, bulkMessage) {
159
173
/**
160
174
* Helper function - check group condition
161
175
*/
162
- async function checkUserGroup ( userId , bulkMessage ) {
176
+ async function checkUserGroup ( userId , bulkMessage , userGroupInfo ) {
163
177
try {
178
+ const excludeGroupSign = '!'
164
179
const groups = _ . get ( bulkMessage , 'recipients.groups' )
180
+
165
181
let flag = false // default
166
- const userGroupInfo = await getUserGroup ( userId )
167
- if ( groups . length > 0 ) {
182
+ let excludeGroups = [ ]
183
+ let includeGroups = [ ]
184
+
185
+ _ . map ( groups , ( g ) => {
186
+ if ( _ . startsWith ( g , excludeGroupSign ) ) {
187
+ excludeGroups . push ( g )
188
+ } else {
189
+ includeGroups . push ( g )
190
+ }
191
+ } )
192
+
193
+ if ( includeGroups . length > 0 ) {
168
194
_ . map ( userGroupInfo , ( o ) => {
169
195
// particular group only condition
170
- flag = ( _ . indexOf ( groups , _ . get ( o , "name" ) ) >= 0 ) ? true : flag
196
+ flag = ( _ . indexOf ( includeGroups , _ . get ( o , "name" ) ) >= 0 ) ? true : flag
171
197
} )
172
- } else { // no group condition means its for `public` no private group
198
+ }
199
+ if ( excludeGroups . length > 0 ) {
173
200
flag = true // default allow for all
174
201
_ . map ( userGroupInfo , ( o ) => {
175
- // not allow if user is part of any private group
176
- flag = ( _ . get ( o , "privateGroup" ) ) ? false : flag
202
+ // not allow if user is part of any private group i.e. excludeGroups
203
+ flag = ( _ . indexOf ( excludeGroups , ( excludeGroupSign + _ . get ( o , "name" ) ) ) >= 0 ) ? false : flag
177
204
} )
178
205
logger . info ( `public group condition for userId ${ userId } ` +
179
206
` and BC messageId ${ bulkMessage . id } , the result is: ${ flag } ` )
180
207
}
208
+
209
+ if ( groups . length === 0 ) {
210
+ flag = true // no restriction
211
+ }
212
+
181
213
return flag
182
214
} catch ( e ) {
183
215
throw new Error ( `checkUserGroup(): ${ e } ` )
@@ -189,12 +221,16 @@ async function checkUserGroup(userId, bulkMessage) {
189
221
*
190
222
* @param {Integer } userId
191
223
* @param {Object } bulkMessage
224
+ * @param {Object } memberInfo
225
+ * @param {Object } userGroupInfo
226
+ *
227
+ * @return Promise
192
228
*/
193
- async function checkBroadcastMessageForUser ( userId , bulkMessage ) {
229
+ async function checkBroadcastMessageForUser ( userId , bulkMessage , memberInfo , userGroupInfo ) {
194
230
return new Promise ( function ( resolve , reject ) {
195
231
Promise . all ( [
196
- checkUserSkillsAndTracks ( userId , bulkMessage ) ,
197
- checkUserGroup ( userId , bulkMessage ) ,
232
+ checkUserSkillsAndTracks ( userId , bulkMessage , memberInfo ) ,
233
+ checkUserGroup ( userId , bulkMessage , userGroupInfo ) ,
198
234
] ) . then ( ( results ) => {
199
235
let flag = true // TODO need to be sure about default value
200
236
_ . map ( results , ( r ) => {
@@ -214,4 +250,6 @@ async function checkBroadcastMessageForUser(userId, bulkMessage) {
214
250
215
251
module . exports = {
216
252
checkBroadcastMessageForUser,
253
+ getMemberInfo,
254
+ getUserGroup,
217
255
}
0 commit comments