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