Skip to content

Commit 83490b3

Browse files
author
Sachin Maheshwari
committed
adding track conditions
1 parent 62cf1a1 commit 83490b3

File tree

1 file changed

+73
-52
lines changed

1 file changed

+73
-52
lines changed

src/common/broadcastAPIHelper.js

Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ async function getM2MToken() {
2525
async function getMemberInfo(userId) {
2626
const url = config.TC_API_V3_BASE_URL +
2727
"/members/_search/?" +
28-
"fields=userId%2Cskills" +
29-
`&query=userId%3A${userId}` +
28+
`query=userId%3A${userId}` +
3029
`&limit=1`
3130
return new Promise(async function (resolve, reject) {
3231
let memberInfo = []
@@ -102,64 +101,86 @@ async function callApi(url, machineToken) {
102101
}
103102

104103
/**
105-
* Helper function - check Skill condition
104+
* Helper function - check Skills and Tracks condition
106105
*/
107-
async function checkUserSkill(userId, bulkMessage) {
108-
return new Promise(async function (resolve, reject) {
109-
try {
110-
const skills = _.get(bulkMessage, 'recipients.skills')
111-
let flag = true // allow for all
112-
if (skills && skills.length > 0) {
113-
const m = await getMemberInfo(userId)
114-
const ms = _.get(m[0], "skills") // get member skills
115-
const memberSkills = []
116-
flag = false
117-
_.map(ms, (o) => {
118-
memberSkills.push(_.get(o, 'name').toLowerCase())
119-
})
120-
_.map(skills, (s) => {
121-
if (_.indexOf(memberSkills, s.toLowerCase()) >= 0) {
122-
flag = true
123-
logger.info(`BroadcastMessageId: ${bulkMessage.id},` +
124-
` '${s}' skill matached for user id ${userId}`)
125-
}
126-
})
127-
}
128-
resolve(flag)
129-
} catch (e) {
130-
reject(e)
106+
async function checkUserSkillsAndTracks(userId, bulkMessage) {
107+
try {
108+
const skills = _.get(bulkMessage, 'recipients.skills')
109+
const tracks = _.get(bulkMessage, 'recipients.tracks')
110+
const m = await getMemberInfo(userId)
111+
let skillMatch, trackMatch = false // default
112+
if (skills && skills.length > 0) {
113+
const ms = _.get(m[0], "skills") // get member skills
114+
const memberSkills = []
115+
skillMatch = false
116+
_.map(ms, (o) => {
117+
memberSkills.push(_.get(o, 'name').toLowerCase())
118+
})
119+
_.map(skills, (s) => {
120+
if (_.indexOf(memberSkills, s.toLowerCase()) >= 0) {
121+
skillMatch = true
122+
logger.info(`BroadcastMessageId: ${bulkMessage.id},` +
123+
` '${s}' skill matached for user id ${userId}`)
124+
}
125+
})
126+
} else {
127+
skillMatch = true // no condition, means allow for all
131128
}
132-
}) // promise end
129+
130+
//
131+
if (tracks.length > 0) {
132+
trackMatch = false
133+
const uDevChallenges = _.get(m[0], "stats[0].DEVELOP.challenges1")
134+
const uDesignChallenges = _.get(m[0], "stats[0].DEVELOP.challenges")
135+
const uDSChallenges = _.get(m[0], "stats[0].DEVELOP.challenges")
136+
_.map(tracks, (t) => {
137+
/**
138+
* checking if user participated in specific challenges
139+
*/
140+
if (t.equalsIgnoreCase("DEVELOP")) {
141+
trackMatch = uDevChallenges > 0 ? true : trackMatch
142+
} else if (t.equalsIgnoreCase("DESIGN")) {
143+
trackMatch = uDesignChallenges > 0 ? true : trackMatch
144+
} else if (t.equalsIgnoreCase("DATA_SCIENCE")) {
145+
trackMatch = uDSChallenges > 0 ? true : trackMatch
146+
}
147+
})
148+
} else {
149+
trackMatch = true // no condition, means allow for all
150+
}
151+
const flag = (skillMatch && trackMatch) ? true : false
152+
return flag
153+
} catch (e) {
154+
throw new Error(`checkUserSkillsAndTracks() : ${e}`)
155+
}
133156
}
134157

135158
/**
136159
* Helper function - check group condition
137160
*/
138161
async function checkUserGroup(userId, bulkMessage) {
139-
return new Promise(async function (resolve, reject) {
140-
try {
141-
const groups = _.get(bulkMessage, 'recipients.groups')
142-
let flag = false // default
143-
const userGroupInfo = await getUserGroup(userId)
144-
if (groups.length > 0) {
145-
_.map(userGroupInfo, (o) => {
146-
// particular group only condition
147-
flag = (_.indexOf(groups, _.get(o, "name")) >= 0) ? true : flag
148-
})
149-
} else { // no group condition means its for `public` no private group
150-
flag = true // default allow for all
151-
_.map(userGroupInfo, (o) => {
152-
// not allow if user is part of any private group
153-
flag = (_.get(o, "privateGroup")) ? false : flag
154-
})
155-
logger.info(`public group condition for userId ${userId}` +
156-
` and BC messageId ${bulkMessage.id}, the result is: ${flag}`)
157-
}
158-
resolve(flag)
159-
} catch (e) {
160-
reject(e)
162+
try {
163+
const groups = _.get(bulkMessage, 'recipients.groups')
164+
let flag = false // default
165+
const userGroupInfo = await getUserGroup(userId)
166+
if (groups.length > 0) {
167+
_.map(userGroupInfo, (o) => {
168+
// particular group only condition
169+
flag = (_.indexOf(groups, _.get(o, "name")) >= 0) ? true : flag
170+
})
171+
} else { // no group condition means its for `public` no private group
172+
flag = true // default allow for all
173+
_.map(userGroupInfo, (o) => {
174+
// not allow if user is part of any private group
175+
flag = (_.get(o, "privateGroup")) ? false : flag
176+
})
177+
logger.info(`public group condition for userId ${userId}` +
178+
` and BC messageId ${bulkMessage.id}, the result is: ${flag}`)
161179
}
162-
})
180+
return flag
181+
} catch (e) {
182+
throw new Error(`checkUserGroup(): ${e}`)
183+
}
163184
}
164185

165186
/**
@@ -171,7 +192,7 @@ async function checkUserGroup(userId, bulkMessage) {
171192
async function checkBroadcastMessageForUser(userId, bulkMessage) {
172193
return new Promise(function (resolve, reject) {
173194
Promise.all([
174-
checkUserSkill(userId, bulkMessage),
195+
checkUserSkillsAndTracks(userId, bulkMessage),
175196
checkUserGroup(userId, bulkMessage),
176197
]).then((results) => {
177198
let flag = true // TODO need to be sure about default value

0 commit comments

Comments
 (0)