1
+ /*
2
+ * Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
3
+ *
4
+ * Get user design challenges api
5
+ *
6
+ * @version 1.0
7
+ * @author TCSASSEMBLER
8
+ *
9
+ */
10
+ "use strict" ;
11
+ /*jslint stupid: true, unparam: true, continue: true */
12
+
13
+
14
+ var async = require ( 'async' ) ;
15
+ var _ = require ( 'underscore' ) ;
16
+ var BadRequestError = require ( '../errors/BadRequestError' ) ;
17
+ var NotFoundError = require ( '../errors/NotFoundError' ) ;
18
+
19
+ /**
20
+ * The default pzge size.
21
+ */
22
+ var DEFAULT_PAGE_SIZE = 50 ;
23
+
24
+ /**
25
+ * The constants of project type.
26
+ */
27
+ var DESIGN_PROJECT_TYPE = "17,20,16,32,30,31,21,18,22,34,40,1" ;
28
+
29
+ /**
30
+ * The valid sort column for user challenges api.
31
+ */
32
+ var USER_CHALLENGE_ALLOWABLE_SORT_COLUMN = [ "id" , "type" , "placement" , "prize" , "num_contestants" , "num_submitters" , "coding_duration" ] ;
33
+
34
+
35
+ /**
36
+ * check whether given user is activated and existed.
37
+ * @param {String } handle - the handle to check.
38
+ * @param {Object } api - the action hero api object
39
+ * @param {Object } dbConnectionMap - the database connection map
40
+ * @param {Function<err> } callback - the callback function
41
+ */
42
+ function checkUserExistAndActivated ( handle , api , dbConnectionMap , callback ) {
43
+ api . dataAccess . executeQuery ( 'check_user' , { handle : handle } , dbConnectionMap , function ( err , result ) {
44
+ if ( err ) {
45
+ callback ( err ) ;
46
+ return ;
47
+ }
48
+
49
+ if ( ! result || result . length === 0 ) {
50
+ callback ( new NotFoundError ( "User does not exist." ) ) ;
51
+ return ;
52
+ }
53
+
54
+ if ( result && result [ 0 ] && result [ 0 ] . status === 'A' ) {
55
+ callback ( null , result ) ;
56
+ } else {
57
+ callback ( new BadRequestError ( 'User is not activated.' ) ) ;
58
+ }
59
+ } ) ;
60
+ }
61
+
62
+ /**
63
+ * Build str to array.
64
+ *
65
+ * @param {String } str - the str like str1,str2,str3
66
+ * @return the array by split str.
67
+ */
68
+ function buildArray ( str ) {
69
+ if ( str . trim ( ) . length === 0 ) {
70
+ return [ ] ;
71
+ }
72
+
73
+ return _ . map ( str . split ( ',' ) , function ( item ) {
74
+ return item . trim ( ) ;
75
+ } ) ;
76
+
77
+ }
78
+
79
+ /**
80
+ * Handle get user challenges api.
81
+ *
82
+ * @param {Object } api - the api object.
83
+ * @param {Object } connection - the connection object.
84
+ * @param {Integer } challengeType - the challenge type
85
+ * @param {Function } next - the callback function.
86
+ */
87
+ var getUserChallenges = function ( api , connection , challengeType , next ) {
88
+ var helper = api . helper , params = connection . params , sqlParams ,
89
+ pageIndex , pageSize , sortColumn , sortOrder , error , result , handle = connection . params . handle ,
90
+ dbConnectionMap = connection . dbConnectionMap ;
91
+
92
+ sortOrder = ( params . sortOrder || "asc" ) . toLowerCase ( ) ;
93
+ sortColumn = ( params . sortColumn || "id" ) . toLowerCase ( ) ;
94
+ pageIndex = Number ( params . pageIndex || 1 ) ;
95
+ pageSize = Number ( params . pageSize || DEFAULT_PAGE_SIZE ) ;
96
+
97
+ if ( ! _ . isDefined ( params . sortOrder ) && sortColumn === "id" ) {
98
+ sortOrder = "desc" ;
99
+ }
100
+
101
+ async . waterfall ( [
102
+ function ( cb ) {
103
+ checkUserExistAndActivated ( handle , api , dbConnectionMap , cb ) ;
104
+ } ,
105
+ function ( result , cb ) {
106
+ var allowedSort = helper . getLowerCaseList ( USER_CHALLENGE_ALLOWABLE_SORT_COLUMN ) , scriptName = "get_user_design_challenges" ;
107
+ if ( _ . isDefined ( params . pageIndex ) && pageIndex !== - 1 ) {
108
+ error = helper . checkDefined ( params . pageSize , "pageSize" ) ;
109
+ }
110
+ error = error ||
111
+ helper . checkMaxNumber ( pageIndex , helper . MAX_INT , "pageIndex" ) ||
112
+ helper . checkMaxNumber ( pageSize , helper . MAX_INT , "pageSize" ) ||
113
+ helper . checkPageIndex ( pageIndex , "pageIndex" ) ||
114
+ helper . checkPositiveInteger ( pageSize , "pageSize" ) ||
115
+ helper . checkContains ( [ "asc" , "desc" ] , sortOrder , "sortOrder" ) ||
116
+ helper . checkContains ( allowedSort , sortColumn , "sortColumn" ) ;
117
+
118
+ if ( error ) {
119
+ cb ( error ) ;
120
+ return ;
121
+ }
122
+
123
+ if ( pageIndex === - 1 ) {
124
+ pageIndex = 1 ;
125
+ pageSize = helper . MAX_INT ;
126
+ }
127
+ sqlParams = {
128
+ firstRowIndex : ( pageIndex - 1 ) * pageSize ,
129
+ pageSize : pageSize ,
130
+ sortColumn : helper . getSortColumnDBName ( sortColumn ) ,
131
+ sortOrder : sortOrder ,
132
+ userId : result [ 0 ] . user_id ,
133
+ challengeType : challengeType
134
+ } ;
135
+ async . parallel ( {
136
+ count : function ( cbx ) {
137
+ api . dataAccess . executeQuery ( scriptName + "_count" ,
138
+ sqlParams ,
139
+ dbConnectionMap ,
140
+ cbx ) ;
141
+ } ,
142
+ data : function ( cbx ) {
143
+ api . dataAccess . executeQuery ( scriptName ,
144
+ sqlParams ,
145
+ dbConnectionMap ,
146
+ cbx ) ;
147
+ }
148
+ } , cb ) ;
149
+ } , function ( results , cb ) {
150
+
151
+ var total = results . count [ 0 ] . total ;
152
+ if ( total === 0 || results . data . length === 0 ) {
153
+ result = {
154
+ data : [ ] ,
155
+ total : total ,
156
+ pageIndex : pageIndex ,
157
+ pageSize : Number ( params . pageIndex ) === - 1 ? total : pageSize
158
+ } ;
159
+ cb ( ) ;
160
+ return ;
161
+ }
162
+ result = {
163
+ data : _ . map ( results . data , function ( item ) {
164
+
165
+ var challenge = {
166
+ id : item . id ,
167
+ type : item . type ,
168
+ placement : item . placement ,
169
+ prize : item . payment > 0 ? true : false ,
170
+ numContestants : item . num_contestants ,
171
+ numSubmitters : item . num_submitters ,
172
+ codingDuration : item . coding_duration ,
173
+ platforms : buildArray ( item . platforms ) ,
174
+ technologies : buildArray ( item . technologies )
175
+ } ;
176
+ return challenge ;
177
+ } ) ,
178
+ total : total ,
179
+ pageIndex : pageIndex ,
180
+ pageSize : Number ( params . pageIndex ) === - 1 ? total : pageSize
181
+ } ;
182
+ cb ( ) ;
183
+ }
184
+ ] , function ( err ) {
185
+ if ( err ) {
186
+ helper . handleError ( api , connection , err ) ;
187
+ } else {
188
+ connection . response = result ;
189
+ }
190
+ next ( connection , true ) ;
191
+ } ) ;
192
+ } ;
193
+
194
+
195
+ /**
196
+ * The API for get user design challenges
197
+ */
198
+ exports . getUserDesignChallenges = {
199
+ name : "getUserDesignChallenges" ,
200
+ description : "get user design challenges api" ,
201
+ inputs : {
202
+ required : [ 'handle' ] ,
203
+ optional : [ 'pageSize' , 'pageIndex' , 'sortColumn' , 'sortOrder' ]
204
+ } ,
205
+ blockedConnectionTypes : [ ] ,
206
+ outputExample : { } ,
207
+ version : 'v2' ,
208
+ transaction : 'read' ,
209
+ databases : [ 'tcs_catalog' ] ,
210
+ run : function ( api , connection , next ) {
211
+ if ( connection . dbConnectionMap ) {
212
+ api . log ( "Execute getUserDesignChallenges#run" , 'debug' ) ;
213
+ getUserChallenges ( api , connection , DESIGN_PROJECT_TYPE , next ) ;
214
+ } else {
215
+ api . helper . handleNoConnection ( api , connection , next ) ;
216
+ }
217
+ }
218
+ } ;
0 commit comments