From 4aac074a8b2a4b9ee3704a256dc5f635a2543b83 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 9 May 2023 13:14:54 +0200 Subject: [PATCH] refs #4823 Suppliers to supplier and volumePrices --- floriday.js | 2 +- models/index.js | 12 +++--- models/supplier/{suppliers.js => supplier.js} | 2 +- models/supplyLine/volumePrices.js | 9 ++-- utils.js | 42 +++++++++---------- 5 files changed, 33 insertions(+), 34 deletions(-) rename models/supplier/{suppliers.js => supplier.js} (94%) diff --git a/floriday.js b/floriday.js index 6c7aae2..d1bc5dd 100644 --- a/floriday.js +++ b/floriday.js @@ -62,7 +62,7 @@ class Floriday { // Continuar con todo lo que haga falta realizar en la rutina } catch (err) { - throw err; + utils.criticalError(err); } } diff --git a/models/index.js b/models/index.js index 62dfa80..4363f7f 100644 --- a/models/index.js +++ b/models/index.js @@ -38,7 +38,7 @@ import clientConfig from './conf/clientConfig.js'; import sequenceNumber from './conf/sequenceNumber.js'; // Supplier Models -import suppliers from './supplier/suppliers.js'; +import supplier from './supplier/supplier.js'; import connections from './supplier/connections.js'; // TradeItem Models @@ -83,8 +83,8 @@ let models = { clientConfig: clientConfig(sequelize), botanicalName: botanicalNames(sequelize), supplyLine: supplyLine(sequelize), - volumePrice: volumePrices(sequelize), - supplier: suppliers(sequelize), + volumePrices: volumePrices(sequelize), + supplier: supplier(sequelize), sequenceNumber: sequenceNumber(sequelize), connection: connections(sequelize), }; @@ -147,8 +147,8 @@ try { targetKey: 'tradeItemId', }); - models.volumePrice.belongsTo(models.supplyLine, { - foreignKey: 'supplyLineFk', + models.volumePrices.belongsTo(models.supplyLine, { + foreignKey: 'supplyLineId', as: 'supplyLine_Fk', targetKey: 'supplyLineId', }); @@ -171,7 +171,7 @@ try { targetKey: 'supplierOrganizationId', }); } catch (err) { - throw new Error(err) + criticalError(err); } let action, isForce; diff --git a/models/supplier/suppliers.js b/models/supplier/supplier.js similarity index 94% rename from models/supplier/suppliers.js rename to models/supplier/supplier.js index 269aad2..ff3a699 100644 --- a/models/supplier/suppliers.js +++ b/models/supplier/supplier.js @@ -51,7 +51,7 @@ const suppliers = { }; export default (sequelize) => { - const Suppliers = sequelize.define('suppliers', suppliers, { + const Suppliers = sequelize.define('supplier', suppliers, { timestamps: false, freezeTableName: true, }); diff --git a/models/supplyLine/volumePrices.js b/models/supplyLine/volumePrices.js index 103d078..4b70bf5 100644 --- a/models/supplyLine/volumePrices.js +++ b/models/supplyLine/volumePrices.js @@ -1,16 +1,17 @@ import { Sequelize } from 'sequelize'; const volumePrices = { - id: { - type: Sequelize.INTEGER, + supplyLineId: { + type: Sequelize.STRING, primaryKey: true, - autoIncrement: true, }, unit: { type: Sequelize.STRING, + primaryKey: true, }, pricePerPiece: { - type: Sequelize.INTEGER, + type: Sequelize.DECIMAL(10,2), + primaryKey: true, }, }; diff --git a/utils.js b/utils.js index be52811..52f3514 100644 --- a/utils.js +++ b/utils.js @@ -3,7 +3,7 @@ import fetch from 'node-fetch'; import { models } from './models/index.js'; import { v4 as uuidv4 } from 'uuid'; import chalk from 'chalk'; -import ora from 'ora'; +import ora, { spinners } from 'ora'; const env = process.env; /** @@ -218,7 +218,7 @@ export async function syncSequence(current = 0, model = null , maximumSequenceNu try { const spinner = ora(`Syncing sequence...`).start(); let mockModels = [ - 'suppliers', + 'supplier', 'tradeItems', 'supplyLines', ]; @@ -322,7 +322,7 @@ export async function syncSuppliers(){ physicalAddress: supplier.physicalAddress ? supplier.physicalAddress : null, }); }; - await syncSequence(curSequenceNumber, 'suppliers', maxSequenceNumber); + await syncSequence(curSequenceNumber, 'supplier', maxSequenceNumber); timeFinish = new moment(); timeToGoSec = (timeFinish.diff(timeStart, 'seconds') * (maxSequenceNumber - curSequenceNumber) / 1000) timeToGoMin = Math.trunc(timeToGoSec / 60) @@ -443,24 +443,18 @@ export async function syncTradeItems(){ * it fetches all the supply lines for every tradeitem of the suppliers */ export async function syncSupplyLines(){ + const spinner = ora(`Syncing supply lines...`).start(); try { - let currentSequenceNumber = await models.sequenceNumber.findOne({ // TODO: Mirar como manejar este error - where: { - model: 'supplyLines' - } + const currentSequenceNumber = await models.sequenceNumber.findOne({ + where: { model: 'supplyLines' } }); - const spinner = ora(`Syncing supply lines...`).start(); - let suppliers = await models.supplier.findAll({ - where: { - isConnected: true - } + let connectedSuppliers = await models.supplier.findAll({ + where: { isConnected: true } }); let tradeItems = await models.tradeItem.findAll({ - where: { - supplierOrganizationId: suppliers.map(supplier => supplier.supplierOrganizationId) - } + where: { supplierOrganizationId: connectedSuppliers.map(supplier => supplier.supplierOrganizationId) } }); let promises = []; @@ -473,7 +467,8 @@ export async function syncSupplyLines(){ // Launch a promise for each supplier for (let tradeItem of tradeItems) { - let supplier = suppliers.find(supplier => supplier.supplierOrganizationId == tradeItem.supplierOrganizationId); + let supplier = connectedSuppliers.find(({ supplierOrganizationId }) => supplierOrganizationId === tradeItem.supplierOrganizationId); + // eslint-disable-next-line no-async-promise-executor let promise = new Promise(async (resolve) => { try { let url = `${env.API_URL}/supply-lines/sync/0` @@ -507,9 +502,7 @@ export async function syncSupplyLines(){ resolve([]); } }); - promises.push(promise); - } let supplyLines = await Promise.all(promises); @@ -589,20 +582,25 @@ export async function syncSupplyLines(){ isCustomerSpecific: line.isCustomerSpecific, tradeItemId: line.tradeItemId, }); - - + + for (let volumePrice of line.volumePrices) + await models.volumePrices.upsert({ + unit: volumePrice.unit, + pricePerPiece: volumePrice.pricePerPiece, + supplyLineId: line.supplyLineId, + }); } } } catch (err) { - spinner.fail(); throw err; } } spinner.succeed(); - console.log('Found', suppliers.length, 'connected suppliers'); + console.log('Found', connectedSuppliers.length, 'connected suppliers'); await syncSequence(currentSequenceNumber, 'supplyLines' ,maximumSequenceNumber); } catch (err) { + spinner.fail(); throw err; } }