Skip to content

Commit 8e48501

Browse files
paginate through the results when fetching members challenges
1 parent f647c1f commit 8e48501

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/common/helper.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,28 @@ function calculateChallengeEndDate (challenge, data) {
693693
*/
694694
async function listChallengesByMember (memberId) {
695695
const token = await getM2MToken()
696-
const url = `${config.RESOURCES_API_URL}/${memberId}/challenges?perPage=10000`
697-
const res = await axios.get(url, { headers: { Authorization: `Bearer ${token}` } })
698-
return res.data || []
696+
let allIds = []
697+
// get search is paginated, we need to get all pages' data
698+
let page = 1
699+
while (true) {
700+
const result = await axios.get(`${config.RESOURCES_API_URL}/${memberId}/challenges`, {
701+
headers: { Authorization: `Bearer ${token}` },
702+
params: {
703+
page,
704+
perPage: 10000
705+
}
706+
})
707+
const ids = result.data || []
708+
if (ids.length === 0) {
709+
break
710+
}
711+
allIds = allIds.concat(ids)
712+
page += 1
713+
if (result.headers['x-total-pages'] && page > Number(result.headers['x-total-pages'])) {
714+
break
715+
}
716+
}
717+
return allIds
699718
}
700719

701720
/**

0 commit comments

Comments
 (0)