diff --git a/utils.js b/utils.js index f0f36a6..4f24f00 100644 --- a/utils.js +++ b/utils.js @@ -191,13 +191,7 @@ export async function asyncQueue(fnArray, concurrency = 1) { export async function syncSuppliers(){ let spinner = ora('Preparing to load suppliers...').start(); try { - let headers = { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${await getCurrentToken()}`, - 'X-Api-Key': process.env.API_KEY, - }; - - const maxSequenceNumber = (await vnRequest('GET', `${env.API_URL}/organizations/current-max-sequence`, null, headers)).data; + const maxSequenceNumber = (await vnRequest('GET', `${env.API_URL}/organizations/current-max-sequence`)).data; let timeFinish, timeToGoSec, timeToGoMin, timeLeft; for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) { @@ -245,13 +239,7 @@ export async function syncConnections(){ where: { isConnected: true } }); - let headers = { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${await getCurrentToken()}`, - 'X-Api-Key': process.env.API_KEY, - }; - - const remoteConnections = (await vnRequest('GET', `${env.API_URL}/connections`, null, headers)).data; + const remoteConnections = (await vnRequest('GET', `${env.API_URL}/connections`)).data; let i = 1; for (let connection of connections) { @@ -259,7 +247,7 @@ export async function syncConnections(){ let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.organizationId); if (!remoteConnection) - await vnRequest('PUT', `${env.API_URL}/connections/${connection.organizationId}`, null, headers); + await vnRequest('PUT', `${env.API_URL}/connections/${connection.organizationId}`); } spinner.succeed(); } catch (err) { @@ -279,16 +267,11 @@ export async function syncTradeItems(){ }); let i = 1, x = 1; for (let supplier of suppliers) { - let headers = { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${await getCurrentToken()}`, - 'X-Api-Key': process.env.API_KEY - }; try { const params = new URLSearchParams({ supplierOrganizationId: supplier.organizationId, }).toString(); - let tradeItems = (await vnRequest('GET', `${env.API_URL}/trade-items?${params}`, null, headers)).data + let tradeItems = (await vnRequest('GET', `${env.API_URL}/trade-items?${params}`)).data spinner.text = `Syncing ${i} trade items of [${x++}|${suppliers.length}] suppliers...` if (!tradeItems.length) continue; @@ -311,12 +294,6 @@ export async function syncTradeItems(){ export async function syncSupplyLines() { const spinner = ora(`Syncing supply lines...`).start(); try { - let headers = { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${await getCurrentToken()}`, - 'X-Api-Key': process.env.API_KEY - }; - let suppliersWithTradeItem = await models.tradeItem.findAll({ attributes: ['organizationId'], group: ['organizationId'] @@ -339,7 +316,7 @@ export async function syncSupplyLines() { const params = new URLSearchParams({ supplierOrganizationId: supplier, }).toString(); - let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`, null, headers)).data; + let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`)).data; if (!supplyLines.length) continue for (let supplyLine of supplyLines) { @@ -349,7 +326,7 @@ export async function syncSupplyLines() { }); if (!tradeItem) { - let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items?tradeItemIds=${supplyLine.tradeItemId}`, null, headers)).data; + let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items?tradeItemIds=${supplyLine.tradeItemId}`)).data; insertItem(tradeItem[0]) } @@ -482,20 +459,14 @@ export async function insertItem(tradeItem) { export async function syncWarehouses(){ let spinner = ora('Syncing warehouses...').start(); try { - let headers = { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${await getCurrentToken()}`, - 'X-Api-Key': process.env.API_KEY, - }; - const suppliers = await models.organization.findAll({ where: { isConnected: true } }); let x = 0, i = 1; for (let supplier of suppliers) { - spinner.text = `Syncing ${i} warehouses of [${x++}|${suppliers.length}]...` - const warehouses = (await vnRequest('GET', `${env.API_URL}/organizations/supplier/${supplier.organizationId}/warehouses`, null, headers)).data; + spinner.text = `Syncing ${i} warehouses of [${x++}|${suppliers.length}] suppliers...` + const warehouses = (await vnRequest('GET', `${env.API_URL}/organizations/supplier/${supplier.organizationId}/warehouses`)).data; for (let warehouse of warehouses) { spinner.text = `Syncing ${i++} warehouses of [${x}|${suppliers.length}] suppliers...` await models.warehouses.upsert({ @@ -525,13 +496,7 @@ export async function deleteConnections() { let spinner; try { let i = 1; - const headers = { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${await getCurrentToken()}`, - 'X-Api-Key': process.env.API_KEY - }; - - const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`, null, headers)).data; + const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`)).data; const connectionsInDb = await models.organization.findAll({ attributes: ['organizationId'], where: { isConnected: true } @@ -573,6 +538,13 @@ export async function deleteConnections() { * @return {array} **/ export async function vnRequest(method, url, data, headers) { + if (!headers) + headers = { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${await getCurrentToken()}`, + 'X-Api-Key': process.env.API_KEY, + }; + for(let i = 0; i < env.MAX_REQUEST_RETRIES; i++) { try { if (['GET', 'DELETE'].includes(method))