refs #4823 Added new function vnRequest

This commit is contained in:
Guillermo Bonet 2023-05-12 09:13:03 +02:00
parent 8fc7660183
commit 2a195194b2
2 changed files with 68 additions and 57 deletions

View File

@ -55,6 +55,7 @@ SYNC_SEQUENCE = true
SYNC_SUPPLIER = true SYNC_SUPPLIER = true
SYNC_CONN = true SYNC_CONN = true
SYNC_TRADEITEM = true SYNC_TRADEITEM = true
MAX_REQUEST_RETRIES = 3
``` ```
## Guidelines ## Guidelines

124
utils.js
View File

@ -34,25 +34,22 @@ export async function requestToken() {
clientSecret = env.CLIENT_SECRET clientSecret = env.CLIENT_SECRET
}; };
const data = { let data = {
grant_type: 'client_credentials', grant_type: 'client_credentials',
client_id: clientId, client_id: clientId,
client_secret: clientSecret, client_secret: clientSecret,
scope: 'role:app catalog:read supply:read organization:read network:write network:read' scope: 'role:app catalog:read supply:read organization:read network:write network:read'
}; };
const response = await axios.post(env.API_ENDPOINT, data = Object.keys(data)
Object.keys(data) .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`) .join('&')
.join('&'),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
}
);
const responseData = response.data; const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
const response = (await vnRequest('GET', env.API_ENDPOINT, data, headers)).data;
if (response.statusText = 'OK') if (response.statusText = 'OK')
spinner.succeed(); spinner.succeed();
@ -61,13 +58,13 @@ export async function requestToken() {
criticalError(new Error(`Token request failed with status: ${response.status} - ${response.statusText}`)); criticalError(new Error(`Token request failed with status: ${response.status} - ${response.statusText}`));
} }
let tokenExpirationDate = moment() let tokenExpirationDate = moment()
.add(responseData.expires_in, 's') .add(response.expires_in, 's')
.format('YYYY-MM-DD HH:mm:ss'); .format('YYYY-MM-DD HH:mm:ss');
await updateClientConfig( await updateClientConfig(
clientId, clientId,
clientSecret, clientSecret,
responseData.access_token, response.access_token,
tokenExpirationDate tokenExpirationDate
); );
@ -278,19 +275,12 @@ export async function syncSuppliers(){
'X-Api-Key': process.env.API_KEY, 'X-Api-Key': process.env.API_KEY,
}; };
const response = await axios.get(`${env.API_URL}/organizations/current-max-sequence`, { headers }); const maxSequenceNumber = (await vnRequest('GET', `${env.API_URL}/organizations/current-max-sequence`, null, headers)).data;
if (!response.ok) {
spinner.fail();
criticalError(new Error(`Max sequence request failed with status: ${response.status} - ${response.statusText}`));
}
const maxSequenceNumber = response.data;
let timeFinish, timeToGoSec, timeToGoMin, timeLeft; let timeFinish, timeToGoSec, timeToGoMin, timeLeft;
for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) { for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
let timeStart = new moment(); let timeStart = new moment();
let response = await axios.get(`${env.API_URL}/organizations/sync/${curSequenceNumber}?organizationType=SUPPLIER`, { headers }); let data = (await vnRequest('GET', `${env.API_URL}/organizations/sync/${curSequenceNumber}?organizationType=SUPPLIER`, null, headers)).data;
let data = response.data;
let suppliers = data.results; let suppliers = data.results;
for (let supplier of suppliers) { for (let supplier of suppliers) {
@ -343,15 +333,16 @@ export async function syncConn(){
'X-Api-Key': process.env.API_KEY 'X-Api-Key': process.env.API_KEY
}; };
const remoteConnections = await axios.get(`${env.API_URL}/connections`, { headers }); const remoteConnections = (await vnRequest('GET', `${env.API_URL}/connections`, null, headers)).data;
let i = 1; let i = 1;
for (let connection of connections){ for (let connection of connections){
spinner.text = `Creating ${i++} connections...` spinner.text = `Creating ${i++} connections...`
let remoteConnection = remoteConnections.data.find(remoteConnection => remoteConnection == connection.supplierOrganizationId); let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.supplierOrganizationId);
if (!remoteConnection){ if (!remoteConnection){
await axios.put(`${env.API_URL}/connections/${connection.supplierOrganizationId}`, null, { headers }); await vnRequest('PUT', `${env.API_URL}/connections/${connection.supplierOrganizationId}`, null, headers);
await models.connection.update({ isConnected: true }, { await models.connection.update({ isConnected: true }, {
where: { where: {
supplierOrganizationId: connection.supplierOrganizationId supplierOrganizationId: connection.supplierOrganizationId
@ -387,7 +378,8 @@ export async function syncTradeItems(){
const suppliers = await models.supplier.findAll({ const suppliers = await models.supplier.findAll({
where: { isConnected: true } where: { isConnected: true }
}); });
let i = 1; let i = 0;
let x = 0;
for (let supplier of suppliers) { for (let supplier of suppliers) {
let headers = { let headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -395,16 +387,14 @@ export async function syncTradeItems(){
'X-Api-Key': process.env.API_KEY 'X-Api-Key': process.env.API_KEY
}; };
try { try {
let request = await axios.get(`${env.API_URL}/trade-items?supplierOrganizationId=${supplier.supplierOrganizationId}`, { headers }) let tradeItems = (await vnRequest('GET', `${env.API_URL}/trade-items?supplierOrganizationId=${supplier.supplierOrganizationId}`, null, headers)).data
let tradeItems = request.data; spinner.text = `Syncing ${i} trade items of [${x++}|${suppliers.length}] suppliers...`
if (!tradeItems.length) continue;
if (!tradeItems.length)
continue;
for (let tradeItem of tradeItems) { for (let tradeItem of tradeItems) {
await insertItem(tradeItem); await insertItem(tradeItem);
spinner.text = `Syncing ${i++} trade items...` spinner.text = `Syncing ${i++} trade items of [${x++}|${suppliers.length}] suppliers...`
}; };
} catch (err) { } catch (err) {
spinner.fail(); spinner.fail();
@ -452,14 +442,7 @@ export async function syncSupplyLines(){
tradeItemId: tradeItem.tradeItemId, tradeItemId: tradeItem.tradeItemId,
postFilterSelectedTradeItems: false postFilterSelectedTradeItems: false
}).toString(); }).toString();
let request = await axios.get(`${`${env.API_URL}/supply-lines`}?${params}`, { headers }); let supplyLines = (await vnRequest('GET', `${`${env.API_URL}/supply-lines`}?${params}`, null, headers)).data;
if (request.status == 429) { // Too many request
resolve([]);
return;
}
let supplyLines = request.data;
if (!supplyLines.results.length) { if (!supplyLines.results.length) {
resolve([]); resolve([]);
@ -497,11 +480,9 @@ export async function syncSupplyLines(){
console.log('Trade item not found for supply line: ', line.supplyLineId); console.log('Trade item not found for supply line: ', line.supplyLineId);
console.log('Requesting data for trade item id: ', line.tradeItemId); console.log('Requesting data for trade item id: ', line.tradeItemId);
let queryTradeItem = await axios.get(`${env.API_URL}/trade-items?tradeItemIds=${line.tradeItemId}`, { headers }); let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items?tradeItemIds=${line.tradeItemId}`, null, headers)).data;
let tradeItem = queryTradeItem.data; if (!tradeItem.length) {
if (tradeItem.length == 0) {
console.log('Trade item not found for supply line: ', line.supplyLineId); console.log('Trade item not found for supply line: ', line.supplyLineId);
console.log('Trade item id: ', line.tradeItemId); console.log('Trade item id: ', line.tradeItemId);
continue; continue;
@ -569,6 +550,7 @@ export async function syncSupplyLines(){
} }
export async function newSyncSupplyLines() { export async function newSyncSupplyLines() {
const spinner = ora(`(NEW) Syncing supply lines...`).start();
try { try {
let headers = { let headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -592,24 +574,22 @@ export async function newSyncSupplyLines() {
}); });
}).map(supplier => supplier.supplierOrganizationId); }).map(supplier => supplier.supplierOrganizationId);
let i = 1;
for (let supplier of suppliers) { for (let supplier of suppliers) {
spinner.text = `(NEW) Syncing ${i++} supply lines...`
const params = new URLSearchParams({ const params = new URLSearchParams({
supplierOrganizationId: supplier, supplierOrganizationId: supplier,
}).toString(); }).toString();
let request = await axios.get(`${`${env.API_URL}/supply-lines`}?${params}`, { headers }); let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`, null, headers)).data;
if (!supplyLines.length) continue
if (!request.data.length) for (let supplyLine of supplyLines)
continue
let supplyLines = request.data;
for (let supplyLine of supplyLines) {
console.log(supplyLine) console.log(supplyLine)
}
} }
console.log('Finalizado') spinner.succeed();
} }
catch (err) { catch (err) {
spinner.fail();
throw err; throw err;
} }
} }
@ -718,9 +698,9 @@ export async function deleteConnections() {
'Authorization': `Bearer ${await getCurrentToken()}`, 'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY 'X-Api-Key': process.env.API_KEY
}; };
let connections = (await axios.get(`${env.API_URL}/connections`, { headers })).data; let connections = (await vnRequest('GET', `${env.API_URL}/connections`, null, headers)).data;
for (let connection of connections) { for (let connection of connections) {
await axios.delete(`${env.API_URL}/connections/${connection}`, { headers }); await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`, null, headers);
spinner.text = `Deleting ${i++} connections...` spinner.text = `Deleting ${i++} connections...`
} }
spinner.succeed(); spinner.succeed();
@ -730,6 +710,36 @@ export async function deleteConnections() {
} }
} }
/**
* Perform a REST request
*
* @param {string} url
* @param {string} method
* @param {array} body
* @param {array} header
*
* @return {array}
**/
export async function vnRequest(method = 'GET', url, data = null, headers = {}) {
let response;
for(let i = 0; i < env.MAX_REQUEST_RETRIES; i++) {
try {
if (['GET', 'DELETE'].includes(method))
response = await axios({method, url, headers});
else
response = await axios({method, url, data, headers});
break;
} catch (err) {
if (err.code == 'ECONNRESET')
warning(err)
else
criticalError(err)
await sleep(1000)
}
}
return response;
}
/** /**
* Critical error * Critical error
* *