refs #4823 Many changes

This commit is contained in:
Guillermo Bonet 2023-05-09 11:59:21 +02:00
parent a7bc5a807c
commit 7058ab837b
6 changed files with 143 additions and 125 deletions

View File

@ -4,7 +4,7 @@ import moment from 'moment';
import chalk from 'chalk';
// Añade la hora a todos los console.log
// console.log = (...args) => console.info(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`, ...args);
console.log = (...args) => console.info(chalk.gray(`[${new moment().format('YYYY-MM-DD hh:mm:ss A')}]`), ...args);
const env = process.env;
class Floriday {
async start() {
@ -18,7 +18,7 @@ class Floriday {
} catch (err) {
utils.criticalError(err);
}
await this.trunk();
await this.schedule();
}
async tryConn() {
@ -37,14 +37,16 @@ class Floriday {
const intervalTime = JSON.parse(env.IS_PRODUCTION)
? env.MS_PRODUCTION_SCHEDULE
: env.MS_TEST_SCHEDULE;
setInterval(async () => {
while (true) {
try {
await this.trunk();
}
catch (err) {
await new Promise(resolve => setTimeout(resolve, intervalTime));
} catch (err) {
await this.tryConn();
await new Promise(resolve => setTimeout(resolve, intervalTime));
}
}, intervalTime);
}
} catch (err) {
throw new Error(err);
}
@ -60,7 +62,7 @@ class Floriday {
// Continuar con todo lo que haga falta realizar en la rutina
} catch (err) {
throw new Error(err);
throw err;
}
}

View File

@ -99,25 +99,25 @@ try {
/* ------------------------------ */
models.characteristic.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.seasonalPeriod.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.photo.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.packingConfiguration.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
@ -136,13 +136,13 @@ try {
models.botanicalName.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
@ -154,7 +154,7 @@ try {
});
models.supplyLine.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
@ -162,7 +162,13 @@ try {
models.tradeItem.belongsTo(models.supplier, {
foreignKey: 'supplierOrganizationId',
as: 'supplierOrganization_Id',
targetKey: 'organizationId',
targetKey: 'supplierOrganizationId',
});
models.supplyLine.belongsTo(models.supplier, {
foreignKey: 'supplierOrganizationId',
as: 'supplierOrganization_Id',
targetKey: 'supplierOrganizationId',
});
} catch (err) {
throw new Error(err)

View File

@ -1,7 +1,7 @@
import { Sequelize } from 'sequelize';
const connections = {
organizationId: {
supplierOrganizationId: {
type: Sequelize.STRING,
primaryKey: true,
allowNull: false,

View File

@ -1,13 +1,13 @@
import { Sequelize } from 'sequelize';
const suppliers = {
sequenceNumber: {
type: Sequelize.INTEGER,
supplierOrganizationId: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
},
organizationId: {
type: Sequelize.STRING,
sequenceNumber: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
},

View File

@ -8,22 +8,28 @@ const supplyLine = {
status: {
type: Sequelize.STRING,
},
supplierOrganizationId: {
pricePerPiece_currency: {
type: Sequelize.STRING,
},
pricePerPiece: {
type: Sequelize.JSON,
pricePerPiece_value: {
type: Sequelize.DECIMAL(10,2),
},
numberOfPieces : {
type: Sequelize.INTEGER,
},
deliveryPeriod: {
type: Sequelize.JSON,
deliveryPeriod_startDateTime: {
type: Sequelize.DATE,
},
orderPeriod: {
type: Sequelize.JSON,
deliveryPeriod_endDateTime: {
type: Sequelize.DATE,
},
wharehouseId: {
orderPeriod_startDateTime: {
type: Sequelize.DATE,
},
orderPeriod_endDateTime: {
type: Sequelize.DATE,
},
warehouseId: {
type: Sequelize.STRING,
},
sequenceNumber: {
@ -38,10 +44,10 @@ const supplyLine = {
salesUnit: {
type: Sequelize.STRING,
},
agreementReferenceCode: {
agreementReference_code: {
type: Sequelize.STRING,
},
agreementReferenceDescription: {
agreementReference_description: {
type: Sequelize.STRING,
},
isLimited: {

194
utils.js
View File

@ -307,14 +307,14 @@ export async function syncSuppliers(){
if (timeFinish)
spinner.text = spinner.text + ` (${timeLeft})`
await models.supplier.upsert({
supplierOrganizationId: supplier.organizationId,
sequenceNumber: supplier.sequenceNumber,
companyGln: supplier.companyGln,
companyGln: supplier.companyGln ? supplier.companyGln : null,
name: supplier.name ? supplier.name : null,
commercialName: supplier.commercialName ? supplier.commercialName : null,
email: supplier.email ? supplier.email : null,
phone: supplier.phone ? supplier.phone : null,
website: supplier.website ? supplier.website : null,
organizationId: supplier.organizationId ? supplier.organizationId : null,
rfhRelationId: supplier.rfhRelationId ? supplier.rfhRelationId : null,
paymentProviders: supplier.paymentProviders.length ? `${supplier.paymentProviders}` : null,
endDate: supplier.endDate ? supplier.endDate : null,
@ -363,34 +363,34 @@ export async function syncConn(){
spinner.text = `Syncing ${i++} connections...`
if (connection.isConnected == false)
continue;
let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.organizationId);
let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.supplierOrganizationId);
if (remoteConnection == undefined){
console.log('Connection: ', connection, 'does not exist in the remote server');
console.log('Creating remote connection');
await fetch(`${env.API_URL}/connections/${connection.organizationId}`, {
await fetch(`${env.API_URL}/connections/${connection.supplierOrganizationId}`, {
method: 'PUT',
headers
});
await models.connection.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
supplierOrganizationId: connection.supplierOrganizationId
}
});
await models.supplier.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
supplierOrganizationId: connection.supplierOrganizationId
}
});
} else {
await models.connection.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
supplierOrganizationId: connection.supplierOrganizationId
}
});
await models.supplier.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
supplierOrganizationId: connection.supplierOrganizationId
}
});
}
@ -409,7 +409,7 @@ export async function syncTradeItems(){
for (let supplier of suppliers) {
if (!supplier.isConnected) continue;
let query = `${env.API_URL}/trade-items?supplierOrganizationId=${supplier.organizationId}`;
let query = `${env.API_URL}/trade-items?supplierOrganizationId=${supplier.supplierOrganizationId}`;
let headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${await getCurrentToken()}`,
@ -439,8 +439,8 @@ export async function syncTradeItems(){
}
/**
* Syncs the supply lines for suppliers that are connected
* to do this, it fetches all the supply lines for every tradeitem of the suppliers
* Syncs the supply lines for suppliers that are connected to do this,
* it fetches all the supply lines for every tradeitem of the suppliers
*/
export async function syncSupplyLines(){
try {
@ -459,7 +459,7 @@ export async function syncSupplyLines(){
let tradeItems = await models.tradeItem.findAll({
where: {
supplierOrganizationId: suppliers.map(supplier => supplier.organizationId)
supplierOrganizationId: suppliers.map(supplier => supplier.supplierOrganizationId)
}
});
@ -472,14 +472,13 @@ export async function syncSupplyLines(){
};
// Launch a promise for each supplier
for (let tradeItem of tradeItems) {
let supplier = suppliers.find(supplier => supplier.organizationId == tradeItem.supplierOrganizationId);
// eslint-disable-next-line no-async-promise-executor
for (let tradeItem of tradeItems) {
let supplier = suppliers.find(supplier => supplier.supplierOrganizationId == tradeItem.supplierOrganizationId);
let promise = new Promise(async (resolve) => {
try {
let url = `${env.API_URL}/supply-lines/sync/0`
const params = new URLSearchParams({
supplierOrganizationId: supplier.organizationId,
supplierOrganizationId: supplier.supplierOrganizationId,
tradeItemId: tradeItem.tradeItemId,
postFilterSelectedTradeItems: false
});
@ -488,19 +487,23 @@ export async function syncSupplyLines(){
headers
});
if (request.status == 429) { // Too many request
resolve([]);
return;
}
let supplyLines = await request.json();
if (supplyLines.length == 0) {
console.log('No supply lines for supplier: ', supplier.commercialName, ' - ' , tradeItem.name);
if (!supplyLines.results.length) {
resolve([]);
return;
}
resolve(supplyLines);
} catch (error) {
} catch (err) {
console.log('Error while syncing supply lines for: ', supplier.commercialName, ' - ' , tradeItem.name);
console.log(error);
console.log(err);
resolve([]);
}
});
@ -511,95 +514,96 @@ export async function syncSupplyLines(){
let supplyLines = await Promise.all(promises);
let maximumSequenceNumber;
for (let supplyLine of supplyLines) {
maximumSequenceNumber = supplyLine.maximumSequenceNumber;
supplyLine = supplyLine.results;
try {
for (let line of supplyLine) {
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 urlTradeItem = `${env.API_URL}/trade-items?tradeItemIds=${line.tradeItemId}`;
let queryTradeItem = await fetch(urlTradeItem, {
method: 'GET',
headers
});
let tradeItem = await queryTradeItem.json();
if (tradeItem.length == 0) {
console.log('Trade item not found for supply line: ', line.supplyLineId);
console.log('Trade item id: ', line.tradeItemId);
continue;
}
let supplier = await models.supplier.findOne({
where: {
organizationId: tradeItem[0].supplierOrganizationId
}
});
await insertItem(tradeItem[0], supplier);
tradeItem = await models.tradeItem.findOne({
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('Trade item id: ', line.tradeItemId);
continue;
console.log('Requesting data for trade item id: ', line.tradeItemId);
let queryTradeItem = await fetch(`${env.API_URL}/trade-items?tradeItemIds=${line.tradeItemId}`, {
method: 'GET',
headers
});
let tradeItem = await queryTradeItem.json();
if (tradeItem.length == 0) {
console.log('Trade item not found for supply line: ', line.supplyLineId);
console.log('Trade item id: ', line.tradeItemId);
continue;
}
let supplier = await models.supplier.findOne({
where: {
supplierOrganizationId: tradeItem[0].supplierOrganizationId
}
});
await insertItem(tradeItem[0], supplier);
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({
supplyLineId: line.supplyLineId,
status: line.status,
supplierOrganizationId: line.supplierOrganizationId,
pricePerPiece_currency: line.pricePerPiece.currency,
pricePerPiece_value: line.pricePerPiece.value,
numberOfPieces: line.numberOfPieces,
deliveryPeriod_startDateTime: line.deliveryPeriod.startDateTime,
deliveryPeriod_endDateTime: line.deliveryPeriod.endDateTime,
orderPeriod_startDateTime: line.orderPeriod.startDateTime,
orderPeriod_endDateTime: line.orderPeriod.endDateTime,
warehouseId: line.warehouseId,
sequenceNumber: line.sequenceNumber,
type: line.type,
isDeleted: line.isDeleted,
salesUnit: line.salesUnit,
agreementReference_code: line.agreementReference.code ? line.agreementReference.code : null,
agreementReference_description: line.agreementReference.description ? line.agreementReference.description : null,
isLimited: line.isLimited,
isCustomerSpecific: line.isCustomerSpecific,
tradeItemId: line.tradeItemId,
});
}
await models.supplyLine.upsert({
supplyLineId: line.supplyLineId,
status: line.status,
supplierOrganizationId: line.supplierOrganizationId,
pricePerPiece: line.pricePerPiece,
numberOfPieces: line.numberOfPieces,
deliveryPeriod: line.deliveryPeriod,
orderPeriod: line.orderPeriod,
wharehouseId: line.wharehouseId,
sequenceNumber: line.sequenceNumber,
type: line.type,
isDeleted: line.isDeleted,
salesUnit: line.salesUnit,
agreementReferenceCode: line.agreementReference.code,
agreementReferenceDescription: line.agreementReference.description,
isLimited: line.isLimited,
isCustomerSpecific: line.isCustomerSpecific,
tradeItemFk: line.tradeItemId,
});
}
} catch (err) {
spinner.fail();
console.log('Error while syncing supply lines - ' + supplyLine.results[0].tradeItemId);
throw new Error(err);
throw err;
}
}
spinner.succeed();
console.log('Found', suppliers.length, 'connected suppliers');
await syncSequence(currentSequenceNumber,'supplyLines' ,maximumSequenceNumber);
await syncSequence(currentSequenceNumber, 'supplyLines' ,maximumSequenceNumber);
} catch (err) {
throw new Error(err);
throw err;
}
}
@ -609,7 +613,7 @@ export async function insertItem(tradeItem, supplier) {
let currentSupp = await models.supplier.findOne({
where: {
organizationId: tradeItem.supplierOrganizationId
supplierOrganizationId: tradeItem.supplierOrganizationId
}
});
@ -619,7 +623,7 @@ export async function insertItem(tradeItem, supplier) {
await currentSupp.save({transaction: tx});
await models.connection.upsert({
organizationId: tradeItem.supplierOrganizationId,
supplierOrganizationId: tradeItem.supplierOrganizationId,
connect: true,
}, {transaction: tx});
@ -646,7 +650,7 @@ export async function insertItem(tradeItem, supplier) {
await models.characteristic.upsert({
vbnCode: characteristic.vbnCode,
vbnValueCode: characteristic.vbnValueCode,
tradeItemFk: tradeItem.tradeItemId,
tradeItemId: tradeItem.tradeItemId,
}, {transaction: tx});
}
@ -656,7 +660,7 @@ export async function insertItem(tradeItem, supplier) {
await models.seasonalPeriod.upsert({
startWeek: seasonalPeriod.startWeek,
endWeek: seasonalPeriod.endWeek,
tradeItemFk: tradeItem.tradeItemId,
tradeItemId: tradeItem.tradeItemId,
}, {transaction: tx});
}
@ -668,7 +672,7 @@ export async function insertItem(tradeItem, supplier) {
url: photo.url,
type: photo.type,
primary: photo.primary,
tradeItemFk: tradeItem.tradeItemId,
tradeItemId: tradeItem.tradeItemId,
}, {transaction: tx});
let packingConfigurations = tradeItem.packingConfigurations;
@ -688,7 +692,7 @@ export async function insertItem(tradeItem, supplier) {
transportHeightInCm: packingConfiguration.transportHeightInCm,
loadCarrierType: packingConfiguration.loadCarrierType,
isPrimary: packingConfiguration.isPrimary,
tradeItemFk: tradeItem.tradeItemId,
tradeItemId: tradeItem.tradeItemId,
}, {transaction: tx});
await models.package.upsert({
@ -705,7 +709,7 @@ export async function insertItem(tradeItem, supplier) {
for (let countryOfOriginIsoCode of countryOfOriginIsoCodes) {
await models.countryOfOriginIsoCode.upsert({
isoCode: countryOfOriginIsoCode,
tradeItemFk: tradeItem.tradeItemId,
tradeItemId: tradeItem.tradeItemId,
}, {transaction: tx});
}
@ -714,7 +718,7 @@ export async function insertItem(tradeItem, supplier) {
for (let botanicalName of botanicalNames) {
await models.botanicalName.upsert({
name: botanicalName.name,
tradeItemFk: tradeItem.tradeItemId,
tradeItemId: tradeItem.tradeItemId,
}, {transaction: tx});
}