refs #4823 Bugs solved

This commit is contained in:
Guillermo Bonet 2023-05-08 13:02:31 +02:00
parent 17533d873f
commit 168f97275b
2 changed files with 127 additions and 123 deletions

View File

@ -9,24 +9,24 @@ const env = process.env;
console.clear()
console.log(chalk.hex('#06c581')(
`
`
`
`
))
let sequelize, conSpinner
try {
conSpinner = ora('Creating connection...').start();
sequelize = createConn();
await checkConn();
conSpinner.succeed();
conSpinner = ora('Creating connection...').start();
sequelize = createConn();
await checkConn();
conSpinner.succeed();
} catch (err) {
conSpinner.fail();
criticalError(err);
conSpinner.fail();
criticalError(err);
}
// Supply Line Models
@ -72,119 +72,119 @@ import characteristics from './tradeItem/characteristics.js';
* @type {Object.<string, Sequelize.Model>}
*/
let models = {
sequelize: sequelize,
tradeItem: tradeItem(sequelize),
packingConfiguration: packingConfigurations(sequelize),
photo: photos(sequelize),
characteristic: characteristics(sequelize),
countryOfOriginIsoCode: countryOfOriginIsoCodes(sequelize),
package: packageModel(sequelize),
seasonalPeriod: seasonalPeriod(sequelize),
clientConfig: clientConfig(sequelize),
botanicalName: botanicalNames(sequelize),
supplyLine: supplyLine(sequelize),
volumePrice: volumePrices(sequelize),
supplier: suppliers(sequelize),
sequenceNumber: sequenceNumber(sequelize),
connection: connections(sequelize),
sequelize: sequelize,
tradeItem: tradeItem(sequelize),
packingConfiguration: packingConfigurations(sequelize),
photo: photos(sequelize),
characteristic: characteristics(sequelize),
countryOfOriginIsoCode: countryOfOriginIsoCodes(sequelize),
package: packageModel(sequelize),
seasonalPeriod: seasonalPeriod(sequelize),
clientConfig: clientConfig(sequelize),
botanicalName: botanicalNames(sequelize),
supplyLine: supplyLine(sequelize),
volumePrice: volumePrices(sequelize),
supplier: suppliers(sequelize),
sequenceNumber: sequenceNumber(sequelize),
connection: connections(sequelize),
};
try {
/* Remove ID atribute from models */
models.characteristic.removeAttribute('id');
models.seasonalPeriod.removeAttribute('id');
models.package.removeAttribute('id');
models.botanicalName.removeAttribute('id');
models.countryOfOriginIsoCode.removeAttribute('id');
/* ------------------------------ */
/* Remove ID atribute from models */
models.characteristic.removeAttribute('id');
models.seasonalPeriod.removeAttribute('id');
models.package.removeAttribute('id');
models.botanicalName.removeAttribute('id');
models.countryOfOriginIsoCode.removeAttribute('id');
/* ------------------------------ */
models.characteristic.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.characteristic.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.seasonalPeriod.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.seasonalPeriod.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.photo.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.photo.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.packingConfiguration.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.packingConfiguration.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.packingConfiguration.hasMany(models.package, {
foreignKey: 'packingConfigurationFk',
as: 'package_Fk',
targetKey: 'packingConfigurationId',
});
models.packingConfiguration.hasMany(models.package, {
foreignKey: 'packingConfigurationFk',
as: 'package_Fk',
targetKey: 'packingConfigurationId',
});
models.package.belongsTo(models.packingConfiguration, {
foreignKey: 'packingConfigurationFk',
as: 'packingConfiguration_Fk',
targetKey: 'packingConfigurationId',
});
models.package.belongsTo(models.packingConfiguration, {
foreignKey: 'packingConfigurationFk',
as: 'packingConfiguration_Fk',
targetKey: 'packingConfigurationId',
});
models.botanicalName.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.botanicalName.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.volumePrice.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineFk',
as: 'supplyLine_Fk',
targetKey: 'supplyLineId',
});
models.volumePrice.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineFk',
as: 'supplyLine_Fk',
targetKey: 'supplyLineId',
});
models.supplyLine.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.supplyLine.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.tradeItem.belongsTo(models.supplier, {
foreignKey: 'supplierOrganizationId',
as: 'supplierOrganization_Id',
targetKey: 'organizationId',
});
models.tradeItem.belongsTo(models.supplier, {
foreignKey: 'supplierOrganizationId',
as: 'supplierOrganization_Id',
targetKey: 'organizationId',
});
} catch (err) {
throw new Error(err)
throw new Error(err)
}
let action, isForce;
if (JSON.parse(process.env.FORCE_SYNC)) {
action = 'Forcing'
isForce = true
action = 'Forcing'
isForce = true
} else {
action = 'Altering'
isForce = false
action = 'Altering'
isForce = false
}
const modSpinner = ora(`${action} models...`).start();
try {
await sequelize.sync({ force: isForce });
modSpinner.succeed();
await sequelize.sync({ force: isForce });
modSpinner.succeed();
}
catch (err) {
modSpinner.fail();
criticalError(err);
modSpinner.fail();
criticalError(err);
}
/**
@ -193,41 +193,41 @@ catch (err) {
* @returns {Sequelize} Sequelize instance with the connection to the database.
*/
function createConn() {
return new Sequelize(process.env.DB_SCHEMA, process.env.DB_USER, process.env.DB_PWD, {
host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT,
logging: false,
pool: {
max: parseInt(process.env.DB_MAX_CONN_POOL),
acquire: 60000,
idle: 10000,
},
});
return new Sequelize(process.env.DB_SCHEMA, process.env.DB_USER, process.env.DB_PWD, {
host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT,
logging: false,
pool: {
max: parseInt(process.env.DB_MAX_CONN_POOL),
acquire: 60000,
idle: 10000,
},
});
}
/**
* Check if connection is ok
*/
async function checkConn() {
try {
await sequelize.authenticate();
} catch (err) {
throw new Error (err);
}
try {
await sequelize.authenticate();
} catch (err) {
throw new Error (err);
}
}
/**
* Close connection
*/
async function closeConn() {
const spinner = ora('Stopping connection...').start();
try {
await sequelize.close()
spinner.succeed();
} catch (err) {
spinner.fail();
criticalError(err)
}
const spinner = ora('Stopping connection...').start();
try {
await sequelize.close()
spinner.succeed();
} catch (err) {
spinner.fail();
criticalError(err)
}
}
export { models, checkConn, closeConn};

View File

@ -109,6 +109,10 @@ export async function checkConfig() {
throw new Error(`You haven't provided the ${reqEnvVar} environment variable`);
}
}
const clientConfigData = await models.clientConfig.findOne();
if (!clientConfigData)
await updateClientConfig(env.CLIENT_ID, env.CLIENT_SECRET);
spinner.succeed();
}
@ -452,7 +456,7 @@ export async function syncSupplyLines(){
}
});
const spinner = ora(`Syncing trade items...`).start();
const spinner = ora(`Syncing supply lines...`).start();
let suppliers = await models.supplier.findAll({
where: {
isConnected: true