@@ -6,6 +6,8 @@ const request = require('superagent');
66const config = require ( 'config' ) ;
77const _ = require ( 'lodash' ) ;
88
9+ const debug = require ( 'debug' ) ( 'app:project-service' ) ;
10+
911/**
1012 * Get project details
1113 *
@@ -14,25 +16,22 @@ const _ = require('lodash');
1416 * @return {Promise } promise resolved to project details
1517 */
1618const getProject = ( projectId ) => {
17- console . log ( `AUTH0_CLIENT_ID: ${ config . AUTH0_CLIENT_ID . substring ( 0 , 5 ) } ` ) ;
18- console . log ( `AUTH0_CLIENT_SECRET: ${ config . AUTH0_CLIENT_SECRET . substring ( 0 , 5 ) } ` ) ;
19- console . log ( `AUTH0_URL: ${ config . AUTH0_URL } ` ) ;
20- console . log ( `AUTH0_AUDIENCE: ${ config . AUTH0_AUDIENCE } ` ) ;
21- console . log ( `AUTH0_PROXY_SERVER_URL: ${ config . AUTH0_PROXY_SERVER_URL } ` ) ;
19+ debug ( `AUTH0_CLIENT_ID: ${ config . AUTH0_CLIENT_ID . substring ( 0 , 5 ) } ` ) ;
20+ debug ( `AUTH0_CLIENT_SECRET: ${ config . AUTH0_CLIENT_SECRET . substring ( 0 , 5 ) } ` ) ;
2221 return M2m . getMachineToken ( config . AUTH0_CLIENT_ID , config . AUTH0_CLIENT_SECRET )
2322 . then ( ( token ) => (
2423 request
2524 . get ( `${ config . projectApi . url } /projects/${ projectId } ` )
2625 . set ( 'accept' , 'application/json' )
2726 . set ( 'authorization' , `Bearer ${ token } ` )
2827 . then ( ( res ) => {
29- if ( ! _ . get ( res , 'body.result.success' ) ) {
28+ if ( res . status !== 200 ) {
3029 throw new Error ( `Failed to get project details of project id: ${ projectId } ` ) ;
3130 }
32- const project = _ . get ( res , 'body.result.content ' ) ;
31+ const project = _ . get ( res , 'body' ) ;
3332 return project ;
3433 } ) . catch ( ( err ) => {
35- const errorDetails = _ . get ( err , 'response.body.result.content.message ' ) ;
34+ const errorDetails = _ . get ( err , 'response.body' ) ;
3635 throw new Error (
3736 `Failed to get project details of project id: ${ projectId } .` +
3837 ( errorDetails ? ' Server response: ' + errorDetails : '' )
@@ -52,31 +51,32 @@ const getProject = (projectId) => {
5251 *
5352 * @return {Promise } promise resolved to the updated project
5453 */
55- const activateProject = ( projectId ) => {
56- console . log ( `AUTH0_CLIENT_ID: ${ config . AUTH0_CLIENT_ID . substring ( 0 , 5 ) } ` ) ;
57- console . log ( `AUTH0_CLIENT_SECRET: ${ config . AUTH0_CLIENT_SECRET . substring ( 0 , 5 ) } ` ) ;
58- console . log ( `AUTH0_URL: ${ config . AUTH0_URL } ` ) ;
59- console . log ( `AUTH0_AUDIENCE: ${ config . AUTH0_AUDIENCE } ` ) ;
60- console . log ( `AUTH0_PROXY_SERVER_URL: ${ config . AUTH0_PROXY_SERVER_URL } ` ) ;
54+ const updateProjectStatus = ( projectId , status = 'active' , changeReason ) => {
55+ debug ( `AUTH0_CLIENT_ID: ${ config . AUTH0_CLIENT_ID . substring ( 0 , 5 ) } ` ) ;
56+ debug ( `AUTH0_CLIENT_SECRET: ${ config . AUTH0_CLIENT_SECRET . substring ( 0 , 5 ) } ` ) ;
57+ const updatedProject = { status } ;
58+ if ( changeReason ) {
59+ updatedProject . cancelReason = changeReason ;
60+ }
6161 return M2m . getMachineToken ( config . AUTH0_CLIENT_ID , config . AUTH0_CLIENT_SECRET )
6262 . then ( ( token ) => (
6363 request
6464 . patch ( `${ config . projectApi . url } /projects/${ projectId } ` )
6565 . set ( 'accept' , 'application/json' )
6666 . set ( 'Authorization' , `Bearer ${ token } ` )
67- . send ( { param : { status : 'active' } } )
67+ . send ( updatedProject )
6868 . then ( ( res ) => {
69- if ( ! _ . get ( res , 'body.result.success' ) ) {
70- throw new Error ( `Failed to activate project with id: ${ projectId } ` ) ;
69+ if ( res . status !== 200 ) {
70+ throw new Error ( `Failed to update project with id: ${ projectId } ` ) ;
7171 }
72- const project = _ . get ( res , 'body.result.content ' ) ;
72+ const project = _ . get ( res , 'body' ) ;
7373 if ( project ) {
74- console . log ( `Successfully activated the project with id ${ projectId } ` ) ;
74+ debug ( `Successfully updated the project ${ projectId } with status ${ status } ` ) ;
7575 }
7676 return project ;
7777 } ) . catch ( ( err ) => {
78- console . log ( err ) ;
79- const errorDetails = _ . get ( err , 'response.body.result.content.message ' ) ;
78+ debug ( err ) ;
79+ const errorDetails = _ . get ( err , 'response.body' ) ;
8080 throw new Error (
8181 `Failed to update project with id: ${ projectId } .` +
8282 ( errorDetails ? ' Server response: ' + errorDetails : '' )
@@ -91,5 +91,5 @@ const activateProject = (projectId) => {
9191
9292module . exports = {
9393 getProject,
94- activateProject
94+ updateProjectStatus
9595} ;
0 commit comments