Skip to content

Commit 4623942

Browse files
authored
Merge pull request #169 from topcoder-platform/issue-4378
Issue 4378
2 parents 6286153 + aeb69b5 commit 4623942

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

src/services/challenges.js

+35-22
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,22 @@ class ChallengesService {
370370
));
371371
}
372372

373+
/**
374+
* Get the ID from a challenge type by abbreviation
375+
* @param {String} abbreviation
376+
* @return {Promise} ID from first abbreviation match
377+
*/
378+
async getChallengeTypeId(abbreviation) {
379+
const ret = await this.private.apiV5.get(`/challenge-types?abbreviation=${abbreviation}`)
380+
.then(checkErrorV5).then(res => res);
381+
382+
if (_.isEmpty(ret.result)) {
383+
throw new Error('Challenge typeId not found!');
384+
}
385+
386+
return ret.result[0].id;
387+
}
388+
373389
/**
374390
* Gets possible challenge tags (technologies).
375391
* @return {Promise} Resolves to the array of tag strings.
@@ -401,11 +417,10 @@ class ChallengesService {
401417
/**
402418
* Gets SRM matches.
403419
* @param {Object} params
404-
* @param {string} typeId Challenge SRM TypeId
405420
* @return {Promise}
406421
*/
407422
async getSrms(params) {
408-
const res = await this.private.apiV5.get(`/challenges/?${qs.stringify(params)}`);
423+
const res = await this.private.api.get(`/srms/?${qs.stringify(params)}`);
409424
return getApiResponsePayload(res);
410425
}
411426

@@ -445,17 +460,25 @@ class ChallengesService {
445460

446461
/**
447462
* Gets marathon matches of the specified user.
448-
* @param {String} userId User whose challenges we want to fetch.
449-
* @param {Object} filters Optional.
450-
* @param {Number} params Optional.
463+
* @param {String} memberId User whose challenges we want to fetch.
464+
* @param {Object} params
451465
* @return {Promise} Resolves to the api response.
452466
*/
453-
async getUserMarathonMatches(userId) {
454-
const marathonTypeId = 'c2579605-e294-4967-b3db-875ef85240cd';
455-
const url = `/challenges?typeId=${marathonTypeId}&memberId=${userId}`;
467+
async getUserMarathonMatches(memberId, params) {
468+
const typeId = await this.getChallengeTypeId('DEVELOP_MARATHON_MATCH');
456469

457-
const res = await this.private.apiV5.get(url);
458-
return res;
470+
if (!typeId) {
471+
return null;
472+
}
473+
474+
const newParams = {
475+
...params,
476+
typeId,
477+
memberId,
478+
};
479+
480+
const res = await this.private.apiV5.get(`/challenges?${qs.stringify(newParams)}`);
481+
return getApiResponsePayload(res);
459482
}
460483

461484
/**
@@ -465,18 +488,8 @@ class ChallengesService {
465488
* @return {Promise}
466489
*/
467490
async getUserSrms(handle, params) {
468-
const challenges = await this.private.apiV5.get(`/resources?memberHandle=${handle}`);
469-
let newParams = params;
470-
if (challenges) {
471-
const { challengeId } = challenges[0];
472-
newParams = {
473-
...params,
474-
challengeId,
475-
};
476-
}
477-
478-
const url = `/challenges/${qs.stringify(newParams)}`;
479-
const res = await this.private.apiV5.get(url);
491+
const url = `/members/${handle}/srms/?${qs.stringify(params)}`;
492+
const res = await this.private.api.get(url);
480493
return getApiResponsePayload(res);
481494
}
482495

src/utils/tc.js

+3
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)