refs #4823 Suppliers to supplier and volumePrices
This commit is contained in:
parent
7058ab837b
commit
4aac074a8b
|
@ -62,7 +62,7 @@ class Floriday {
|
||||||
// Continuar con todo lo que haga falta realizar en la rutina
|
// Continuar con todo lo que haga falta realizar en la rutina
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err;
|
utils.criticalError(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ import clientConfig from './conf/clientConfig.js';
|
||||||
import sequenceNumber from './conf/sequenceNumber.js';
|
import sequenceNumber from './conf/sequenceNumber.js';
|
||||||
|
|
||||||
// Supplier Models
|
// Supplier Models
|
||||||
import suppliers from './supplier/suppliers.js';
|
import supplier from './supplier/supplier.js';
|
||||||
import connections from './supplier/connections.js';
|
import connections from './supplier/connections.js';
|
||||||
|
|
||||||
// TradeItem Models
|
// TradeItem Models
|
||||||
|
@ -83,8 +83,8 @@ let models = {
|
||||||
clientConfig: clientConfig(sequelize),
|
clientConfig: clientConfig(sequelize),
|
||||||
botanicalName: botanicalNames(sequelize),
|
botanicalName: botanicalNames(sequelize),
|
||||||
supplyLine: supplyLine(sequelize),
|
supplyLine: supplyLine(sequelize),
|
||||||
volumePrice: volumePrices(sequelize),
|
volumePrices: volumePrices(sequelize),
|
||||||
supplier: suppliers(sequelize),
|
supplier: supplier(sequelize),
|
||||||
sequenceNumber: sequenceNumber(sequelize),
|
sequenceNumber: sequenceNumber(sequelize),
|
||||||
connection: connections(sequelize),
|
connection: connections(sequelize),
|
||||||
};
|
};
|
||||||
|
@ -147,8 +147,8 @@ try {
|
||||||
targetKey: 'tradeItemId',
|
targetKey: 'tradeItemId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.volumePrice.belongsTo(models.supplyLine, {
|
models.volumePrices.belongsTo(models.supplyLine, {
|
||||||
foreignKey: 'supplyLineFk',
|
foreignKey: 'supplyLineId',
|
||||||
as: 'supplyLine_Fk',
|
as: 'supplyLine_Fk',
|
||||||
targetKey: 'supplyLineId',
|
targetKey: 'supplyLineId',
|
||||||
});
|
});
|
||||||
|
@ -171,7 +171,7 @@ try {
|
||||||
targetKey: 'supplierOrganizationId',
|
targetKey: 'supplierOrganizationId',
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(err)
|
criticalError(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
let action, isForce;
|
let action, isForce;
|
||||||
|
|
|
@ -51,7 +51,7 @@ const suppliers = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (sequelize) => {
|
export default (sequelize) => {
|
||||||
const Suppliers = sequelize.define('suppliers', suppliers, {
|
const Suppliers = sequelize.define('supplier', suppliers, {
|
||||||
timestamps: false,
|
timestamps: false,
|
||||||
freezeTableName: true,
|
freezeTableName: true,
|
||||||
});
|
});
|
|
@ -1,16 +1,17 @@
|
||||||
import { Sequelize } from 'sequelize';
|
import { Sequelize } from 'sequelize';
|
||||||
|
|
||||||
const volumePrices = {
|
const volumePrices = {
|
||||||
id: {
|
supplyLineId: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.STRING,
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true,
|
|
||||||
},
|
},
|
||||||
unit: {
|
unit: {
|
||||||
type: Sequelize.STRING,
|
type: Sequelize.STRING,
|
||||||
|
primaryKey: true,
|
||||||
},
|
},
|
||||||
pricePerPiece: {
|
pricePerPiece: {
|
||||||
type: Sequelize.INTEGER,
|
type: Sequelize.DECIMAL(10,2),
|
||||||
|
primaryKey: true,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
42
utils.js
42
utils.js
|
@ -3,7 +3,7 @@ import fetch from 'node-fetch';
|
||||||
import { models } from './models/index.js';
|
import { models } from './models/index.js';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import ora from 'ora';
|
import ora, { spinners } from 'ora';
|
||||||
const env = process.env;
|
const env = process.env;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -218,7 +218,7 @@ export async function syncSequence(current = 0, model = null , maximumSequenceNu
|
||||||
try {
|
try {
|
||||||
const spinner = ora(`Syncing sequence...`).start();
|
const spinner = ora(`Syncing sequence...`).start();
|
||||||
let mockModels = [
|
let mockModels = [
|
||||||
'suppliers',
|
'supplier',
|
||||||
'tradeItems',
|
'tradeItems',
|
||||||
'supplyLines',
|
'supplyLines',
|
||||||
];
|
];
|
||||||
|
@ -322,7 +322,7 @@ export async function syncSuppliers(){
|
||||||
physicalAddress: supplier.physicalAddress ? supplier.physicalAddress : null,
|
physicalAddress: supplier.physicalAddress ? supplier.physicalAddress : null,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
await syncSequence(curSequenceNumber, 'suppliers', maxSequenceNumber);
|
await syncSequence(curSequenceNumber, 'supplier', maxSequenceNumber);
|
||||||
timeFinish = new moment();
|
timeFinish = new moment();
|
||||||
timeToGoSec = (timeFinish.diff(timeStart, 'seconds') * (maxSequenceNumber - curSequenceNumber) / 1000)
|
timeToGoSec = (timeFinish.diff(timeStart, 'seconds') * (maxSequenceNumber - curSequenceNumber) / 1000)
|
||||||
timeToGoMin = Math.trunc(timeToGoSec / 60)
|
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
|
* it fetches all the supply lines for every tradeitem of the suppliers
|
||||||
*/
|
*/
|
||||||
export async function syncSupplyLines(){
|
export async function syncSupplyLines(){
|
||||||
|
const spinner = ora(`Syncing supply lines...`).start();
|
||||||
try {
|
try {
|
||||||
let currentSequenceNumber = await models.sequenceNumber.findOne({ // TODO: Mirar como manejar este error
|
const currentSequenceNumber = await models.sequenceNumber.findOne({
|
||||||
where: {
|
where: { model: 'supplyLines' }
|
||||||
model: 'supplyLines'
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const spinner = ora(`Syncing supply lines...`).start();
|
let connectedSuppliers = await models.supplier.findAll({
|
||||||
let suppliers = await models.supplier.findAll({
|
where: { isConnected: true }
|
||||||
where: {
|
|
||||||
isConnected: true
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let tradeItems = await models.tradeItem.findAll({
|
let tradeItems = await models.tradeItem.findAll({
|
||||||
where: {
|
where: { supplierOrganizationId: connectedSuppliers.map(supplier => supplier.supplierOrganizationId) }
|
||||||
supplierOrganizationId: suppliers.map(supplier => supplier.supplierOrganizationId)
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let promises = [];
|
let promises = [];
|
||||||
|
@ -473,7 +467,8 @@ export async function syncSupplyLines(){
|
||||||
|
|
||||||
// Launch a promise for each supplier
|
// Launch a promise for each supplier
|
||||||
for (let tradeItem of tradeItems) {
|
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) => {
|
let promise = new Promise(async (resolve) => {
|
||||||
try {
|
try {
|
||||||
let url = `${env.API_URL}/supply-lines/sync/0`
|
let url = `${env.API_URL}/supply-lines/sync/0`
|
||||||
|
@ -507,9 +502,7 @@ export async function syncSupplyLines(){
|
||||||
resolve([]);
|
resolve([]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
promises.push(promise);
|
promises.push(promise);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let supplyLines = await Promise.all(promises);
|
let supplyLines = await Promise.all(promises);
|
||||||
|
@ -589,20 +582,25 @@ export async function syncSupplyLines(){
|
||||||
isCustomerSpecific: line.isCustomerSpecific,
|
isCustomerSpecific: line.isCustomerSpecific,
|
||||||
tradeItemId: line.tradeItemId,
|
tradeItemId: line.tradeItemId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
for (let volumePrice of line.volumePrices)
|
||||||
|
await models.volumePrices.upsert({
|
||||||
|
unit: volumePrice.unit,
|
||||||
|
pricePerPiece: volumePrice.pricePerPiece,
|
||||||
|
supplyLineId: line.supplyLineId,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
spinner.fail();
|
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
console.log('Found', suppliers.length, 'connected suppliers');
|
console.log('Found', connectedSuppliers.length, 'connected suppliers');
|
||||||
|
|
||||||
await syncSequence(currentSequenceNumber, 'supplyLines' ,maximumSequenceNumber);
|
await syncSequence(currentSequenceNumber, 'supplyLines' ,maximumSequenceNumber);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
spinner.fail();
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue