refs #4823 Suppliers to supplier and volumePrices

This commit is contained in:
Guillermo Bonet 2023-05-09 13:14:54 +02:00
parent 7058ab837b
commit 4aac074a8b
5 changed files with 33 additions and 34 deletions

View File

@ -62,7 +62,7 @@ class Floriday {
// Continuar con todo lo que haga falta realizar en la rutina
} catch (err) {
throw err;
utils.criticalError(err);
}
}

View File

@ -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;

View File

@ -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,
});

View File

@ -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,
},
};

View File

@ -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;
}
}