1
1
/*
2
2
* Copyright (C) 2013 - 2014 TopCoder Inc., All Rights Reserved.
3
3
*
4
- * @version 1.14
5
- * @author Sky_, Ghost_141, muzehyun, hesibo, isv, LazyChild
4
+ * @version 1.15
5
+ * @author Sky_, Ghost_141, muzehyun, hesibo, isv, LazyChild, jamestc
6
6
* changes in 1.1:
7
7
* - implement marathon statistics
8
8
* changes in 1.2:
36
36
* changes in 1.14
37
37
* - added my profile api
38
38
* - modified public profile api(basic user profile api), only return public information
39
+ * changes in 1.15
40
+ * - enabled granular data access in getBasicUserProfile via optional query param
39
41
*/
40
42
"use strict" ;
41
43
var async = require ( 'async' ) ;
@@ -118,6 +120,24 @@ function getBasicUserProfile(api, handle, privateInfoEligibility, dbConnectionMa
118
120
} ,
119
121
result ;
120
122
123
+ var loadData ;
124
+ // check for an optional data query string param than enables loading a subset of data
125
+ var requestedData = connection . rawConnection . parsedURL . query . data ;
126
+ if ( _ . isDefined ( requestedData ) ) {
127
+ // NOTE: an empty value is acceptable and indicates only basic data is returned
128
+ loadData = { } ;
129
+ if ( requestedData ) {
130
+ // data is comma delimited string of requested data
131
+ var parts = requestedData . split ( ',' ) ;
132
+ _ . each ( parts , function ( part ) {
133
+ loadData [ part ] = true ;
134
+ } ) ;
135
+ }
136
+ api . log ( "Requested data param found: " + requestedData , "debug" ) ;
137
+ } else {
138
+ loadData = { earnings :true , ratings :true , achievements :true , address :true , email :true } ; // load all data by default
139
+ }
140
+
121
141
async . waterfall ( [
122
142
function ( cb ) {
123
143
if ( privateInfoEligibility ) {
@@ -139,103 +159,95 @@ function getBasicUserProfile(api, handle, privateInfoEligibility, dbConnectionMa
139
159
} ;
140
160
async . parallel ( {
141
161
basic : execQuery ( 'basic' ) ,
142
- earning : execQuery ( 'overall_earning' ) ,
143
- ratingSummary : execQuery ( 'rating_summary' ) ,
144
- achievements : execQuery ( 'achievements' ) ,
145
- privateInfo : privateInfoEligibility ? execQuery ( 'private' ) : function ( cbx ) { cbx ( ) ; } ,
146
- emails : privateInfoEligibility ? execQuery ( 'private_email' ) : function ( cbx ) { cbx ( ) ; }
162
+ earning : loadData . earnings ? execQuery ( 'overall_earning' ) : function ( cbx ) { cbx ( ) ; } ,
163
+ ratingSummary : loadData . ratings ? execQuery ( 'rating_summary' ) : function ( cbx ) { cbx ( ) ; } ,
164
+ achievements : loadData . achievements ? execQuery ( 'achievements' ) : function ( cbx ) { cbx ( ) ; } ,
165
+ privateInfo : loadData . address && privateInfoEligibility ? execQuery ( 'private' ) : function ( cbx ) { cbx ( ) ; } ,
166
+ emails : loadData . email && privateInfoEligibility ? execQuery ( 'private_email' ) : function ( cbx ) { cbx ( ) ; }
147
167
} , cb ) ;
148
168
} , function ( results , cb ) {
149
- var basic = results . basic [ 0 ] , earning = results . earning [ 0 ] , ratingSummary = results . ratingSummary ,
150
- achievements = results . achievements , privateInfo ,
151
- mapRatingSummary = function ( ratings ) {
152
- var ret = [ ] ;
153
- ratings . forEach ( function ( item ) {
154
- ret . push ( {
155
- name : helper . getPhaseName ( item . phase_id ) ,
156
- rating : item . rating ,
157
- colorStyle : helper . getColorStyle ( item . rating )
158
- } ) ;
169
+ var basic = results . basic [ 0 ] ;
170
+
171
+ result = {
172
+ handle : basic . handle ,
173
+ country : basic . country ,
174
+ memberSince : basic . member_since ,
175
+ quote : basic . quote ,
176
+ photoLink : basic . photo_link || ''
177
+ } ;
178
+
179
+ if ( loadData . earnings && _ . isDefined ( basic . show_earnings ) && basic . show_earnings !== 'hide' ) {
180
+ result . overallEarning = results . earning [ 0 ] . overall_earning ;
181
+ }
182
+
183
+ if ( loadData . ratings ) {
184
+ var ratingSummary = [ ] ;
185
+ results . ratingSummary . forEach ( function ( item ) {
186
+ ratingSummary . push ( {
187
+ name : helper . getPhaseName ( item . phase_id ) ,
188
+ rating : item . rating ,
189
+ colorStyle : helper . getColorStyle ( item . rating )
159
190
} ) ;
160
- return ret ;
161
- } ,
162
- mapAchievements = function ( achievements ) {
163
- var ret = [ ] , achieveItem ;
164
- achievements . forEach ( function ( item ) {
165
- achieveItem = {
166
- date : item . achievement_date ,
167
- description : item . description
168
- } ;
169
- ret . push ( achieveItem ) ;
191
+ } ) ;
192
+ result . ratingSummary = ratingSummary ;
193
+ }
194
+
195
+ if ( loadData . achievements ) {
196
+ var achievements = [ ] ;
197
+ results . achievements . forEach ( function ( item ) {
198
+ achievements . push ( {
199
+ date : item . achievement_date ,
200
+ description : item . description
170
201
} ) ;
171
- return ret ;
172
- } ,
173
- mapEmails = function ( emails ) {
174
- var ret = [ ] ;
175
- emails . forEach ( function ( item ) {
176
- ret . push ( {
177
- email : item . email ,
178
- type : item . type ,
179
- status : item . status
180
- } ) ;
202
+ } ) ;
203
+ // TODO: why is this capitalized?
204
+ result . Achievements = achievements ;
205
+ }
206
+
207
+ if ( privateInfoEligibility && loadData . email ) {
208
+ var emails = [ ] ;
209
+ results . emails . forEach ( function ( item ) {
210
+ emails . push ( {
211
+ email : item . email ,
212
+ type : item . type ,
213
+ status : item . status
181
214
} ) ;
182
- return ret ;
183
- } ,
184
- appendIfNotEmpty = function ( str ) {
215
+ } ) ;
216
+ result . emails = emails ;
217
+ }
218
+
219
+ if ( privateInfoEligibility && loadData . address && results . privateInfo && results . privateInfo [ 0 ] ) {
220
+ var appendIfNotEmpty = function ( str ) {
185
221
var ret = '' ;
186
222
if ( str && str . length > 0 ) {
187
223
ret += ', ' + str ;
188
224
}
189
225
return ret ;
190
- } ,
191
- getAddressString = function ( privateInfo ) {
192
- var address = privateInfo . address1 ;
193
- if ( ! address ) { return undefined ; } // if address1 is undefined, there is no address.
226
+ } ;
227
+
228
+ var privateInfo = results . privateInfo [ 0 ] ;
229
+
230
+ result . name = privateInfo . first_name + ' ' + privateInfo . last_name ;
231
+ result . age = privateInfo . age ;
232
+ result . gender = privateInfo . gender ;
233
+ result . shirtSize = privateInfo . shirt_size ;
194
234
235
+ var address = privateInfo . address1 ;
236
+ // if address1 is undefined, there is no address.
237
+ if ( address ) {
195
238
address += appendIfNotEmpty ( privateInfo . address2 ) ;
196
239
address += appendIfNotEmpty ( privateInfo . address3 ) ;
197
240
address += ', ' + privateInfo . city ;
198
241
address += appendIfNotEmpty ( privateInfo . state ) ;
199
242
address += ', ' + privateInfo . zip + ', ' + privateInfo . country ;
200
- return address ;
201
- } ;
202
- result = {
203
- handle : basic . handle ,
204
- country : basic . country ,
205
- memberSince : basic . member_since ,
206
- overallEarning : earning . overall_earning ,
207
- quote : basic . quote ,
208
- photoLink : basic . photo_link || '' ,
209
- isCopilot : {
210
- value : basic . is_copilot ,
211
- software : basic . is_software_copilot ,
212
- studio : basic . is_studio_copilot
213
- } ,
214
- isPM : basic . is_pm ,
215
-
216
- ratingSummary : mapRatingSummary ( ratingSummary ) ,
217
- Achievements : mapAchievements ( achievements )
218
- } ;
219
-
220
- if ( ! _ . isDefined ( basic . show_earnings ) || basic . show_earnings === 'hide' ) {
221
- delete result . overallEarning ;
243
+ result . address = address ;
244
+ }
222
245
}
223
246
224
247
if ( result . isPM ) {
225
248
delete result . ratingSummary ;
226
249
}
227
250
228
- if ( privateInfoEligibility ) {
229
- result . emails = mapEmails ( results . emails ) ;
230
- if ( results . privateInfo && results . privateInfo [ 0 ] ) {
231
- privateInfo = results . privateInfo [ 0 ] ;
232
- result . name = privateInfo . first_name + ' ' + privateInfo . last_name ;
233
- result . address = getAddressString ( privateInfo ) ;
234
- result . age = privateInfo . age ;
235
- result . gender = privateInfo . gender ;
236
- result . shirtSize = privateInfo . shirt_size ;
237
- }
238
- }
239
251
cb ( ) ;
240
252
}
241
253
] , function ( err ) {
@@ -1094,4 +1106,4 @@ exports.getMyProfile = {
1094
1106
api . helper . handleNoConnection ( api , connection , next ) ;
1095
1107
}
1096
1108
}
1097
- } ;
1109
+ } ;
0 commit comments