@@ -19,12 +19,13 @@ const _ = require('lodash');
1919const moment = require ( 'moment' ) ;
2020const circularJSON = require ( 'circular-json' ) ;
2121
22- // const m2mAuth = require('tc-core-library-js').auth.m2m;
22+ const m2mAuth = require ( 'tc-core-library-js' ) . auth . m2m ;
2323
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' ] ) ) ;
2525
2626let 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' ) ;
2829
2930const topcoderDevApiProjects = require ( 'topcoder-dev-api-projects' ) ;
3031const topcoderDevApiChallenges = require ( '@topcoder-platform/topcoder-api-challenges-v4-wrapper-dev' ) ;
@@ -45,6 +46,7 @@ let cachedAccessToken;
4546// Init the API instances
4647const projectsClient = topcoderApiProjects . ApiClient . instance ;
4748const challengesClient = topcoderApiChallenges . ApiClient . instance ;
49+ const challengesClientV3 = topcoderApiChallengesV3 . ApiClient . instance ;
4850
4951// Timeout increase to 5 minutes
5052challengesClient . timeout = 300000 ;
@@ -101,21 +103,21 @@ async function getAccessToken() {
101103 return cachedAccessToken ;
102104}
103105
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+ }
111113
112114/**
113115 * Create a new project.
114116 * @param {String } projectName the project name
115117 * @returns {Number } the created project id
116118 */
117119async function createProject ( projectName ) {
118- bearer . apiKey = await getAccessToken ( ) ;
120+ bearer . apiKey = await getM2Mtoken ( ) ;
119121 // eslint-disable-next-line new-cap
120122 const projectBody = new topcoderApiProjects . ProjectRequestBody . constructFromObject ( {
121123 projectName
@@ -147,7 +149,7 @@ async function createProject(projectName) {
147149 * @returns {Number } the created challenge id
148150 */
149151async function createChallenge ( challenge ) {
150- bearer . apiKey = await getAccessToken ( ) ;
152+ bearer . apiKey = await getM2Mtoken ( ) ;
151153 const start = new Date ( ) ;
152154 const startTime = moment ( start ) . toISOString ( ) ;
153155 const end = moment ( start ) . add ( config . NEW_CHALLENGE_DURATION_IN_DAYS , 'days' ) . toISOString ( ) ;
@@ -188,7 +190,7 @@ async function createChallenge(challenge) {
188190 * @param {Object } challenge the challenge to update
189191 */
190192async function updateChallenge ( id , challenge ) {
191- bearer . apiKey = await getAccessToken ( ) ;
193+ bearer . apiKey = await getM2Mtoken ( ) ;
192194 logger . debug ( `Updating challenge ${ id } with ${ circularJSON . stringify ( challenge ) } ` ) ;
193195 // eslint-disable-next-line new-cap
194196 const challengeBody = new topcoderApiChallenges . UpdateChallengeBodyParam . constructFromObject ( {
@@ -226,7 +228,7 @@ async function updateChallenge(id, challenge) {
226228 * @param {Number } id the challenge id
227229 */
228230async function activateChallenge ( id ) {
229- bearer . apiKey = await getAccessToken ( ) ;
231+ bearer . apiKey = await getM2Mtoken ( ) ;
230232 logger . debug ( `Activating challenge ${ id } ` ) ;
231233 try {
232234 const response = await new Promise ( ( resolve , reject ) => {
@@ -266,7 +268,7 @@ async function getChallengeById(id) {
266268 if ( ! _ . isNumber ( id ) ) {
267269 throw new Error ( 'The challenge id must valid number' ) ;
268270 }
269- const apiKey = await getAccessToken ( ) ;
271+ const apiKey = await getM2Mtoken ( ) ;
270272 logger . debug ( 'Getting topcoder challenge details' ) ;
271273 try {
272274 const response = await axios . get ( `${ challengesClient . basePath } /challenges/${ id } ` , {
@@ -300,7 +302,11 @@ async function closeChallenge(id, winnerId) {
300302 const apiKey = await getAccessToken ( ) ;
301303 logger . debug ( `Closing challenge ${ id } ` ) ;
302304 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 , {
304310 headers : {
305311 authorization : `Bearer ${ apiKey } ` ,
306312 'Content-Type' : 'application/json'
@@ -327,7 +333,7 @@ async function closeChallenge(id, winnerId) {
327333 * @returns {Number } the billing account id
328334 */
329335async function getProjectBillingAccountId ( id ) {
330- const apiKey = await getAccessToken ( ) ;
336+ const apiKey = await getM2Mtoken ( ) ;
331337 logger . debug ( `Getting project billing detail ${ id } ` ) ;
332338 try {
333339 const response = await axios . get ( `${ projectsClient . basePath } /direct/projects/${ id } ` , {
@@ -359,7 +365,7 @@ async function getProjectBillingAccountId(id) {
359365 * @returns {Number } the user id
360366 */
361367async function getTopcoderMemberId ( handle ) {
362- bearer . apiKey = await getAccessToken ( ) ;
368+ bearer . apiKey = await getM2Mtoken ( ) ;
363369 try {
364370 const response = await axios . get ( `${ projectsClient . basePath } /members/${ handle } ` ) ;
365371 const statusCode = response ? response . status : null ;
@@ -378,7 +384,7 @@ async function getTopcoderMemberId(handle) {
378384 * @param {Object } resource the resource resource to add
379385 */
380386async function addResourceToChallenge ( id , resource ) {
381- bearer . apiKey = await getAccessToken ( ) ;
387+ bearer . apiKey = await getM2Mtoken ( ) ;
382388 logger . debug ( `adding resource to challenge ${ id } ` ) ;
383389 try {
384390 const response = await new Promise ( ( resolve , reject ) => {
@@ -419,7 +425,7 @@ async function getResourcesFromChallenge(id) {
419425 if ( ! _ . isNumber ( id ) ) {
420426 throw new Error ( 'The challenge id must valid number' ) ;
421427 }
422- const apiKey = await getAccessToken ( ) ;
428+ const apiKey = await getM2Mtoken ( ) ;
423429 logger . debug ( `fetch resource from challenge ${ id } ` ) ;
424430 try {
425431 const response = await axios . get ( `${ challengesClient . basePath } /challenges/${ id } /resources` , {
@@ -464,7 +470,7 @@ async function roleAlreadySet(id, role) {
464470 * @param {Object } resource the resource resource to remove
465471 */
466472async function unregisterUserFromChallenge ( id ) {
467- bearer . apiKey = await getAccessToken ( ) ;
473+ bearer . apiKey = await getM2Mtoken ( ) ;
468474 logger . debug ( `removing resource from challenge ${ id } ` ) ;
469475 try {
470476 const response = await new Promise ( ( resolve , reject ) => {
@@ -501,7 +507,7 @@ async function unregisterUserFromChallenge(id) {
501507 * @param {Number } id the challenge id
502508 */
503509async function cancelPrivateContent ( id ) {
504- bearer . apiKey = await getAccessToken ( ) ;
510+ bearer . apiKey = await getM2Mtoken ( ) ;
505511 logger . debug ( `Cancelling challenge ${ id } ` ) ;
506512 try {
507513 const response = await new Promise ( ( resolve , reject ) => {
@@ -549,7 +555,7 @@ async function assignUserAsRegistrant(topcoderUserId, challengeId) {
549555 * @param {Object } resource the resource resource to remove
550556 */
551557async function removeResourceToChallenge ( id , resource ) {
552- bearer . apiKey = await getAccessToken ( ) ;
558+ bearer . apiKey = await getM2Mtoken ( ) ;
553559 logger . debug ( `removing resource from challenge ${ id } ` ) ;
554560 try {
555561 const response = await new Promise ( ( resolve , reject ) => {
@@ -579,7 +585,7 @@ async function removeResourceToChallenge(id, resource) {
579585 * @returns {Array } the resources of challenge
580586 */
581587async function getChallengeResources ( id ) {
582- const apiKey = await getAccessToken ( ) ;
588+ const apiKey = await getM2Mtoken ( ) ;
583589 logger . debug ( `getting resource from challenge ${ id } ` ) ;
584590 try {
585591 const response = await axios . get ( `${ challengesClient . basePath } /challenges/${ id } /resources` , {
0 commit comments