diff --git a/floriday.js b/floriday.js index 9a14106..10442aa 100644 --- a/floriday.js +++ b/floriday.js @@ -54,8 +54,7 @@ class Floriday { async trunk() { try{ - //await utils.syncSupplyLines(); - await utils.newSyncSupplyLines(); + await utils.syncSupplyLines(); // Continuar con todo lo que haga falta realizar en la rutina diff --git a/utils.js b/utils.js index d834100..e367577 100644 --- a/utils.js +++ b/utils.js @@ -374,131 +374,9 @@ export async function syncTradeItems(){ } /** - * Syncs the supply lines for suppliers that are connected to do this, - * it searches all the supply lines for every tradeitem of the suppliers + * Sync the supply lines for suppliers that are connected */ -export async function syncSupplyLines(){ - const spinner = ora(`Syncing supply lines...`).start(); - try { - const currentSequenceNumber = await models.sequenceNumber.findOne({ - where: { model: 'supplyLines' } - }); - - let connectedSuppliers = await models.supplier.findAll({ - where: { isConnected: true } - }); - - let tradeItems = await models.tradeItem.findAll({ - where: { supplierOrganizationId: connectedSuppliers.map(supplier => supplier.supplierOrganizationId) } - }); - - let promises = []; - - let headers = { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${await getCurrentToken()}`, - 'X-Api-Key': process.env.API_KEY - }; - - // Launch a promise for each supplier - for (let tradeItem of tradeItems) { - let supplier = connectedSuppliers.find(({ supplierOrganizationId }) => supplierOrganizationId === tradeItem.supplierOrganizationId); - // eslint-disable-next-line no-async-promise-executor - let promise = new Promise(async (resolve) => { - try { - const params = new URLSearchParams({ - supplierOrganizationId: supplier.supplierOrganizationId, - tradeItemId: tradeItem.tradeItemId, - postFilterSelectedTradeItems: false - }).toString(); - let supplyLines = (await vnRequest('GET', `${`${env.API_URL}/supply-lines`}?${params}`, null, headers)).data; - - if (!supplyLines.results.length) { - resolve([]); - return; - } - - resolve(supplyLines); - - } catch (err) { - resolve([]); - } - }); - promises.push(promise); - } - - let supplyLines = await Promise.all(promises); - let maximumSequenceNumber; - - for (let supplyLine of supplyLines) { - maximumSequenceNumber = supplyLine.maximumSequenceNumber; - supplyLine = supplyLine.results; - try { - if (supplyLine) { - for (let line of supplyLine) { - - let tradeItem = await models.tradeItem.findOne({ - where: { - tradeItemId: line.tradeItemId - } - }); - - if (!tradeItem) { - console.log('Trade item not found for supply line: ', line.supplyLineId); - console.log('Requesting data for trade item id: ', line.tradeItemId); - - let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items?tradeItemIds=${line.tradeItemId}`, null, headers)).data; - - if (!tradeItem.length) { - console.log('Trade item not found for supply line: ', line.supplyLineId); - console.log('Trade item id: ', line.tradeItemId); - continue; - } - - await insertItem(tradeItem[0]); - - tradeItem = await models.tradeItem.findOne({ - where: { - tradeItemId: line.tradeItemId - } - }); - - if (!tradeItem) { - console.log('Trade item not found for supply line: ', line.supplyLineId); - console.log('Trade item id: ', line.tradeItemId); - continue; - } - - } - - await models.supplyLine.upsert({ - ...supplyLine, - lastSync: moment(), - }); - - for (let volumePrice of line.volumePrices) - await models.volumePrices.upsert({ - unit: volumePrice.unit, - pricePerPiece: volumePrice.pricePerPiece, - supplyLineId: line.supplyLineId, - }); - } - } - } catch (err) { - throw err; - } - } - spinner.succeed(); - console.log('Found', connectedSuppliers.length, 'connected suppliers'); - - await syncSequence(currentSequenceNumber, 'supplyLines' ,maximumSequenceNumber); - } catch (err) { - spinner.fail(); - throw err; - } -} - -export async function newSyncSupplyLines() { +export async function syncSupplyLines() { const spinner = ora(`(NEW) Syncing supply lines...`).start(); try { let headers = {