refs #4823 Fix on axios.put

This commit is contained in:
Guillermo Bonet 2023-05-11 09:43:27 +02:00
parent 1a746968b8
commit 4a264cf147
1 changed files with 11 additions and 9 deletions

View File

@ -20,11 +20,13 @@ export async function requestToken() {
const clientConfigData = await models.clientConfig.findOne();
let tokenExpirationDate;
if (clientConfigData)
let tokenExpirationDate, token;
if (clientConfigData) {
token = clientConfigData.currentToken;
tokenExpirationDate = clientConfigData.tokenExpiration;
}
if (!tokenExpirationDate || moment().isAfter(tokenExpirationDate)) {
if (!token || !tokenExpirationDate || moment().isAfter(tokenExpirationDate)) {
let clientId, clientSecret
if (JSON.parse(env.USE_SECRETS_DB)) {
clientId = clientConfigData.clientId;
@ -343,7 +345,7 @@ export async function syncConn(){
'X-Api-Key': process.env.API_KEY
};
let remoteConnections = await axios.get(`${env.API_URL}/connections`, { headers });
const remoteConnections = await axios.get(`${env.API_URL}/connections`, { headers });
let i = 1;
for (let connection of connections){
@ -352,10 +354,10 @@ export async function syncConn(){
continue;
let remoteConnection = remoteConnections.data.find(remoteConnection => remoteConnection == connection.supplierOrganizationId);
if (remoteConnection == undefined){
console.log('Connection: ', connection, 'does not exist in the remote server');
if (!remoteConnection){
console.log('Connection: ', connection.supplierOrganizationId, 'does not exist in the remote server');
console.log('Creating remote connection');
await axios.put(`${env.API_URL}/connections/${connection.supplierOrganizationId}`, { headers });
await axios.put(`${env.API_URL}/connections/${connection.supplierOrganizationId}`, null, { headers });
await models.connection.update({ isConnected: true }, {
where: {
supplierOrganizationId: connection.supplierOrganizationId
@ -474,8 +476,8 @@ export async function syncSupplyLines(){
resolve(supplyLines);
} catch (err) {
console.log('Error while syncing supply lines for: ', supplier.commercialName, ' - ' , tradeItem.name);
console.log(err);
if (err.message = 'Request failed with status code 429')
console.log('Request failed with status code 429 - Too many request');
resolve([]);
}
});