@@ -19,12 +19,13 @@ const _ = require('lodash');
19
19
const moment = require ( 'moment' ) ;
20
20
const circularJSON = require ( 'circular-json' ) ;
21
21
22
- // const m2mAuth = require('tc-core-library-js').auth.m2m;
22
+ const m2mAuth = require ( 'tc-core-library-js' ) . auth . m2m ;
23
23
24
- // const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']));
24
+ const m2m = m2mAuth ( _ . pick ( config , [ 'AUTH0_URL' , 'AUTH0_AUDIENCE' , 'TOKEN_CACHE_TIME' , 'AUTH0_PROXY_SERVER_URL' ] ) ) ;
25
25
26
26
let topcoderApiProjects = require ( 'topcoder-api-projects' ) ;
27
- let topcoderApiChallenges = require ( 'topcoder-api-challenges' ) ;
27
+ let topcoderApiChallenges = require ( '@topcoder-platform/topcoder-api-challenges-v4-wrapper' ) ;
28
+ let topcoderApiChallengesV3 = require ( 'topcoder-api-challenges' ) ;
28
29
29
30
const topcoderDevApiProjects = require ( 'topcoder-dev-api-projects' ) ;
30
31
const topcoderDevApiChallenges = require ( '@topcoder-platform/topcoder-api-challenges-v4-wrapper-dev' ) ;
@@ -45,6 +46,7 @@ let cachedAccessToken;
45
46
// Init the API instances
46
47
const projectsClient = topcoderApiProjects . ApiClient . instance ;
47
48
const challengesClient = topcoderApiChallenges . ApiClient . instance ;
49
+ const challengesClientV3 = topcoderApiChallengesV3 . ApiClient . instance ;
48
50
49
51
// Timeout increase to 5 minutes
50
52
challengesClient . timeout = 300000 ;
@@ -101,21 +103,21 @@ async function getAccessToken() {
101
103
return cachedAccessToken ;
102
104
}
103
105
104
- // / **
105
- // * Function to get M2M token
106
- // * @returns {Promise } The promised token
107
- // */
108
- // async function getM2Mtoken() {
109
- // return await m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET);
110
- // }
106
+ /**
107
+ * Function to get M2M token
108
+ * @returns {Promise } The promised token
109
+ */
110
+ async function getM2Mtoken ( ) {
111
+ return await m2m . getMachineToken ( config . AUTH0_CLIENT_ID , config . AUTH0_CLIENT_SECRET ) ;
112
+ }
111
113
112
114
/**
113
115
* Create a new project.
114
116
* @param {String } projectName the project name
115
117
* @returns {Number } the created project id
116
118
*/
117
119
async function createProject ( projectName ) {
118
- bearer . apiKey = await getAccessToken ( ) ;
120
+ bearer . apiKey = await getM2Mtoken ( ) ;
119
121
// eslint-disable-next-line new-cap
120
122
const projectBody = new topcoderApiProjects . ProjectRequestBody . constructFromObject ( {
121
123
projectName
@@ -147,7 +149,7 @@ async function createProject(projectName) {
147
149
* @returns {Number } the created challenge id
148
150
*/
149
151
async function createChallenge ( challenge ) {
150
- bearer . apiKey = await getAccessToken ( ) ;
152
+ bearer . apiKey = await getM2Mtoken ( ) ;
151
153
const start = new Date ( ) ;
152
154
const startTime = moment ( start ) . toISOString ( ) ;
153
155
const end = moment ( start ) . add ( config . NEW_CHALLENGE_DURATION_IN_DAYS , 'days' ) . toISOString ( ) ;
@@ -188,7 +190,7 @@ async function createChallenge(challenge) {
188
190
* @param {Object } challenge the challenge to update
189
191
*/
190
192
async function updateChallenge ( id , challenge ) {
191
- bearer . apiKey = await getAccessToken ( ) ;
193
+ bearer . apiKey = await getM2Mtoken ( ) ;
192
194
logger . debug ( `Updating challenge ${ id } with ${ circularJSON . stringify ( challenge ) } ` ) ;
193
195
// eslint-disable-next-line new-cap
194
196
const challengeBody = new topcoderApiChallenges . UpdateChallengeBodyParam . constructFromObject ( {
@@ -226,7 +228,7 @@ async function updateChallenge(id, challenge) {
226
228
* @param {Number } id the challenge id
227
229
*/
228
230
async function activateChallenge ( id ) {
229
- bearer . apiKey = await getAccessToken ( ) ;
231
+ bearer . apiKey = await getM2Mtoken ( ) ;
230
232
logger . debug ( `Activating challenge ${ id } ` ) ;
231
233
try {
232
234
const response = await new Promise ( ( resolve , reject ) => {
@@ -266,7 +268,7 @@ async function getChallengeById(id) {
266
268
if ( ! _ . isNumber ( id ) ) {
267
269
throw new Error ( 'The challenge id must valid number' ) ;
268
270
}
269
- const apiKey = await getAccessToken ( ) ;
271
+ const apiKey = await getM2Mtoken ( ) ;
270
272
logger . debug ( 'Getting topcoder challenge details' ) ;
271
273
try {
272
274
const response = await axios . get ( `${ challengesClient . basePath } /challenges/${ id } ` , {
@@ -300,7 +302,11 @@ async function closeChallenge(id, winnerId) {
300
302
const apiKey = await getAccessToken ( ) ;
301
303
logger . debug ( `Closing challenge ${ id } ` ) ;
302
304
try {
303
- const response = await axios . post ( `${ challengesClient . basePath } /challenges/${ id } /close?winnerId=${ winnerId } ` , null , {
305
+ let basePath = challengesClient . basePath ;
306
+ if ( ! config . TC_DEV_ENV ) {
307
+ basePath = challengesClientV3 . basePath ;
308
+ }
309
+ const response = await axios . post ( `${ basePath } /challenges/${ id } /close?winnerId=${ winnerId } ` , null , {
304
310
headers : {
305
311
authorization : `Bearer ${ apiKey } ` ,
306
312
'Content-Type' : 'application/json'
@@ -327,7 +333,7 @@ async function closeChallenge(id, winnerId) {
327
333
* @returns {Number } the billing account id
328
334
*/
329
335
async function getProjectBillingAccountId ( id ) {
330
- const apiKey = await getAccessToken ( ) ;
336
+ const apiKey = await getM2Mtoken ( ) ;
331
337
logger . debug ( `Getting project billing detail ${ id } ` ) ;
332
338
try {
333
339
const response = await axios . get ( `${ projectsClient . basePath } /direct/projects/${ id } ` , {
@@ -359,7 +365,7 @@ async function getProjectBillingAccountId(id) {
359
365
* @returns {Number } the user id
360
366
*/
361
367
async function getTopcoderMemberId ( handle ) {
362
- bearer . apiKey = await getAccessToken ( ) ;
368
+ bearer . apiKey = await getM2Mtoken ( ) ;
363
369
try {
364
370
const response = await axios . get ( `${ projectsClient . basePath } /members/${ handle } ` ) ;
365
371
const statusCode = response ? response . status : null ;
@@ -378,7 +384,7 @@ async function getTopcoderMemberId(handle) {
378
384
* @param {Object } resource the resource resource to add
379
385
*/
380
386
async function addResourceToChallenge ( id , resource ) {
381
- bearer . apiKey = await getAccessToken ( ) ;
387
+ bearer . apiKey = await getM2Mtoken ( ) ;
382
388
logger . debug ( `adding resource to challenge ${ id } ` ) ;
383
389
try {
384
390
const response = await new Promise ( ( resolve , reject ) => {
@@ -419,7 +425,7 @@ async function getResourcesFromChallenge(id) {
419
425
if ( ! _ . isNumber ( id ) ) {
420
426
throw new Error ( 'The challenge id must valid number' ) ;
421
427
}
422
- const apiKey = await getAccessToken ( ) ;
428
+ const apiKey = await getM2Mtoken ( ) ;
423
429
logger . debug ( `fetch resource from challenge ${ id } ` ) ;
424
430
try {
425
431
const response = await axios . get ( `${ challengesClient . basePath } /challenges/${ id } /resources` , {
@@ -464,7 +470,7 @@ async function roleAlreadySet(id, role) {
464
470
* @param {Object } resource the resource resource to remove
465
471
*/
466
472
async function unregisterUserFromChallenge ( id ) {
467
- bearer . apiKey = await getAccessToken ( ) ;
473
+ bearer . apiKey = await getM2Mtoken ( ) ;
468
474
logger . debug ( `removing resource from challenge ${ id } ` ) ;
469
475
try {
470
476
const response = await new Promise ( ( resolve , reject ) => {
@@ -501,7 +507,7 @@ async function unregisterUserFromChallenge(id) {
501
507
* @param {Number } id the challenge id
502
508
*/
503
509
async function cancelPrivateContent ( id ) {
504
- bearer . apiKey = await getAccessToken ( ) ;
510
+ bearer . apiKey = await getM2Mtoken ( ) ;
505
511
logger . debug ( `Cancelling challenge ${ id } ` ) ;
506
512
try {
507
513
const response = await new Promise ( ( resolve , reject ) => {
@@ -549,7 +555,7 @@ async function assignUserAsRegistrant(topcoderUserId, challengeId) {
549
555
* @param {Object } resource the resource resource to remove
550
556
*/
551
557
async function removeResourceToChallenge ( id , resource ) {
552
- bearer . apiKey = await getAccessToken ( ) ;
558
+ bearer . apiKey = await getM2Mtoken ( ) ;
553
559
logger . debug ( `removing resource from challenge ${ id } ` ) ;
554
560
try {
555
561
const response = await new Promise ( ( resolve , reject ) => {
@@ -579,7 +585,7 @@ async function removeResourceToChallenge(id, resource) {
579
585
* @returns {Array } the resources of challenge
580
586
*/
581
587
async function getChallengeResources ( id ) {
582
- const apiKey = await getAccessToken ( ) ;
588
+ const apiKey = await getM2Mtoken ( ) ;
583
589
logger . debug ( `getting resource from challenge ${ id } ` ) ;
584
590
try {
585
591
const response = await axios . get ( `${ challengesClient . basePath } /challenges/${ id } /resources` , {
0 commit comments