Skip to content

Commit 62cf1a1

Browse files
author
Sachin Maheshwari
committed
changes in group api calling..
1 parent a036f9f commit 62cf1a1

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

src/common/broadcastAPIHelper.js

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
*
2+
* Broadcast: API calling helper
33
*/
44

55
const _ = require('lodash')
@@ -40,7 +40,8 @@ async function getMemberInfo(userId) {
4040
logger.info(`BCA Memeber API: Feteched ${memberInfo.length} record(s) from member api`)
4141
resolve(memberInfo)
4242
} catch (err) {
43-
reject(new Error(`BCA Memeber API: Failed to get member api detail for user id ${userId}, ${err}`))
43+
reject(new Error(`BCA Memeber API: Failed to get member ` +
44+
`api detail for user id ${userId}, ${err}`))
4445
}
4546

4647
})
@@ -51,26 +52,53 @@ async function getMemberInfo(userId) {
5152
* @param {Integer} userId
5253
*/
5354
async function getUserGroup(userId) {
54-
//TODO need to take care of pagination
55-
const url = config.TC_API_V5_BASE_URL +
56-
`/groups/?memberId=${userId}` +
57-
"&membershipType=user&page=1"
58-
let groupInfo = []
59-
return new Promise(async (resolve, reject) => {
60-
try {
61-
const machineToken = await getM2MToken()
62-
//logger.info(`BCA Group API: got m2m token of length ${machineToken.length}`)
63-
const res = await request.get(url).set('Authorization', `Bearer ${machineToken}`);
64-
if (_.get(res, 'res.statusCode') != 200) {
65-
reject(new Error(`BCA Group API: Failed to get group detail for user id ${userId}`))
66-
}
67-
groupInfo = _.get(res, 'body')
68-
logger.info(`BCA Group API: Feteched ${groupInfo.length} record(s) from group api`)
69-
resolve(groupInfo)
70-
} catch (e) {
71-
reject(`Calling group api ${e}`)
55+
try {
56+
const machineToken = await getM2MToken()
57+
if (machineToken.length <= 0) {
58+
return (new Error(`BCA Group API: fecthing m2m token failed for ${userId}`))
7259
}
73-
})
60+
let nextPage
61+
let res
62+
let url
63+
let page = 1
64+
let groupInfo = []
65+
const perPage = 100
66+
do {
67+
url = config.TC_API_V5_BASE_URL +
68+
`/groups/?memberId=${userId}&membershipType=user` +
69+
`&page=${page}&perPage=${perPage}`
70+
res = await callApi(url, machineToken)
71+
let resStatus = _.get(res, 'res.statusCode')
72+
if (resStatus != 200) {
73+
throw new Error(`BCA Group API: Failed for user id ${userId},` +
74+
` response status ${resStatus}`)
75+
}
76+
let data = _.get(res, 'body')
77+
groupInfo = groupInfo.concat(data)
78+
nextPage = _.get(res, 'header.x-next-page')
79+
page = nextPage
80+
} while (nextPage)
81+
logger.info(`BCA Group API: Feteched ${groupInfo.length} record(s) from group api`)
82+
return groupInfo
83+
} catch (e) {
84+
logger.error(`BCA: Error calling group api : ${e}`)
85+
throw new Error(`getUserGroup() : ${e}`)
86+
}
87+
}
88+
89+
/**
90+
*
91+
* @param {String} url
92+
* @param {String} machineToken
93+
*/
94+
async function callApi(url, machineToken) {
95+
try {
96+
logger.info(`calling api url ${url}`)
97+
return request.get(url).set('Authorization', `Bearer ${machineToken}`)
98+
} catch (e) {
99+
logger.error(`Error in calling URL ${url}, ${e}`)
100+
throw new Error(`callApi() : ${e}`)
101+
}
74102
}
75103

76104
/**
@@ -92,7 +120,8 @@ async function checkUserSkill(userId, bulkMessage) {
92120
_.map(skills, (s) => {
93121
if (_.indexOf(memberSkills, s.toLowerCase()) >= 0) {
94122
flag = true
95-
logger.info(`BroadcastMessageId: ${bulkMessage.id}, '${s}' skill matached for user id ${userId}`)
123+
logger.info(`BroadcastMessageId: ${bulkMessage.id},` +
124+
` '${s}' skill matached for user id ${userId}`)
96125
}
97126
})
98127
}
@@ -123,6 +152,8 @@ async function checkUserGroup(userId, bulkMessage) {
123152
// not allow if user is part of any private group
124153
flag = (_.get(o, "privateGroup")) ? false : flag
125154
})
155+
logger.info(`public group condition for userId ${userId}` +
156+
` and BC messageId ${bulkMessage.id}, the result is: ${flag}`)
126157
}
127158
resolve(flag)
128159
} catch (e) {
@@ -147,7 +178,8 @@ async function checkBroadcastMessageForUser(userId, bulkMessage) {
147178
_.map(results, (r) => {
148179
flag = !r ? false : flag // TODO recheck condition
149180
})
150-
logger.info(`BCA: Final recepient condition result is: ${flag} for userId ${userId}`)
181+
logger.info(`BCA: messageId: ${bulkMessage.id} Final recipient` +
182+
` condition result is: ${flag} for userId ${userId}`)
151183
resolve({
152184
record: bulkMessage,
153185
result: flag

0 commit comments

Comments
 (0)