Skip to content

Commit 050d64b

Browse files
authored
Merge pull request #26 from bcgov/update-shapes-failure-fix
fix: Update updateShapes.js
2 parents 89b3a42 + 404f78f commit 050d64b

2 files changed

Lines changed: 100 additions & 150 deletions

File tree

.github/workflows/analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ jobs:
104104
steps:
105105
- uses: actions/checkout@v6
106106
- name: Run Trivy vulnerability scanner in repo mode
107-
uses: aquasecurity/trivy-action@b6643a29fecd7f34b3597bc6acb0a98b03d33ff8 # 0.33.1
107+
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
108108
with:
109109
format: "sarif"
110110
output: "trivy-results.sarif"

backend/seed/shapesMigration/updateShapes.js

Lines changed: 99 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
// winston logger needs to be created before any local classes that use the logger are loaded.
1616
const 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');
2119
const querystring = require('querystring');
2220
const moment = require('moment');
2321
const TTLSUtils = require('../../api/helpers/ttlsUtils');
@@ -93,6 +91,25 @@ let jwt_login = null; // the ACRFD login token
9391
let jwt_expiry = null; // how long the token lasts before expiring
9492
let 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
*/
103120
const 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
*/
163169
const 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() {
207199
const 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
*/
245226
const 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
*/
285251
const 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

Comments
 (0)