Skip to content

Commit d0c3795

Browse files
Merge branch 'issue-4378' into issue-4408
2 parents f6fedaa + aeb69b5 commit d0c3795

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

src/services/challenges.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,22 @@ class ChallengesService {
363363
));
364364
}
365365

366+
/**
367+
* Get the ID from a challenge type by abbreviation
368+
* @param {String} abbreviation
369+
* @return {Promise} ID from first abbreviation match
370+
*/
371+
async getChallengeTypeId(abbreviation) {
372+
const ret = await this.private.apiV5.get(`/challenge-types?abbreviation=${abbreviation}`)
373+
.then(checkErrorV5).then(res => res);
374+
375+
if (_.isEmpty(ret.result)) {
376+
throw new Error('Challenge typeId not found!');
377+
}
378+
379+
return ret.result[0].id;
380+
}
381+
366382
/**
367383
* Gets possible challenge tags (technologies).
368384
* @return {Promise} Resolves to the array of tag strings.
@@ -394,11 +410,10 @@ class ChallengesService {
394410
/**
395411
* Gets SRM matches.
396412
* @param {Object} params
397-
* @param {string} typeId Challenge SRM TypeId
398413
* @return {Promise}
399414
*/
400415
async getSrms(params) {
401-
const res = await this.private.apiV5.get(`/challenges/?${qs.stringify(params)}`);
416+
const res = await this.private.api.get(`/srms/?${qs.stringify(params)}`);
402417
return getApiResponsePayload(res);
403418
}
404419

@@ -438,17 +453,25 @@ class ChallengesService {
438453

439454
/**
440455
* Gets marathon matches of the specified user.
441-
* @param {String} userId User whose challenges we want to fetch.
442-
* @param {Object} filters Optional.
443-
* @param {Number} params Optional.
456+
* @param {String} memberId User whose challenges we want to fetch.
457+
* @param {Object} params
444458
* @return {Promise} Resolves to the api response.
445459
*/
446-
async getUserMarathonMatches(userId) {
447-
const marathonTypeId = 'c2579605-e294-4967-b3db-875ef85240cd';
448-
const url = `/challenges?typeId=${marathonTypeId}&memberId=${userId}`;
460+
async getUserMarathonMatches(memberId, params) {
461+
const typeId = await this.getChallengeTypeId('DEVELOP_MARATHON_MATCH');
449462

450-
const res = await this.private.apiV5.get(url);
451-
return res;
463+
if (!typeId) {
464+
return null;
465+
}
466+
467+
const newParams = {
468+
...params,
469+
typeId,
470+
memberId,
471+
};
472+
473+
const res = await this.private.apiV5.get(`/challenges?${qs.stringify(newParams)}`);
474+
return getApiResponsePayload(res);
452475
}
453476

454477
/**
@@ -458,18 +481,8 @@ class ChallengesService {
458481
* @return {Promise}
459482
*/
460483
async getUserSrms(handle, params) {
461-
const challenges = await this.private.apiV5.get(`/resources?memberHandle=${handle}`);
462-
let newParams = params;
463-
if (challenges) {
464-
const { challengeId } = challenges[0];
465-
newParams = {
466-
...params,
467-
challengeId,
468-
};
469-
}
470-
471-
const url = `/challenges/${qs.stringify(newParams)}`;
472-
const res = await this.private.apiV5.get(url);
484+
const url = `/members/${handle}/srms/?${qs.stringify(params)}`;
485+
const res = await this.private.api.get(url);
473486
return getApiResponsePayload(res);
474487
}
475488

src/utils/tc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export async function getApiResponsePayload(res, shouldThrowError = true) {
4141
}
4242
}
4343
const x = (await res.json()).result;
44+
if (!x) {
45+
return null;
46+
}
4447
if ((!x.success)) {
4548
if (shouldThrowError) {
4649
throw new Error(x.content);

0 commit comments

Comments
 (0)