11/*
22 * Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
33 *
4- * @version 1.0
5- * @author isv
4+ * Changes in version 1.1 (Module Assembly - Topcoder NodeJS Active and Upcoming Data Science Challenge API):
5+ * - Added the logic for active / upcoming data science challenges.
6+ *
7+ * @version 1.1
8+ * @author isv, TCASSEMBLER
69 */
710/*jslint node: true, nomen: true */
811
@@ -35,14 +38,14 @@ var PAST_CHALLENGES_DATA_COLUMN_NAMES = ['challengetype', 'challengename', 'chal
3538 'numregistrants' , 'registrationstartdate' , 'submissionenddate' , 'challengecommunity' , 'postingdate' ] ;
3639
3740/**
38- * A format for the dates for Past Data Science Challenges filter.
41+ * A format for the dates for Data Science Challenges filter.
3942 */
40- var PAST_CHALLENGES_FILTER_DATE_FORMAT = 'YYYY-MM-DD' ;
43+ var CHALLENGES_FILTER_DATE_FORMAT = 'YYYY-MM-DD' ;
4144
4245/**
43- * A format for the dates for Past Data Science Challenges output.
46+ * A format for the dates for Data Science Challenges output.
4447 */
45- var PAST_CHALLENGES_OUTPUT_DATE_FORMAT = 'YYYY-MM-DD HH:mm z' ;
48+ var CHALLENGES_OUTPUT_DATE_FORMAT = 'YYYY-MM-DD HH:mm z' ;
4649
4750/**
4851 * Maximum value for integer number.
@@ -103,6 +106,138 @@ function pastDataScienceChallenges(pageIndex, pageSize, sortingColumnName, sorti
103106 } ) ;
104107}
105108
109+ /**
110+ * Populate the result.
111+ * @param data - the data value
112+ * @param helper - the helper instance
113+ * @param response - the response.
114+ * @returns {* } - the response
115+ * @since 1.1
116+ */
117+ function populateResult ( data , helper , response ) {
118+ _ . each ( data , function ( row ) {
119+ var challenge = {
120+ challengeType : row . challenge_type ,
121+ challengeName : row . challenge_name ,
122+ challengeId : row . challenge_id ,
123+ numSubmissions : row . num_submissions ,
124+ numRegistrants : row . num_registrants ,
125+ registrationStartDate : helper . formatDateWithTimezone ( row . registration_start_date , CHALLENGES_OUTPUT_DATE_FORMAT ) ,
126+ submissionEndDate : helper . formatDateWithTimezone ( row . submission_end_date , CHALLENGES_OUTPUT_DATE_FORMAT ) ,
127+ challengeCommunity : row . challenge_community ,
128+ postingDate : helper . formatDateWithTimezone ( row . posting_date , CHALLENGES_OUTPUT_DATE_FORMAT )
129+ } ;
130+ response . data . push ( challenge ) ;
131+ } ) ;
132+
133+ return response ;
134+ }
135+
136+ /**
137+ * Sets the submission dates.
138+ * @param connection - the connection instance.
139+ * @param response - the response instance.
140+ * @returns {* } the response instance
141+ * @since 1.1
142+ */
143+ function setSubmissionDates ( connection , response ) {
144+ if ( _ . isDefined ( connection . params . submissionEndFrom ) ) {
145+ response . submissionEndFrom = connection . params . submissionEndFrom ;
146+ }
147+ if ( _ . isDefined ( connection . params . submissionEndTo ) ) {
148+ response . submissionEndTo = connection . params . submissionEndTo ;
149+ }
150+
151+ return response ;
152+ }
153+
154+ /**
155+ * Get data science challenges from database.
156+ * @param isActive - the active flag.
157+ * @param submissionEndFrom - the submission from date.
158+ * @param submissionEndTo - the submission to date.
159+ * @param api - the api instance
160+ * @param connection - the connection instance
161+ * @param callback - the callback function
162+ * @since 1.1
163+ */
164+ function getDataScienceChallenges ( isActive , submissionEndFrom , submissionEndTo , api , connection , callback ) {
165+ var sqlParams = { } ;
166+ sqlParams . submitByFrom = submissionEndFrom ;
167+ sqlParams . submitByTo = submissionEndTo ;
168+
169+ async . waterfall ( [
170+ function ( cb ) {
171+ api . dataAccess . executeQuery ( ( isActive ? 'active' : 'upcoming' ) + '_data_science_challenges' , sqlParams , connection . dbConnectionMap , cb ) ;
172+ }
173+ ] , function ( err , results ) {
174+ if ( err ) {
175+ callback ( err ) ;
176+ } else {
177+ callback ( null , results . length , results ) ;
178+ }
179+ } ) ;
180+ }
181+
182+ /**
183+ * Set the default submission from date.
184+ * @param connection - the connection instance
185+ * @returns {* } the submission from date.
186+ * @since 1.1
187+ */
188+ function setSubmissionEndFromDefault ( connection ) {
189+ var submissionEndFrom ;
190+ if ( _ . isDefined ( connection . params . submissionEndFrom ) ) {
191+ submissionEndFrom = connection . params . submissionEndFrom ;
192+ } else {
193+ submissionEndFrom = '1900-01-01' ;
194+ }
195+
196+ return submissionEndFrom ;
197+ }
198+
199+ /**
200+ * Set the default submission to date.
201+ * @param connection - the connection instance
202+ * @returns {* } the submission to date
203+ * @since 1.1
204+ */
205+ function setSubmissionEndToDefault ( connection ) {
206+ var submissionEndTo ;
207+ if ( _ . isDefined ( connection . params . submissionEndTo ) ) {
208+ submissionEndTo = connection . params . submissionEndTo ;
209+ } else {
210+ submissionEndTo = '2999-12-31' ;
211+ }
212+ return submissionEndTo ;
213+ }
214+
215+ /**
216+ * Validates the submission dates.
217+ * @param submissionEndFrom - the submission from date
218+ * @param submissionEndTo - the submission to date
219+ * @param helper - the helper instance
220+ * @returns {* } the validation result
221+ * @since 1.1
222+ */
223+ function validateSubmissionDates ( submissionEndFrom , submissionEndTo , helper ) {
224+ var err = helper . checkDateFormat ( submissionEndFrom , CHALLENGES_FILTER_DATE_FORMAT , 'submissionEndFrom' ) ;
225+ if ( err ) {
226+ return err ;
227+ }
228+
229+ err = helper . checkDateFormat ( submissionEndTo , CHALLENGES_FILTER_DATE_FORMAT , 'submissionEndTo' ) ;
230+ if ( err ) {
231+ return err ;
232+ }
233+
234+ err = helper . checkDates ( submissionEndFrom , submissionEndTo , 'submissionEndFrom must be before submissionEndTo' ) ;
235+ if ( err ) {
236+ return err ;
237+ }
238+ return null ;
239+ }
240+
106241
107242/**
108243 * View Past Data Science Challenges API.
@@ -150,31 +285,10 @@ exports.pastDataScienceChallenges = {
150285 return ;
151286 }
152287
153- if ( _ . isDefined ( connection . params . submissionEndFrom ) ) {
154- err = helper . checkDateFormat ( connection . params . submissionEndFrom ,
155- PAST_CHALLENGES_FILTER_DATE_FORMAT , 'submissionEndFrom' ) ;
156- if ( err ) {
157- cb ( err ) ;
158- return ;
159- }
160- submissionEndFrom = connection . params . submissionEndFrom ;
161- } else {
162- submissionEndFrom = '1900-01-01' ;
163- }
164- if ( _ . isDefined ( connection . params . submissionEndTo ) ) {
165- err = helper . checkDateFormat ( connection . params . submissionEndTo ,
166- PAST_CHALLENGES_FILTER_DATE_FORMAT , 'submissionEndTo' ) ;
167- if ( err ) {
168- cb ( err ) ;
169- return ;
170- }
171- submissionEndTo = connection . params . submissionEndTo ;
172- } else {
173- submissionEndTo = '2999-12-31' ;
174- }
288+ submissionEndFrom = setSubmissionEndFromDefault ( connection ) ;
289+ submissionEndTo = setSubmissionEndToDefault ( connection ) ;
290+ err = validateSubmissionDates ( submissionEndFrom , submissionEndTo , helper ) ;
175291
176- err = helper . checkDates ( submissionEndFrom , submissionEndTo ,
177- 'submissionEndFrom must be before submissionEndTo' ) ;
178292 if ( err ) {
179293 cb ( err ) ;
180294 return ;
@@ -207,30 +321,9 @@ exports.pastDataScienceChallenges = {
207321 if ( _ . isDefined ( connection . params . sortOrder ) ) {
208322 response . sortOrder = connection . params . sortOrder ;
209323 }
210- if ( _ . isDefined ( connection . params . submissionEndFrom ) ) {
211- response . submissionEndFrom = connection . params . submissionEndFrom ;
212- }
213- if ( _ . isDefined ( connection . params . submissionEndTo ) ) {
214- response . submissionEndTo = connection . params . submissionEndTo ;
215- }
216- // Convert the rows returned from DB into format suitable for response
217- _ . each ( data , function ( row ) {
218- var challenge = {
219- challengeType : row . challenge_type ,
220- challengeName : row . challenge_name ,
221- challengeId : row . challenge_id ,
222- numSubmissions : row . num_submissions ,
223- numRegistrants : row . num_registrants ,
224- registrationStartDate : helper . formatDateWithTimezone ( row . registration_start_date , PAST_CHALLENGES_OUTPUT_DATE_FORMAT ) ,
225- submissionEndDate : helper . formatDateWithTimezone ( row . submission_end_date , PAST_CHALLENGES_OUTPUT_DATE_FORMAT ) ,
226- challengeCommunity : row . challenge_community ,
227- postingDate : helper . formatDateWithTimezone ( row . posting_date , PAST_CHALLENGES_OUTPUT_DATE_FORMAT )
228- } ;
229- response . data . push ( challenge ) ;
230- } ) ;
231-
232-
233- connection . response = response ;
324+
325+ response = setSubmissionDates ( connection , response ) ;
326+ connection . response = populateResult ( data , helper , response ) ;
234327 }
235328 next ( connection , true ) ;
236329 } ) ;
@@ -239,3 +332,102 @@ exports.pastDataScienceChallenges = {
239332 }
240333 }
241334} ;
335+
336+ /**
337+ * Processes the data science challenges.
338+ * @param isActive - the active flag, if the active is false, it means upcoming type.
339+ * @param api - the api instance
340+ * @param connection - the connection instance
341+ * @param next - the callback function
342+ * @since 1.1
343+ */
344+ function processDataScienceChallenges ( isActive , api , connection , next ) {
345+ var submissionEndFrom ,
346+ submissionEndTo ,
347+ err ,
348+ helper = api . helper ;
349+
350+ async . waterfall ( [
351+ function ( cb ) { // Parse and validate request parameters
352+ submissionEndFrom = setSubmissionEndFromDefault ( connection ) ;
353+ submissionEndTo = setSubmissionEndToDefault ( connection ) ;
354+ err = validateSubmissionDates ( submissionEndFrom , submissionEndTo , helper ) ;
355+
356+ if ( err ) {
357+ cb ( err ) ;
358+ return ;
359+ }
360+
361+ cb ( ) ;
362+ } , function ( cb ) { // Get the data based on requested parameters once provided parameters are valid
363+ getDataScienceChallenges ( isActive , submissionEndFrom , submissionEndTo , api , connection , cb ) ;
364+ }
365+ ] , function ( err , total , data ) {
366+ if ( err ) {
367+ helper . handleError ( api , connection , err ) ;
368+ } else {
369+ var response = { } ;
370+ response . total = total ;
371+ response . data = [ ] ;
372+
373+ response = setSubmissionDates ( connection , response ) ;
374+
375+ connection . response = populateResult ( data , helper , response ) ;
376+ }
377+ next ( connection , true ) ;
378+ } ) ;
379+ }
380+
381+ /**
382+ * View Active Data Science Challenges API.
383+ * @since 1.1
384+ */
385+ exports . activeDataScienceChallenges = {
386+ name : 'activeDataScienceChallenges' ,
387+ description : 'Get the list of active Data Science challenges' ,
388+ inputs : {
389+ required : [ ] ,
390+ optional : [ 'submissionEndFrom' , 'submissionEndTo' ]
391+ } ,
392+ blockedConnectionTypes : [ ] ,
393+ outputExample : { } ,
394+ version : 'v2' ,
395+ cacheEnabled : false ,
396+ transaction : 'read' ,
397+ databases : [ 'tcs_catalog' ] ,
398+ run : function ( api , connection , next ) {
399+ if ( connection . dbConnectionMap ) {
400+ api . log ( 'Execute activeDataScienceChallenges#run' , 'debug' ) ;
401+ processDataScienceChallenges ( true , api , connection , next ) ;
402+ } else {
403+ api . helper . handleNoConnection ( api , connection , next ) ;
404+ }
405+ }
406+ } ;
407+
408+ /**
409+ * View Upcoming Data Science Challenges API.
410+ * @since 1.1
411+ */
412+ exports . upcomingDataScienceChallenges = {
413+ name : 'upcomingDataScienceChallenges' ,
414+ description : 'Get the list of upcoming Data Science challenges' ,
415+ inputs : {
416+ required : [ ] ,
417+ optional : [ 'submissionEndFrom' , 'submissionEndTo' ]
418+ } ,
419+ blockedConnectionTypes : [ ] ,
420+ outputExample : { } ,
421+ version : 'v2' ,
422+ cacheEnabled : false ,
423+ transaction : 'read' ,
424+ databases : [ 'tcs_catalog' ] ,
425+ run : function ( api , connection , next ) {
426+ if ( connection . dbConnectionMap ) {
427+ api . log ( 'Execute upcomingDataScienceChallenges#run' , 'debug' ) ;
428+ processDataScienceChallenges ( false , api , connection , next ) ;
429+ } else {
430+ api . helper . handleNoConnection ( api , connection , next ) ;
431+ }
432+ }
433+ } ;
0 commit comments