Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 69f1b7f

Browse files
committed
Merge branch 'user_marathon_match' into dev
2 parents cbc7e9e + 83f7601 commit 69f1b7f

20 files changed

+1036
-103
lines changed

actions/memberStatistics.js

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -63,48 +63,6 @@ var STUDIO_ID = 6;
6363
var OPENAIM_ID = 8;
6464
var HIGH_SCHOOL_ID = 3;
6565

66-
/**
67-
* check whether given user is activated.
68-
* @param {String} handle - the handle to check.
69-
* @param {Object} api - the action hero api object
70-
* @param {Object} dbConnectionMap - the database connection map
71-
* @param {Function<err>} callback - the callback function
72-
*/
73-
function checkCoderActivated(handle, api, dbConnectionMap, callback) {
74-
api.dataAccess.executeQuery('check_coder_activated', { handle: handle }, dbConnectionMap, function (err, result) {
75-
if (err) {
76-
callback(err, null);
77-
return;
78-
}
79-
if (result && result[0] && result[0].status === 'A') {
80-
callback(err, null);
81-
} else {
82-
callback(err, new BadRequestError('User is not activated.'));
83-
}
84-
});
85-
}
86-
87-
///**
88-
// * Check whether given user is activated.
89-
// * @param {String} handle - the handle to check.
90-
// * @param {Object} api - the action hero api object
91-
// * @param {Object} dbConnectionMap - the database connection map
92-
// * @param {Function<err>} callback - the callback function
93-
// */
94-
//function checkUserActivated(handle, api, dbConnectionMap, callback) {
95-
// api.dataAccess.executeQuery('check_user_activated', { handle: handle }, dbConnectionMap, function (err, result) {
96-
// if (err) {
97-
// callback(err, null);
98-
// return;
99-
// }
100-
// if (result && result[0] && result[0].status === 'A') {
101-
// callback(err, null);
102-
// } else {
103-
// callback(err, new BadRequestError('User is not activated.'));
104-
// }
105-
// });
106-
//}
107-
10866
/**
10967
* Update user preference.
11068
*
@@ -183,42 +141,6 @@ function updateDemographicResponse(key, value, questionId, userId, api, dbConnec
183141
}
184142

185143

186-
/**
187-
* Check if the user exist and activated.
188-
* @param {String} handle - the user handle.
189-
* @param {Object} api - the api object.
190-
* @param {Object} dbConnectionMap - the database connection map object.
191-
* @param {Function} callback - the callback function.
192-
* @since 1.10
193-
*/
194-
function checkUserExistAndActivate(handle, api, dbConnectionMap, callback) {
195-
async.waterfall([
196-
function (cb) {
197-
// check user existence and activated status.
198-
async.parallel({
199-
exist: function (cb) {
200-
api.helper.checkUserExists(handle, api, dbConnectionMap, cb);
201-
},
202-
activate: function (cb) {
203-
checkCoderActivated(handle, api, dbConnectionMap, cb);
204-
}
205-
}, cb);
206-
},
207-
function (results, cb) {
208-
// handle the error situation.
209-
if (results.exist) {
210-
cb(results.exist);
211-
return;
212-
}
213-
if (results.activate) {
214-
cb(results.activate);
215-
return;
216-
}
217-
cb();
218-
}
219-
], callback);
220-
}
221-
222144
/**
223145
* Get the user basic profile information.
224146
* @param {Object} api - the api object.
@@ -270,7 +192,7 @@ function getBasicUserProfile(api, handle, privateInfoEligibility, dbConnectionMa
270192
}
271193
});
272194
} else {
273-
checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
195+
helper.checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
274196
}
275197
}, function (cb) {
276198
var execQuery = function (name) {
@@ -785,7 +707,7 @@ exports.getMarathonStatistics = {
785707
}
786708
async.waterfall([
787709
function (cb) {
788-
checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
710+
helper.checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
789711
}, function (cb) {
790712
var executeQuery = function (sqlName, cbx) {
791713
api.dataAccess.executeQuery(sqlName, sqlParams, dbConnectionMap, cbx);
@@ -899,7 +821,7 @@ exports.getSoftwareStatistics = {
899821
cb();
900822
}
901823
}, function (cb) {
902-
checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
824+
helper.checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
903825
}, function (cb) {
904826
var execQuery = function (name, cbx) {
905827
api.dataAccess.executeQuery(name,
@@ -1036,7 +958,7 @@ exports.getStudioStatistics = {
1036958

1037959
async.waterfall([
1038960
function (cb) {
1039-
checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
961+
helper.checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
1040962
}, function (cb) {
1041963
api.dataAccess.executeQuery('get_studio_member_statistics_track', sqlParams, dbConnectionMap, cb);
1042964
}, function (results, cb) {
@@ -1105,7 +1027,7 @@ exports.getAlgorithmStatistics = {
11051027
}
11061028
async.waterfall([
11071029
function (cb) {
1108-
checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
1030+
helper.checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
11091031
}, function (cb) {
11101032
var execQuery = function (name) {
11111033
return function (cbx) {
@@ -1498,7 +1420,7 @@ exports.getCopilotStatistics = {
14981420
cb();
14991421
}
15001422
}, function (cb) {
1501-
checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
1423+
helper.checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
15021424
}, function (cb) {
15031425
var execQuery = function (name, cbx) {
15041426
api.dataAccess.executeQuery(name,

actions/user.js

Lines changed: 146 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/*
22
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
33
*
4-
* @version 1.2
4+
* @version 1.3
55
* @author muzehyun, Ghost_141
66
* Changes in 1.1:
77
* - Implement user activation email api.
88
* Changes in 1.2:
99
* - Implement get user identity api.
10+
* Changes in 1.3:
11+
* - Implement get user marathon match api.
1012
*/
1113
'use strict';
1214
var async = require('async');
@@ -29,6 +31,13 @@ var activationEmailSubject = "Topcoder User Registration Activation";
2931
*/
3032
var activationEmailSenderName = "Topcoder API";
3133

34+
/**
35+
* The valid sort column for get user marathon matches api.
36+
* @since 1.3
37+
*/
38+
var VALID_SORT_COLUMN_MARATHON_MATCH = ['id', 'type', 'codingDuration', 'placement', 'numContestants',
39+
'numSubmitters'];
40+
3241
/**
3342
* It validates activation code and retrieves user id from activation code
3443
* @param {String} activationCode - activation code string
@@ -332,7 +341,7 @@ exports.getUserIdentity = {
332341
* @since 1.2
333342
*/
334343
function getUserIdentityByAuth0Id(api, connection, next) {
335-
var helper = api.helper,
344+
var helper = api.helper,
336345
auth0id = connection.params.id,
337346
userid = 0,
338347
dbConnectionMap = connection.dbConnectionMap,
@@ -343,35 +352,34 @@ function getUserIdentityByAuth0Id(api, connection, next) {
343352
function (cb) {
344353
try {
345354
var splits = auth0id.split('|');
346-
if (splits[0] == 'ad') {
355+
if (splits[0] === 'ad') {
347356
cb(null, [{ user_id: Number(splits[1]) }]);
348357
} else {
349-
api.helper.getProviderId(splits[0], function(err, provider) {
358+
api.helper.getProviderId(splits[0], function (err, provider) {
350359
if (err) {
351360
cb(notfound);
352361
} else {
353362
api.dataAccess.executeQuery("get_user_by_social_login",
354-
{
355-
social_user_id: splits[1],
356-
provider_id: provider
357-
},
358-
dbConnectionMap, cb);
363+
{
364+
social_user_id: splits[1],
365+
provider_id: provider
366+
},
367+
dbConnectionMap, cb);
359368
}
360369
});
361370
}
362-
}
363-
catch (exc) {
371+
} catch (exc) {
364372
cb(notfound);
365373
}
366374
},
367375
function (result, cb) {
368376
if (!result[0]) {
369377
cb(notfound);
370378
} else {
371-
userid = result[0].user_id
372-
api.dataAccess.executeQuery('get_user_email_and_handle',
373-
{ userId: userid },
374-
dbConnectionMap, cb);
379+
userid = result[0].user_id;
380+
api.dataAccess.executeQuery('get_user_email_and_handle',
381+
{ userId: userid },
382+
dbConnectionMap, cb);
375383
}
376384
},
377385
function (rs, cb) {
@@ -422,3 +430,126 @@ exports.getUserIdentityByAuth0Id = {
422430
}
423431
}
424432
};
433+
434+
/**
435+
* Handle the get marathon matches that user participated to.
436+
* @param {Object} api - The api object.
437+
* @param {Object} connection - The connection object.
438+
* @param {Function} next - The callback function.
439+
* @since 1.3
440+
*/
441+
function getUserMarathonMatches(api, connection, next) {
442+
var helper = api.helper, dbConnectionMap = connection.dbConnectionMap, response, sqlParams, sortOrder, sortColumn,
443+
exeQuery = function (name) {
444+
return function (cbx) {
445+
api.dataAccess.executeQuery(name, sqlParams, dbConnectionMap, cbx);
446+
};
447+
},
448+
handle = connection.params.handle,
449+
pageIndex = Number(connection.params.pageIndex || 1),
450+
pageSize = Number(connection.params.pageSize || 10);
451+
452+
sortOrder = (connection.params.sortOrder || "asc").toLowerCase();
453+
sortColumn = connection.params.sortColumn || "id";
454+
455+
// If the sortOrder is set and sortColumn is missing.
456+
if (connection.params.sortOrder && !connection.params.sortColumn) {
457+
helper.handleError(api, connection, new BadRequestError('The sortColumn is missing.'));
458+
next(connection, true);
459+
return;
460+
}
461+
462+
if (pageIndex === -1) {
463+
pageSize = helper.MAX_INT;
464+
pageIndex = 1;
465+
}
466+
467+
//reverse the sorting direction value because for placement 1 is the best so the descending order should be like 1, 2, 3.
468+
if (sortColumn.toLowerCase() === 'placement') {
469+
sortOrder = sortOrder === 'asc' ? 'desc' : 'asc';
470+
}
471+
472+
async.waterfall([
473+
function (cb) {
474+
var error = helper.checkPageIndex(pageIndex, "pageIndex")
475+
|| helper.checkStringParameter(handle, "handle", 30)
476+
|| helper.checkPositiveInteger(pageSize, "pageSize")
477+
|| helper.checkMaxInt(pageSize, "pageSize")
478+
|| helper.checkContains(['asc', 'desc'], sortOrder, "sortOrder")
479+
|| helper.checkSortColumn(VALID_SORT_COLUMN_MARATHON_MATCH, sortColumn.toLowerCase());
480+
cb(error);
481+
},
482+
function (cb) {
483+
helper.checkUserExistAndActivate(handle, api, dbConnectionMap, cb);
484+
},
485+
function (cb) {
486+
sqlParams = {
487+
first_row_index: (pageIndex - 1) * pageSize,
488+
page_size: pageSize,
489+
sort_order: sortOrder,
490+
sort_column: helper.getSortColumnDBName(sortColumn),
491+
handle: handle.toLowerCase()
492+
};
493+
async.parallel({
494+
data: exeQuery('get_user_marathon_matches'),
495+
count: exeQuery('get_user_marathon_matches_count')
496+
}, cb);
497+
},
498+
function (queryResult, cb) {
499+
var total = queryResult.count[0].total_count;
500+
response = {
501+
pageIndex: pageIndex,
502+
pageSize: pageSize,
503+
total: total,
504+
data: queryResult.data.map(function (row) {
505+
return {
506+
id: row.id,
507+
type: row.type,
508+
prize: row.paid > 0,
509+
codingDuration: row.coding_duration,
510+
placement: row.placement,
511+
numContestants: row.num_contestants,
512+
numSubmitters: row.num_submitters,
513+
platforms: [],
514+
technologies: row.technologies.length === 0
515+
? [] : row.technologies.split(',').map(function (s) { return s.trim(); })
516+
};
517+
})
518+
};
519+
cb();
520+
}
521+
], function (err) {
522+
if (err) {
523+
helper.handleError(api, connection, err);
524+
} else {
525+
connection.response = response;
526+
}
527+
next(connection, true);
528+
});
529+
}
530+
531+
/**
532+
* The api for get user marathon matches.
533+
* @since 1.3
534+
*/
535+
exports.getUserMarathonMatches = {
536+
name: 'getUserMarathonMatches',
537+
description: 'Get marathon match related info that member has participated to',
538+
inputs: {
539+
required: ['handle'],
540+
optional: ["sortOrder", "pageIndex", "pageSize", "sortColumn"]
541+
},
542+
blockedConnectionTypes: [],
543+
outputExample: {},
544+
version: 'v2',
545+
transaction: 'read',
546+
databases: ['topcoder_dw'],
547+
run: function (api, connection, next) {
548+
if (connection.dbConnectionMap) {
549+
api.log('getUserMarathonMatches#run', 'debug');
550+
getUserMarathonMatches(api, connection, next);
551+
} else {
552+
api.helper.handleNoConnection(api, connection, next);
553+
}
554+
}
555+
};

0 commit comments

Comments
 (0)