Skip to content

Commit 5683ba5

Browse files
committed
Brings getMarathonMatches(..) back into the challenges service
1 parent 26e414d commit 5683ba5

File tree

5 files changed

+115
-2
lines changed

5 files changed

+115
-2
lines changed

__tests__/__snapshots__/index.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Object {
192192
"getService": [Function],
193193
"normalizeChallenge": [Function],
194194
"normalizeChallengeDetails": [Function],
195+
"normalizeMarathonMatch": [Function],
195196
},
196197
"communities": Object {
197198
"default": undefined,

docs/services.challenges.md

+33
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This module provides a service for convenient manipulation with
99
* _static_
1010
* [.normalizeChallengeDetails(v3, v3Filtered, v3User, v2, username)](#module_services.challenges.normalizeChallengeDetails) ⇒ <code>Object</code>
1111
* [.normalizeChallenge(challenge, username)](#module_services.challenges.normalizeChallenge)
12+
* [.normalizeMarathonMatch(challenge, username)](#module_services.challenges.normalizeMarathonMatch) ⇒ <code>Object</code>
1213
* [.getService(tokenV3, tokenV2)](#module_services.challenges.getService) ⇒ <code>ChallengesService</code>
1314
* _inner_
1415
* [~ChallengesService](#module_services.challenges..ChallengesService)
@@ -20,6 +21,7 @@ This module provides a service for convenient manipulation with
2021
* [.getChallengeSubtracks()](#module_services.challenges..ChallengesService+getChallengeSubtracks) ⇒ <code>Promise</code>
2122
* [.getChallengeTags()](#module_services.challenges..ChallengesService+getChallengeTags) ⇒ <code>Promise</code>
2223
* [.getChallenges(filters, params)](#module_services.challenges..ChallengesService+getChallenges) ⇒ <code>Promise</code>
24+
* [.getMarathonMatches(filters, params)](#module_services.challenges..ChallengesService+getMarathonMatches) ⇒ <code>Promise</code>
2325
* [.getSrms(params)](#module_services.challenges..ChallengesService+getSrms) ⇒ <code>Promise</code>
2426
* [.getUserChallenges(username, filters, params)](#module_services.challenges..ChallengesService+getUserChallenges) ⇒ <code>Promise</code>
2527
* [.getUserSrms(handle, params)](#module_services.challenges..ChallengesService+getUserSrms) ⇒ <code>Promise</code>
@@ -73,6 +75,23 @@ breaking anything.
7375
| challenge | <code>Object</code> | Challenge object received from the backend. |
7476
| username | <code>String</code> | Optional. |
7577

78+
<a name="module_services.challenges.normalizeMarathonMatch"></a>
79+
80+
### services.challenges.normalizeMarathonMatch(challenge, username) ⇒ <code>Object</code>
81+
Normalizes a marathon match challenge object received from the backend.
82+
NOTE: This function is copied from the existing code in the challenge listing
83+
component. It is possible, that this normalization is not necessary after we
84+
have moved to Topcoder API v3, but it is kept for now to minimize a risk of
85+
breaking anything.
86+
87+
**Kind**: static method of [<code>services.challenges</code>](#module_services.challenges)
88+
**Returns**: <code>Object</code> - Normalized challenge.
89+
90+
| Param | Type | Description |
91+
| --- | --- | --- |
92+
| challenge | <code>Object</code> | MM challenge object received from the backend. |
93+
| username | <code>String</code> | Optional. |
94+
7695
<a name="module_services.challenges.getService"></a>
7796

7897
### services.challenges.getService(tokenV3, tokenV2) ⇒ <code>ChallengesService</code>
@@ -102,6 +121,7 @@ Challenge service.
102121
* [.getChallengeSubtracks()](#module_services.challenges..ChallengesService+getChallengeSubtracks) ⇒ <code>Promise</code>
103122
* [.getChallengeTags()](#module_services.challenges..ChallengesService+getChallengeTags) ⇒ <code>Promise</code>
104123
* [.getChallenges(filters, params)](#module_services.challenges..ChallengesService+getChallenges) ⇒ <code>Promise</code>
124+
* [.getMarathonMatches(filters, params)](#module_services.challenges..ChallengesService+getMarathonMatches) ⇒ <code>Promise</code>
105125
* [.getSrms(params)](#module_services.challenges..ChallengesService+getSrms) ⇒ <code>Promise</code>
106126
* [.getUserChallenges(username, filters, params)](#module_services.challenges..ChallengesService+getUserChallenges) ⇒ <code>Promise</code>
107127
* [.getUserSrms(handle, params)](#module_services.challenges..ChallengesService+getUserSrms) ⇒ <code>Promise</code>
@@ -208,6 +228,19 @@ Gets challenges.
208228
| filters | <code>Object</code> | Optional. |
209229
| params | <code>Object</code> | Optional. |
210230

231+
<a name="module_services.challenges..ChallengesService+getMarathonMatches"></a>
232+
233+
#### challengesService.getMarathonMatches(filters, params) ⇒ <code>Promise</code>
234+
Gets marathon matches.
235+
236+
**Kind**: instance method of [<code>ChallengesService</code>](#module_services.challenges..ChallengesService)
237+
**Returns**: <code>Promise</code> - Resolve to the api response.
238+
239+
| Param | Type | Description |
240+
| --- | --- | --- |
241+
| filters | <code>Object</code> | Optional. |
242+
| params | <code>Object</code> | Optional. |
243+
211244
<a name="module_services.challenges..ChallengesService+getSrms"></a>
212245

213246
#### challengesService.getSrms(params) ⇒ <code>Promise</code>

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"lint:js": "./node_modules/.bin/eslint --ext .js,.jsx .",
2929
"test": "npm run lint && npm run jest"
3030
},
31-
"version": "0.0.3",
31+
"version": "0.0.4",
3232
"dependencies": {
3333
"isomorphic-fetch": "^2.2.1",
3434
"le_node": "^1.7.0",

src/services/challenges.js

+79
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,71 @@ export function normalizeChallenge(challenge, username) {
260260
/* eslint-enable no-param-reassign */
261261
}
262262

263+
/**
264+
* Normalizes a marathon match challenge object received from the backend.
265+
* NOTE: This function is copied from the existing code in the challenge listing
266+
* component. It is possible, that this normalization is not necessary after we
267+
* have moved to Topcoder API v3, but it is kept for now to minimize a risk of
268+
* breaking anything.
269+
* @param {Object} challenge MM challenge object received from the backend.
270+
* @param {String} username Optional.
271+
* @return {Object} Normalized challenge.
272+
*/
273+
export function normalizeMarathonMatch(challenge, username) {
274+
const startDate = _.get(challenge, 'rounds[0].codingStartAt') || challenge.startDate;
275+
const endDate = _.get(challenge, 'rounds[0].codingEndAt') || challenge.endDate;
276+
const endTimestamp = new Date(endDate).getTime();
277+
const status = endTimestamp > Date.now() ? 'Open' : 'Close';
278+
const allPhases = [{
279+
actualStartTime: startDate,
280+
challengeId: challenge.id,
281+
phaseType: 'Registration',
282+
phaseStatus: status,
283+
scheduledEndTime: endDate,
284+
}, {
285+
actualStartTime: startDate,
286+
challengeId: challenge.id,
287+
phaseType: 'Submission',
288+
phaseStatus: status,
289+
scheduledEndTime: endDate,
290+
}];
291+
const groups = {};
292+
if (challenge.groupIds) {
293+
challenge.groupIds.forEach((id) => {
294+
groups[id] = true;
295+
});
296+
}
297+
_.defaults(challenge, {
298+
challengeCommunity: 'Data',
299+
challengeType: 'Marathon',
300+
allPhases,
301+
currentPhases: allPhases.filter(phase => phase.phaseStatus === 'Open'),
302+
communities: new Set([COMPETITION_TRACKS.DATA_SCIENCE]),
303+
currentPhaseName: endTimestamp > Date.now() ? 'Registration' : '',
304+
groups,
305+
numRegistrants: challenge.userIds ? challenge.userIds.length : 0,
306+
numSubmissions: 0, // currently challenge doesn't return submission value
307+
platforms: '',
308+
prizes: [0],
309+
registrationOpen: endTimestamp > Date.now() &&
310+
(challenge.status !== 'PAST') ? 'Yes' : 'No',
311+
registrationStartDate: challenge.startDate,
312+
submissionEndDate: challenge.endDate,
313+
submissionEndTimestamp: endTimestamp,
314+
technologies: '',
315+
totalPrize: 0,
316+
track: 'DATA_SCIENCE',
317+
status: endTimestamp > Date.now() ? 'ACTIVE' : 'COMPLETED',
318+
subTrack: 'MARATHON_MATCH',
319+
users: username ? { username: true } : {},
320+
});
321+
/* eslint-disable no-param-reassign */
322+
challenge.endDate = endDate;
323+
challenge.startDate = startDate;
324+
if (challenge.status === 'PAST') challenge.status = 'COMPLETED';
325+
/* eslint-enable no-param-reassign */
326+
}
327+
263328
/**
264329
* Helper method that checks for HTTP error response and throws Error in this case.
265330
* @param {Object} res HTTP response object
@@ -469,6 +534,20 @@ class ChallengesService {
469534
});
470535
}
471536

537+
/**
538+
* Gets marathon matches.
539+
* @param {Object} filters Optional.
540+
* @param {Object} params Optional.
541+
* @return {Promise} Resolve to the api response.
542+
*/
543+
getMarathonMatches(filters, params) {
544+
return this.private.getChallenges('/marathonMatches/', filters, params)
545+
.then((res) => {
546+
res.challenges.forEach(item => normalizeMarathonMatch(item));
547+
return res;
548+
});
549+
}
550+
472551
/**
473552
* Gets SRM matches.
474553
* @param {Object} params

0 commit comments

Comments
 (0)