1515// winston logger needs to be created before any local classes that use the logger are loaded.
1616const defaultLog = require ( '../../api/helpers/logger' ) ( 'updateShapes' ) ;
1717
18- const Promise = require ( 'es6-promise' ) . Promise ;
19- const _ = require ( 'lodash' ) ;
20- const request = require ( 'request' ) ;
18+ const axios = require ( 'axios' ) ;
2119const querystring = require ( 'querystring' ) ;
2220const moment = require ( 'moment' ) ;
2321const TTLSUtils = require ( '../../api/helpers/ttlsUtils' ) ;
@@ -93,6 +91,25 @@ let jwt_login = null; // the ACRFD login token
9391let jwt_expiry = null ; // how long the token lasts before expiring
9492let jwt_login_time = null ; // time we last logged in
9593
94+ /**
95+ * Logs an http failure and normalizes it into an error to reject with.
96+ *
97+ * @param {String } caller name of the calling function, used for logging.
98+ * @param {* } error error thrown by axios.
99+ * @returns {Error } the error to reject/throw with.
100+ */
101+ const handleRequestError = function ( caller , error ) {
102+ if ( error . response ) {
103+ // the request was made and the server responded with a non 2xx status code
104+ const body = JSON . stringify ( error . response . data ) ;
105+ defaultLog . warn ( ` - ${ caller } response:` , error . response . status , body ) ;
106+ return new Error ( `${ error . response . status } ${ body } ` ) ;
107+ }
108+
109+ defaultLog . error ( ` - ${ caller } error:` , error ) ;
110+ return error ;
111+ } ;
112+
96113/**
97114 * Logs in to ACRFD.
98115 *
@@ -101,40 +118,29 @@ let jwt_login_time = null; // time we last logged in
101118 * @returns {Promise } promise that resolves with the jwt_login token.
102119 */
103120const loginToACRFD = function ( username , password ) {
104- return new Promise ( ( resolve , reject ) => {
105- const body = querystring . stringify ( {
106- grant_type : grant_type ,
107- client_id : client_id ,
108- username : username ,
109- password : password
110- } ) ;
111- const contentLength = body . length ;
112- request . post (
113- {
114- url : auth_endpoint ,
115- headers : {
116- 'Content-Length' : contentLength ,
117- 'Content-Type' : 'application/x-www-form-urlencoded'
118- } ,
119- body : body
120- } ,
121- ( error , res , body ) => {
122- if ( error ) {
123- defaultLog . error ( ' - loginToACRFD error:' , error ) ;
124- reject ( error ) ;
125- } else if ( res . statusCode !== 200 ) {
126- defaultLog . warn ( ' - loginToACRFD response:' , res . statusCode , body ) ;
127- reject ( res . statusCode + ' ' + body ) ;
128- } else {
129- const data = JSON . parse ( body ) ;
130- jwt_login = data . access_token ;
131- jwt_expiry = data . expires_in ;
132- jwt_login_time = moment ( ) ;
133- resolve ( data . access_token ) ;
134- }
135- }
136- ) ;
121+ const body = querystring . stringify ( {
122+ grant_type : grant_type ,
123+ client_id : client_id ,
124+ username : username ,
125+ password : password
137126 } ) ;
127+
128+ return axios
129+ . post ( auth_endpoint , body , {
130+ headers : {
131+ 'Content-Type' : 'application/x-www-form-urlencoded'
132+ }
133+ } )
134+ . then ( res => {
135+ const data = res . data ;
136+ jwt_login = data . access_token ;
137+ jwt_expiry = data . expires_in ;
138+ jwt_login_time = moment ( ) ;
139+ return data . access_token ;
140+ } )
141+ . catch ( error => {
142+ throw handleRequestError ( 'loginToACRFD' , error ) ;
143+ } ) ;
138144} ;
139145
140146/**
@@ -162,40 +168,26 @@ const renewJWTLogin = function() {
162168 */
163169const getApplicationsToUnpublish = function ( ) {
164170 defaultLog . info ( ' - fetching retired applications.' ) ;
165- return new Promise ( ( resolve , reject ) => {
166- const untilDate = moment ( ) . subtract ( 6 , 'months' ) ;
171+ const untilDate = moment ( ) . subtract ( 6 , 'months' ) ;
167172
168- // get all applications that are in a retired status and that have a last status update date older than 6 months ago.
169- let queryString = `?statusHistoryEffectiveDate[until]=${ untilDate . toISOString ( ) } ` ;
170- retiredStatuses . forEach ( status => ( queryString += `&status[eq]=${ encodeURIComponent ( status ) } ` ) ) ;
173+ // get all applications that are in a retired status and that have a last status update date older than 6 months ago.
174+ let queryString = `?statusHistoryEffectiveDate[until]=${ untilDate . toISOString ( ) } ` ;
175+ retiredStatuses . forEach ( status => ( queryString += `&status[eq]=${ encodeURIComponent ( status ) } ` ) ) ;
171176
172- request . get (
173- {
174- url : uri + 'api/application' + queryString ,
175- headers : {
176- 'Content-Type' : 'application/json' ,
177- Authorization : 'Bearer ' + jwt_login
178- }
179- } ,
180- ( error , res , body ) => {
181- if ( error ) {
182- defaultLog . error ( ' - getApplicationsToUnpublish error:' , error , res , body ) ;
183- reject ( error ) ;
184- } else if ( res . statusCode !== 200 ) {
185- defaultLog . warn ( ' - getApplicationsToUnpublish response:' , res . statusCode , body ) ;
186- reject ( res . statusCode + ' ' + body ) ;
187- } else {
188- const data = JSON . parse ( body ) ;
189-
190- // only return applications that are currently published
191- const appsToUnpublish = _ . filter ( data , app => {
192- return Actions . isPublished ( app ) ;
193- } ) ;
194- resolve ( appsToUnpublish ) ;
195- }
177+ return axios
178+ . get ( uri + 'api/application' + queryString , {
179+ headers : {
180+ 'Content-Type' : 'application/json' ,
181+ Authorization : 'Bearer ' + jwt_login
196182 }
197- ) ;
198- } ) ;
183+ } )
184+ . then ( res => {
185+ // only return applications that are currently published
186+ return res . data . filter ( app => Actions . isPublished ( app ) ) ;
187+ } )
188+ . catch ( error => {
189+ throw handleRequestError ( 'getApplicationsToUnpublish' , error ) ;
190+ } ) ;
199191} ;
200192
201193/**
@@ -207,31 +199,20 @@ const getApplicationsToUnpublish = function() {
207199const unpublishApplications = function ( applicationsToUnpublish ) {
208200 return applicationsToUnpublish . reduce ( ( previousApp , currentApp ) => {
209201 return previousApp . then ( ( ) => {
210- return new Promise ( ( resolve , reject ) => {
211- request . put (
212- {
213- url : uri + 'api/application/' + currentApp . _id + '/unpublish' ,
214- headers : {
215- 'Content-Type' : 'application/json' ,
216- Authorization : 'Bearer ' + jwt_login
217- } ,
218- body : JSON . stringify ( currentApp )
219- } ,
220- ( error , res , body ) => {
221- if ( error ) {
222- defaultLog . error ( ' - unpublishApplications error:' , error ) ;
223- reject ( error ) ;
224- } else if ( res . statusCode !== 200 ) {
225- defaultLog . warn ( ' - unpublishApplications response:' , res . statusCode , body ) ;
226- reject ( res . statusCode + ' ' + body ) ;
227- } else {
228- defaultLog . info ( ` - Unpublished application, _id: ${ currentApp . _id } ` ) ;
229- const data = JSON . parse ( body ) ;
230- resolve ( data ) ;
231- }
202+ return axios
203+ . put ( uri + 'api/application/' + currentApp . _id + '/unpublish' , currentApp , {
204+ headers : {
205+ 'Content-Type' : 'application/json' ,
206+ Authorization : 'Bearer ' + jwt_login
232207 }
233- ) ;
234- } ) ;
208+ } )
209+ . then ( res => {
210+ defaultLog . info ( ` - Unpublished application, _id: ${ currentApp . _id } ` ) ;
211+ return res . data ;
212+ } )
213+ . catch ( error => {
214+ throw handleRequestError ( 'unpublishApplications' , error ) ;
215+ } ) ;
235216 } ) ;
236217 } , Promise . resolve ( ) ) ;
237218} ;
@@ -243,36 +224,21 @@ const unpublishApplications = function(applicationsToUnpublish) {
243224 * @returns {Promise }
244225 */
245226const updateACRFDApplication = function ( acrfdAppID ) {
246- return new Promise ( ( resolve , reject ) => {
247- // only update the ones that aren't deleted
248- const url = uri + `api/application/${ acrfdAppID } /refresh` ;
249- request . put (
250- {
251- url : url ,
252- headers : {
253- 'Content-Type' : 'application/json' ,
254- Authorization : 'Bearer ' + jwt_login
255- }
256- } ,
257- ( error , res , body ) => {
258- if ( error ) {
259- defaultLog . error ( ' - updateACRFDApplication error:' , error ) ;
260- reject ( error ) ;
261- } else if ( res . statusCode !== 200 ) {
262- defaultLog . warn ( ' - updateACRFDApplication response:' , res . statusCode , body ) ;
263- reject ( res . statusCode + ' ' + body ) ;
264- } else {
265- let obj = { } ;
266- try {
267- obj = JSON . parse ( body ) ;
268- resolve ( obj ) ;
269- } catch ( e ) {
270- defaultLog . info ( ' - updateACRFDApplication parse error:' , e ) ;
271- }
272- }
227+ // only update the ones that aren't deleted
228+ const url = uri + `api/application/${ acrfdAppID } /refresh` ;
229+
230+ return axios
231+ . put ( url , undefined , {
232+ // the refresh route takes no body
233+ headers : {
234+ 'Content-Type' : 'application/json' ,
235+ Authorization : 'Bearer ' + jwt_login
273236 }
274- ) ;
275- } ) ;
237+ } )
238+ . then ( res => res . data )
239+ . catch ( error => {
240+ throw handleRequestError ( 'updateACRFDApplication' , error ) ;
241+ } ) ;
276242} ;
277243
278244/**
@@ -283,36 +249,20 @@ const updateACRFDApplication = function(acrfdAppID) {
283249 * @returns {Promise } promise that resolves with an array of ACRFD applications.
284250 */
285251const getAllACRFDApplicationIDs = function ( ) {
286- return new Promise ( ( resolve , reject ) => {
287- // only update the ones that aren't deleted
288- const url = uri + 'api/application/' + '?fields=tantalisID&isDeleted=false' ;
289- request . get (
290- {
291- url : url ,
292- headers : {
293- 'Content-Type' : 'application/json' ,
294- Authorization : 'Bearer ' + jwt_login
295- }
296- } ,
297- ( error , res , body ) => {
298- if ( error ) {
299- defaultLog . error ( ' - getAllACRFDApplicationIDs error:' , error ) ;
300- reject ( error ) ;
301- } else if ( res . statusCode !== 200 ) {
302- defaultLog . warn ( ' - getAllACRFDApplicationIDs response:' , res . statusCode , body ) ;
303- reject ( res . statusCode + ' ' + body ) ;
304- } else {
305- let obj = { } ;
306- try {
307- obj = JSON . parse ( body ) ;
308- resolve ( obj ) ;
309- } catch ( e ) {
310- defaultLog . info ( ' - getAllACRFDApplicationIDs parse error:' , e ) ;
311- }
312- }
252+ // only update the ones that aren't deleted
253+ const url = uri + 'api/application/' + '?fields=tantalisID&isDeleted=false' ;
254+
255+ return axios
256+ . get ( url , {
257+ headers : {
258+ 'Content-Type' : 'application/json' ,
259+ Authorization : 'Bearer ' + jwt_login
313260 }
314- ) ;
315- } ) ;
261+ } )
262+ . then ( res => res . data )
263+ . catch ( error => {
264+ throw handleRequestError ( 'getAllACRFDApplicationIDs' , error ) ;
265+ } ) ;
316266} ;
317267
318268/**
0 commit comments