refs #4823 Changes in structure
This commit is contained in:
parent
69227f3853
commit
5a754967ba
|
@ -39,6 +39,8 @@ DB_USER = root
|
||||||
DB_PWD = root
|
DB_PWD = root
|
||||||
DB_HOST = localhost
|
DB_HOST = localhost
|
||||||
DB_DIALECT = mariadb
|
DB_DIALECT = mariadb
|
||||||
|
DB_TIMEOUT_RECONECT = 30000
|
||||||
|
DB_MAX_CONN_POOL = 40
|
||||||
|
|
||||||
#GENERAL CONFIG
|
#GENERAL CONFIG
|
||||||
IS_PRODUCTION = false
|
IS_PRODUCTION = false
|
||||||
|
@ -46,6 +48,7 @@ SECRETS = true
|
||||||
FORCE_SYNC = true
|
FORCE_SYNC = true
|
||||||
SYNC_SEQUENCE = true
|
SYNC_SEQUENCE = true
|
||||||
SYNC_SUPPLIER = true
|
SYNC_SUPPLIER = true
|
||||||
|
SYNC_CONN = true
|
||||||
SYNC_TRADEITEM = true
|
SYNC_TRADEITEM = true
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
61
floriday.js
61
floriday.js
|
@ -1,6 +1,7 @@
|
||||||
import * as vnUtils from './utils.js';
|
import * as utils from './utils.js';
|
||||||
import { models } from './models/index.js';
|
import { models, checkConn, closeConn } from './models/index.js';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
import chalk from 'chalk';
|
||||||
|
|
||||||
// Añade la hora a todos los console.log
|
// Añade la hora a todos los console.log
|
||||||
// console.log = (...args) => console.info(`${new moment().format('HH:mm:ss')} -`, ...args);
|
// console.log = (...args) => console.info(`${new moment().format('HH:mm:ss')} -`, ...args);
|
||||||
|
@ -8,27 +9,63 @@ const env = process.env;
|
||||||
class Floriday {
|
class Floriday {
|
||||||
async start() {
|
async start() {
|
||||||
try {
|
try {
|
||||||
this.tokenExpirationDate = await vnUtils.getClientToken(models);
|
this.tokenExpirationDate = await utils.requestToken(models);
|
||||||
if (JSON.parse(env.SYNC_SEQUENCE)) await vnUtils.syncSequence()
|
if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence()
|
||||||
if (JSON.parse(env.SYNC_SUPPLIER)) await vnUtils.syncSuppliers();
|
if (JSON.parse(env.SYNC_SUPPLIER)) await utils.syncSuppliers();
|
||||||
await vnUtils.syncConnections();
|
if (JSON.parse(env.SYNC_CONN)) await utils.syncConn();
|
||||||
if (JSON.parse(env.SYNC_TRADEITEM)) await vnUtils.syncTradeItems();
|
if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
vnUtils.criticalError(err);
|
utils.criticalError(err);
|
||||||
|
}
|
||||||
|
await this.troncal()
|
||||||
|
}
|
||||||
|
|
||||||
|
async tryConn() {
|
||||||
|
try {
|
||||||
|
utils.sleep(env.DB_TIMEOUT_RECONECT);
|
||||||
|
await checkConn();
|
||||||
|
await this.schedule();
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async schedule () {
|
async schedule () {
|
||||||
try {
|
try {
|
||||||
if (moment().isAfter(await vnUtils.getCurrentTokenExpiration())) {
|
const intervalTime = JSON.parse(env.IS_PRODUCTION) ? 300000 : 5000;
|
||||||
this.tokenExpirationDate = await vnUtils.getClientToken(models);
|
setInterval(async () => {
|
||||||
|
try {
|
||||||
|
await this.troncal();
|
||||||
}
|
}
|
||||||
await vnUtils.syncSupplyLines();
|
catch (err) {
|
||||||
|
await this.tryConn();
|
||||||
|
}
|
||||||
|
}, intervalTime);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async troncal() {
|
||||||
|
try{
|
||||||
|
if (moment().isAfter(await utils.getCurrentTokenExpiration())) {
|
||||||
|
this.tokenExpirationDate = await utils.requestToken(models);
|
||||||
|
}
|
||||||
|
await utils.syncSupplyLines();
|
||||||
|
|
||||||
|
// Continuar con todo lo que haga falta realizar en el evento
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw(err);
|
throw new Error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async stop() {
|
||||||
|
await closeConn();
|
||||||
|
console.log(chalk.dim('Bye, come back soon 👋'))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Floriday;
|
export default Floriday;
|
14
main.js
14
main.js
|
@ -1,20 +1,18 @@
|
||||||
import Floriday from './floriday.js';
|
import Floriday from './floriday.js';
|
||||||
const env = process.env;
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const floriday = new Floriday();
|
const floriday = new Floriday();
|
||||||
|
|
||||||
await floriday.start();
|
await floriday.start();
|
||||||
|
|
||||||
process.on('SIGINT', async function() {
|
process.on('SIGINT', async () => {
|
||||||
console.log('\nBye <3')
|
await floriday.stop();
|
||||||
process.exit();
|
process.exit();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
await floriday.schedule()
|
await floriday.schedule()
|
||||||
const intervalTime = JSON.parse(env.IS_PRODUCTION) ? 300000 : 5000;
|
} catch (err) {
|
||||||
setInterval(async () => {
|
await floriday.tryConn();
|
||||||
await floriday.schedule();
|
}
|
||||||
}, intervalTime);
|
|
||||||
}
|
}
|
||||||
main();
|
main();
|
|
@ -16,10 +16,11 @@ console.log(chalk.hex('#06c581')(
|
||||||
`
|
`
|
||||||
))
|
))
|
||||||
|
|
||||||
let sequelize = createConnection();
|
let sequelize, conSpinner
|
||||||
const conSpinner = ora('Creating connection...').start();
|
|
||||||
try {
|
try {
|
||||||
await sequelize.authenticate();
|
conSpinner = ora('Creating connection...').start();
|
||||||
|
sequelize = createConn();
|
||||||
|
await checkConn();
|
||||||
conSpinner.succeed();
|
conSpinner.succeed();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
conSpinner.fail();
|
conSpinner.fail();
|
||||||
|
@ -86,6 +87,7 @@ let models = {
|
||||||
connection: connections(sequelize),
|
connection: connections(sequelize),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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');
|
||||||
|
@ -160,7 +162,9 @@ models.tradeItem.belongsTo(models.supplier, {
|
||||||
as: 'supplierOrganization_Id',
|
as: 'supplierOrganization_Id',
|
||||||
targetKey: 'organizationId',
|
targetKey: 'organizationId',
|
||||||
});
|
});
|
||||||
|
} catch (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)) {
|
||||||
|
@ -195,23 +199,44 @@ catch (err) {
|
||||||
/**
|
/**
|
||||||
* Creates the connection to the database.
|
* Creates the connection to the database.
|
||||||
*
|
*
|
||||||
* @example
|
|
||||||
* let sequelize = createConnection();
|
|
||||||
*
|
|
||||||
* @returns {Sequelize} Sequelize instance with the connection to the database.
|
* @returns {Sequelize} Sequelize instance with the connection to the database.
|
||||||
*/
|
*/
|
||||||
function createConnection() {
|
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: 40,
|
max: parseInt(process.env.DB_MAX_CONN_POOL),
|
||||||
min: 0,
|
|
||||||
acquire: 60000,
|
acquire: 60000,
|
||||||
idle: 10000,
|
idle: 10000,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export { models } ;
|
/**
|
||||||
|
* Check if connection is ok
|
||||||
|
*/
|
||||||
|
async function checkConn() {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { models, checkConn, closeConn};
|
90
utils.js
90
utils.js
|
@ -17,7 +17,7 @@ const url = 'https://api.staging.floriday.io/customers-api/2022v2';
|
||||||
* @param {sequelize.models} models
|
* @param {sequelize.models} models
|
||||||
* @returns {Date} tokenExpirationDate formated as YYYY-MM-DD HH:mm:ss
|
* @returns {Date} tokenExpirationDate formated as YYYY-MM-DD HH:mm:ss
|
||||||
*/
|
*/
|
||||||
export async function getClientToken() {
|
export async function requestToken() {
|
||||||
const clientConfigData = await models.clientConfig.findOne();
|
const clientConfigData = await models.clientConfig.findOne();
|
||||||
|
|
||||||
if (!clientConfigData)
|
if (!clientConfigData)
|
||||||
|
@ -41,7 +41,7 @@ export async function getClientToken() {
|
||||||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`)
|
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`)
|
||||||
.join('&');
|
.join('&');
|
||||||
|
|
||||||
const tokenRequest = await fetch(tokenEndpoint, {
|
const response = await fetch(tokenEndpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
@ -49,26 +49,22 @@ export async function getClientToken() {
|
||||||
body,
|
body,
|
||||||
});
|
});
|
||||||
|
|
||||||
const tokenResponse = await tokenRequest.json();
|
const responseData = await response.json();
|
||||||
|
|
||||||
if (tokenRequest.ok) {
|
if (response.ok) {
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
} else {
|
} else {
|
||||||
spinner.fail();
|
spinner.fail();
|
||||||
criticalError(new Error(`Token request failed with status: ${tokenRequest.status} - ${tokenRequest.statusText}`));
|
criticalError(new Error(`Token request failed with status: ${response.status} - ${response.statusText}`));
|
||||||
}
|
}
|
||||||
|
let tokenExpirationDate = moment()
|
||||||
const accessToken = tokenResponse.access_token;
|
.add(responseData.expires_in, 's')
|
||||||
let now = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
||||||
|
|
||||||
let tokenExpirationDate = moment(now)
|
|
||||||
.add(tokenResponse.expires_in, 's')
|
|
||||||
.format('YYYY-MM-DD HH:mm:ss');
|
.format('YYYY-MM-DD HH:mm:ss');
|
||||||
|
|
||||||
await updateClientConfig(
|
await updateClientConfig(
|
||||||
clientId,
|
clientId,
|
||||||
clientSecret,
|
clientSecret,
|
||||||
accessToken,
|
responseData.access_token,
|
||||||
tokenExpirationDate
|
tokenExpirationDate
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -80,15 +76,24 @@ export async function getClientToken() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current token
|
||||||
|
*
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
export async function getCurrentToken() {
|
export async function getCurrentToken() {
|
||||||
let data = await models.clientConfig.findOne().currentToken;
|
let data = await models.clientConfig.findOne();
|
||||||
return data.currentToken;
|
return data.currentToken
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the expiration data of current token
|
||||||
|
*
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
export async function getCurrentTokenExpiration() {
|
export async function getCurrentTokenExpiration() {
|
||||||
let data = await models.clientConfig.findOne();
|
let data = await models.clientConfig.findOne();
|
||||||
return data.tokenExpiration;
|
return data.tokenExpiration
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -119,16 +124,6 @@ export async function updateClientConfig(clientId, clientSecret, accessToken, to
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the current Access Token
|
|
||||||
*
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
export async function getJWT() {
|
|
||||||
const clientConfigData = await models.clientConfig.findAll();
|
|
||||||
return clientConfigData[0].currentToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* pauses the execution of the script for the given amount of milliseconds
|
* pauses the execution of the script for the given amount of milliseconds
|
||||||
*
|
*
|
||||||
|
@ -203,7 +198,7 @@ export async function syncSequence(current = 0, model = null , maximumSequenceNu
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
spinner.fail();
|
spinner.fail();
|
||||||
throw(err);
|
throw new Error(err);
|
||||||
}
|
}
|
||||||
} else if (current) {
|
} else if (current) {
|
||||||
|
|
||||||
|
@ -248,7 +243,7 @@ export async function syncSuppliers(){
|
||||||
try {
|
try {
|
||||||
let headers = {
|
let headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': `Bearer ${await getJWT()}`,
|
'Authorization': `Bearer ${await getCurrentToken()}`,
|
||||||
'X-Api-Key': process.env.API_KEY
|
'X-Api-Key': process.env.API_KEY
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -256,6 +251,12 @@ export async function syncSuppliers(){
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers
|
headers
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
spinner.fail();
|
||||||
|
criticalError(new Error(`Max sequence request failed with status: ${response.status} - ${response.statusText}`));
|
||||||
|
}
|
||||||
|
|
||||||
const maxSequenceNumber = await response.json();
|
const maxSequenceNumber = await response.json();
|
||||||
let timeFinish, timeToGoSec, timeToGoMin, timeLeft;
|
let timeFinish, timeToGoSec, timeToGoMin, timeLeft;
|
||||||
for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
|
for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
|
||||||
|
@ -305,16 +306,18 @@ export async function syncSuppliers(){
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
spinner.fail();
|
spinner.fail();
|
||||||
throw(err);
|
throw new Error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function syncConnections(){
|
export async function syncConn(){
|
||||||
|
const spinner = ora(`Syncing connections...`).start();
|
||||||
|
try {
|
||||||
let connections = await models.connection.findAll();
|
let connections = await models.connection.findAll();
|
||||||
|
|
||||||
let headers = {
|
let headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': `Bearer ${await getJWT()}`,
|
'Authorization': `Bearer ${await getCurrentToken()}`,
|
||||||
'X-Api-Key': process.env.API_KEY
|
'X-Api-Key': process.env.API_KEY
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -325,7 +328,9 @@ export async function syncConnections(){
|
||||||
|
|
||||||
remoteConnections = await remoteConnections.json();
|
remoteConnections = await remoteConnections.json();
|
||||||
|
|
||||||
|
let i = 1;
|
||||||
for (let connection of connections){
|
for (let connection of connections){
|
||||||
|
spinner.text = `Syncing ${i++} connections...`
|
||||||
if (connection.isConnected == false){
|
if (connection.isConnected == false){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -349,7 +354,6 @@ export async function syncConnections(){
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log('Connection: ', connection, 'exists in the remote server');
|
|
||||||
await models.connection.update({ isConnected: true }, {
|
await models.connection.update({ isConnected: true }, {
|
||||||
where: {
|
where: {
|
||||||
organizationId: connection.organizationId
|
organizationId: connection.organizationId
|
||||||
|
@ -363,7 +367,11 @@ export async function syncConnections(){
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
spinner.succeed();
|
||||||
|
} catch (err) {
|
||||||
|
spinner.fail();
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function syncTradeItems(){
|
export async function syncTradeItems(){
|
||||||
|
@ -378,7 +386,7 @@ export async function syncTradeItems(){
|
||||||
let query = `${url}/trade-items?supplierOrganizationId=${supplier.organizationId}`;
|
let query = `${url}/trade-items?supplierOrganizationId=${supplier.organizationId}`;
|
||||||
let headers = {
|
let headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': `Bearer ${await getJWT()}`,
|
'Authorization': `Bearer ${await getCurrentToken()}`,
|
||||||
'X-Api-Key': process.env.API_KEY
|
'X-Api-Key': process.env.API_KEY
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
|
@ -391,7 +399,6 @@ export async function syncTradeItems(){
|
||||||
let tradeItems = await request.json();
|
let tradeItems = await request.json();
|
||||||
|
|
||||||
if (!tradeItems.length) {
|
if (!tradeItems.length) {
|
||||||
console.log('No trade items for supplier: ', supplier.commercialName);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -419,8 +426,8 @@ export async function syncTradeItems(){
|
||||||
* to do this, it fetches all the supply lines for every tradeitem of the suppliers
|
* to do this, it fetches all the supply lines for every tradeitem of the suppliers
|
||||||
*/
|
*/
|
||||||
export async function syncSupplyLines(){
|
export async function syncSupplyLines(){
|
||||||
|
try {
|
||||||
let currentSequenceNumber = await models.sequenceNumber.findOne({
|
let currentSequenceNumber = await models.sequenceNumber.findOne({ // TODO: Mirar como manejar este error
|
||||||
where: {
|
where: {
|
||||||
model: 'supplyLines'
|
model: 'supplyLines'
|
||||||
}
|
}
|
||||||
|
@ -443,7 +450,7 @@ export async function syncSupplyLines(){
|
||||||
|
|
||||||
let headers = {
|
let headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': `Bearer ${await getJWT()}`,
|
'Authorization': `Bearer ${await getCurrentToken()}`,
|
||||||
'X-Api-Key': process.env.API_KEY
|
'X-Api-Key': process.env.API_KEY
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -567,16 +574,19 @@ export async function syncSupplyLines(){
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (err) {
|
||||||
spinner.fail();
|
spinner.fail();
|
||||||
console.log('Error while syncing supply lines - ' + supplyLine.results[0].tradeItemId);
|
console.log('Error while syncing supply lines - ' + supplyLine.results[0].tradeItemId);
|
||||||
console.log(error);
|
throw new Error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
console.log('Found', suppliers.length, 'connected suppliers');
|
console.log('Found', suppliers.length, 'connected suppliers');
|
||||||
|
|
||||||
await syncSequence(currentSequenceNumber,'supplyLines' ,maximumSequenceNumber);
|
await syncSequence(currentSequenceNumber,'supplyLines' ,maximumSequenceNumber);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function insertItem(tradeItem, supplier) {
|
export async function insertItem(tradeItem, supplier) {
|
||||||
|
|
Loading…
Reference in New Issue