Skip to content

Commit a7bac0e

Browse files
committed
additional fixes
1 parent 7c25429 commit a7bac0e

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/services/challenges.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,16 @@ class ChallengesService {
112112
*/
113113
const getMemberChallenges = async (
114114
endpoint,
115+
filters = {},
115116
params = {},
116117
) => {
117118
const memberId = decodeToken(this.private.tokenV3).userId;
118119
const query = {
119120
...params,
121+
...filters,
120122
memberId,
121123
};
122-
const url = `${endpoint}?${qs.stringify(query)}`;
124+
const url = `${endpoint}?${qs.stringify(_.omit(query, ['limit', 'offset', 'technologies']))}`;
123125
const res = await this.private.apiV5.get(url).then(checkError);
124126
const totalCount = res.length;
125127
return {
@@ -132,6 +134,7 @@ class ChallengesService {
132134
api: getApi('V4', tokenV3),
133135
apiV5: getApi('V5', tokenV3),
134136
apiV2: getApi('V2', tokenV2),
137+
apiV3: getApi('V3', tokenV3),
135138
getChallenges,
136139
getMemberChallenges,
137140
tokenV2,
@@ -337,32 +340,39 @@ class ChallengesService {
337340

338341
/**
339342
* Gets challenges of the specified user.
340-
* @param {String} username User whose challenges we want to fetch.
343+
* @param {String} userId User id whose challenges we want to fetch.
341344
* @param {Object} filters Optional.
342345
* @param {Number} params Optional.
343346
* @return {Promise} Resolves to the api response.
344347
*/
345-
getUserChallenges(username, filters, params) {
346-
// FIXME: This has not been updated to use the V5 API
348+
getUserChallenges(userId, filters, params) {
347349
const userFilters = _.cloneDeep(filters);
348350
ChallengesService.updateFiltersParamsForGettingMemberChallenges(userFilters, params);
351+
const query = {
352+
...params,
353+
...userFilters,
354+
memberId: userId,
355+
};
349356
const endpoint = '/challenges';
350-
const res = this.private.getMemberChallenges(endpoint);
357+
const url = `${endpoint}?${qs.stringify(_.omit(query, ['limit', 'offset', 'technologies']))}`;
358+
359+
const res = this.private.apiV5.get(url);
351360
return res;
352361
}
353362

354363
/**
355364
* Gets marathon matches of the specified user.
356-
* @param {String} username User whose challenges we want to fetch.
365+
* @param {String} userId User whose challenges we want to fetch.
357366
* @param {Object} filters Optional.
358367
* @param {Number} params Optional.
359368
* @return {Promise} Resolves to the api response.
360369
*/
361-
getUserMarathonMatches(username, filters, params) {
362-
// FIXME: This has not been updated to use the V5 API
363-
ChallengesService.updateFiltersParamsForGettingMemberChallenges(filters, params);
364-
const endpoint = `/members/${username.toLowerCase()}/mms/`;
365-
return this.private.getMemberChallenges(endpoint);
370+
async getUserMarathonMatches(userId) {
371+
const marathonTypeId = 'c2579605-e294-4967-b3db-875ef85240cd';
372+
const url = `/challenges?typeId=${marathonTypeId}&memberId=${userId}`;
373+
374+
const res = await this.private.apiV5.get(url);
375+
return res;
366376
}
367377

368378
/**
@@ -484,9 +494,9 @@ class ChallengesService {
484494
* @return {Promise}
485495
*/
486496
async updateChallenge(challenge) {
487-
const URL = `/challenges/${challenge.id}`;
488-
const body = { param: challenge };
489-
let res = await this.private.api.putJson(URL, body);
497+
const url = `/challenges/${challenge.id}`;
498+
const body = { body: challenge };
499+
let res = await this.private.apiV5.put(url, body);
490500
if (!res.ok) throw new Error(res.statusText);
491501
res = (await res.json()).result;
492502
if (res.status !== 200) throw new Error(res.content);
@@ -507,10 +517,10 @@ class ChallengesService {
507517
*/
508518
async getUserRolesInChallenge(challengeId) {
509519
const user = decodeToken(this.private.tokenV3);
510-
const username = user.handle || user.payload.handle;
511-
const url = `/members/${username.toLowerCase()}/challenges`;
512-
const data = await this.private.getMemberChallenges(url, { id: challengeId });
513-
return data.challenges[0].userDetails.roles;
520+
const url = `/resources?challengeId=${challengeId}?memberHandle=${user.handle}`;
521+
const resources = await this.private.apiV5(url);
522+
if (resources) return resources[0].roleId;
523+
throw new Error(`Failed to fetch user role from challenge #${challengeId}`);
514524
}
515525
}
516526

0 commit comments

Comments
 (0)