2023-01-09 10:59:07 +00:00
|
|
|
import fetch from 'node-fetch';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
// Check the existence of the .env file
|
|
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
2022-12-05 12:53:30 +00:00
|
|
|
|
2022-12-19 12:21:35 +00:00
|
|
|
const _accessTokenEndpoint =
|
2023-01-09 10:59:07 +00:00
|
|
|
'https://idm.staging.floriday.io/oauth2/ausmw6b47z1BnlHkw0h7/v1/token';
|
2022-12-05 12:53:30 +00:00
|
|
|
|
2023-01-09 10:59:07 +00:00
|
|
|
import models from './models/index.js';
|
2022-12-05 12:53:30 +00:00
|
|
|
|
2022-12-19 13:14:10 +00:00
|
|
|
let AccessToken = await getClientToken();
|
|
|
|
|
2023-01-09 10:59:07 +00:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2022-12-19 13:14:10 +00:00
|
|
|
let tokenValue = AccessToken[0];
|
|
|
|
let tokenExpirationDate = AccessToken[1];
|
2022-12-05 12:53:30 +00:00
|
|
|
|
|
|
|
try {
|
2022-12-19 13:34:39 +00:00
|
|
|
// Every 30 sec query the database
|
|
|
|
setInterval(async () => {
|
2023-01-09 10:59:07 +00:00
|
|
|
console.log('Querying the API to check for new data...');
|
|
|
|
console.log('Current token expiration date: ', tokenExpirationDate);
|
2022-12-19 13:34:39 +00:00
|
|
|
|
|
|
|
if (moment().isAfter(tokenExpirationDate)) {
|
2023-01-09 10:59:07 +00:00
|
|
|
console.log('Token expired, getting a new one...');
|
2022-12-19 13:34:39 +00:00
|
|
|
AccessToken = await getClientToken();
|
|
|
|
tokenValue = AccessToken[0];
|
|
|
|
tokenExpirationDate = AccessToken[1];
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:59:07 +00:00
|
|
|
}, process.env.STATUS == 'development' ? 2500 : 5000);
|
2022-12-05 12:53:30 +00:00
|
|
|
} catch (error) {
|
2023-01-09 10:59:07 +00:00
|
|
|
console.error('Unable to connect to the database:', error);
|
2022-12-05 12:53:30 +00:00
|
|
|
}
|
2022-12-19 12:21:35 +00:00
|
|
|
|
|
|
|
async function getClientToken() {
|
2023-01-09 10:59:07 +00:00
|
|
|
const clientConfigData = await models.clientConfig.findAll();
|
2022-12-19 13:34:39 +00:00
|
|
|
|
2023-01-09 10:59:07 +00:00
|
|
|
const now = moment().format('YYYY-MM-DD HH:mm:ss');
|
2022-12-19 13:34:39 +00:00
|
|
|
const tokenExpirationDate = clientConfigData[0].tokenExpiration;
|
|
|
|
|
|
|
|
if (
|
|
|
|
clientConfigData[0].tokenExpiration == null ||
|
|
|
|
moment(now).isAfter(tokenExpirationDate)
|
|
|
|
) {
|
|
|
|
let clientId = clientConfigData[0].clientId;
|
|
|
|
let clientSecret = clientConfigData[0].clientSecret;
|
|
|
|
|
|
|
|
const tokenRequest = await fetch(_accessTokenEndpoint, {
|
2023-01-09 10:59:07 +00:00
|
|
|
method: 'POST',
|
2022-12-19 13:34:39 +00:00
|
|
|
headers: {
|
2023-01-09 10:59:07 +00:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
2022-12-19 13:34:39 +00:00
|
|
|
},
|
2022-12-20 09:50:29 +00:00
|
|
|
body: `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}&scope=role:app catalog:read supply:read organization:read network:read`,
|
2022-12-19 13:34:39 +00:00
|
|
|
});
|
2022-12-19 12:21:35 +00:00
|
|
|
|
2022-12-19 13:34:39 +00:00
|
|
|
const tokenResponse = await tokenRequest.json();
|
|
|
|
|
2023-01-09 11:08:30 +00:00
|
|
|
// If the token request is successful, show a message in the console
|
|
|
|
if (tokenRequest.status === 200) {
|
|
|
|
console.log('Token request successful');
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`Token request failed with status ${tokenRequest.status}`
|
|
|
|
);
|
|
|
|
}
|
2022-12-20 09:50:29 +00:00
|
|
|
|
2022-12-19 13:34:39 +00:00
|
|
|
const accessToken = tokenResponse.access_token;
|
2023-01-09 10:59:07 +00:00
|
|
|
let now = moment().format('YYYY-MM-DD HH:mm:ss');
|
2022-12-19 13:34:39 +00:00
|
|
|
|
|
|
|
let tokenExpirationDate = moment(now)
|
2023-01-09 10:59:07 +00:00
|
|
|
.add(tokenResponse.expires_in, 's')
|
|
|
|
.format('YYYY-MM-DD HH:mm:ss');
|
2022-12-20 09:50:29 +00:00
|
|
|
|
2022-12-19 13:34:39 +00:00
|
|
|
updateClientConfig(
|
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
accessToken,
|
|
|
|
tokenExpirationDate
|
|
|
|
);
|
|
|
|
|
|
|
|
return [accessToken, tokenExpirationDate];
|
|
|
|
} else {
|
2023-01-09 10:59:07 +00:00
|
|
|
console.log('Using the current token...');
|
2022-12-19 13:34:39 +00:00
|
|
|
return [clientConfigData[0].currentToken, tokenExpirationDate];
|
|
|
|
}
|
2022-12-19 12:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateClientConfig(
|
2022-12-19 13:34:39 +00:00
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
accessToken,
|
|
|
|
tokenExpirationDate
|
2022-12-19 12:21:35 +00:00
|
|
|
) {
|
2022-12-19 13:34:39 +00:00
|
|
|
try {
|
2022-12-20 09:50:29 +00:00
|
|
|
|
2023-01-09 10:59:07 +00:00
|
|
|
console.log('Updating the client config with the new token...');
|
2022-12-19 13:34:39 +00:00
|
|
|
await models.clientConfig.update(
|
|
|
|
{
|
|
|
|
clientId: clientId,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
currentToken: accessToken,
|
|
|
|
tokenExpiration: tokenExpirationDate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
where: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2023-01-09 10:59:07 +00:00
|
|
|
console.log('Client config updated, new Token set');
|
|
|
|
console.log('New token expiration date: ', tokenExpirationDate);
|
2022-12-19 13:34:39 +00:00
|
|
|
} catch (error) {
|
2023-01-09 10:59:07 +00:00
|
|
|
console.log('There was a error while updating the client config');
|
2022-12-19 13:34:39 +00:00
|
|
|
console.log(error);
|
|
|
|
}
|
2022-12-19 12:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log = function () {
|
2023-01-09 10:59:07 +00:00
|
|
|
let args = Array.prototype.slice.call(arguments);
|
|
|
|
args.unshift(new moment().format('HH:mm:ss') + ' -');
|
2022-12-19 13:34:39 +00:00
|
|
|
console.info.apply(console, args);
|
2022-12-19 12:21:35 +00:00
|
|
|
};
|