Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit befdfdf

Browse files
committedMay 25, 2020
Removed hardcoded challenge typeId
Added getChallengeTypeId() to get challenge id by abbreviation
1 parent 564fd25 commit befdfdf

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed
 

‎src/services/challenges.js

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,22 @@ class ChallengesService {
340340
));
341341
}
342342

343+
/**
344+
* Get the ID from a challenge type by abbreviation
345+
* @param {String} abbreviation
346+
* @return {Promise} ID from first abbreviation match
347+
*/
348+
async getChallengeTypeId(abbreviation) {
349+
const ret = await this.private.apiV5.get(`/challenge-types?abbreviation=${abbreviation}`)
350+
.then(checkErrorV5).then(res => res);
351+
352+
if (_.isEmpty(ret.result)) {
353+
throw new Error('Challenge typeId not found!');
354+
}
355+
356+
return ret.result[0].id;
357+
}
358+
343359
/**
344360
* Gets possible challenge tags (technologies).
345361
* @return {Promise} Resolves to the array of tag strings.
@@ -375,7 +391,17 @@ class ChallengesService {
375391
* @return {Promise}
376392
*/
377393
async getSrms(params) {
378-
const res = await this.private.apiV5.get(`/challenges/?${qs.stringify(params)}`);
394+
const typeId = await this.getChallengeTypeId('DEVELOP_SINGLE_ROUND_MATCH');
395+
if (!typeId) {
396+
return null;
397+
}
398+
399+
const newParams = {
400+
...params,
401+
typeId,
402+
};
403+
404+
const res = await this.private.apiV5.get(`/challenges?${qs.stringify(newParams)}`);
379405
return getApiResponsePayload(res);
380406
}
381407

@@ -415,17 +441,25 @@ class ChallengesService {
415441

416442
/**
417443
* Gets marathon matches of the specified user.
418-
* @param {String} userId User whose challenges we want to fetch.
419-
* @param {Object} filters Optional.
420-
* @param {Number} params Optional.
444+
* @param {String} memberId User whose challenges we want to fetch.
445+
* @param {Object} params
421446
* @return {Promise} Resolves to the api response.
422447
*/
423-
async getUserMarathonMatches(userId) {
424-
const marathonTypeId = 'c2579605-e294-4967-b3db-875ef85240cd';
425-
const url = `/challenges?typeId=${marathonTypeId}&memberId=${userId}`;
448+
async getUserMarathonMatches(memberId, params) {
449+
const typeId = await this.getChallengeTypeId('DEVELOP_MARATHON_MATCH');
450+
451+
if (!typeId) {
452+
return null;
453+
}
426454

427-
const res = await this.private.apiV5.get(url);
428-
return res;
455+
const newParams = {
456+
...params,
457+
typeId,
458+
memberId,
459+
};
460+
461+
const res = await this.private.apiV5.get(`/challenges?${qs.stringify(newParams)}`);
462+
return getApiResponsePayload(res);
429463
}
430464

431465
/**
@@ -435,18 +469,19 @@ class ChallengesService {
435469
* @return {Promise}
436470
*/
437471
async getUserSrms(memberId, params) {
438-
const challenges = await this.private.apiV5.get(`/resources/${memberId}/challenges`);
439-
let newParams = params;
440-
if (challenges) {
441-
const { challengeId } = challenges[0];
442-
newParams = {
443-
...params,
444-
challengeId,
445-
};
472+
const typeId = await this.getChallengeTypeId('DEVELOP_SINGLE_ROUND_MATCH');
473+
474+
if (!typeId) {
475+
return null;
446476
}
447477

448-
const url = `/challenges/${qs.stringify(newParams)}`;
449-
const res = await this.private.apiV5.get(url);
478+
const newParams = {
479+
...params,
480+
typeId,
481+
memberId,
482+
};
483+
484+
const res = await this.private.apiV5.get(`/challenges?${qs.stringify(newParams)}`);
450485
return getApiResponsePayload(res);
451486
}
452487

0 commit comments

Comments
 (0)
Please sign in to comment.