2023-01-09 10:59:07 +00:00
|
|
|
import moment from 'moment';
|
2023-01-10 12:24:43 +00:00
|
|
|
import * as vnUtils from './utils.js';
|
2023-01-12 13:54:05 +00:00
|
|
|
import cliProgress from 'cli-progress';
|
2023-01-09 10:59:07 +00:00
|
|
|
|
|
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
2022-12-05 12:53:30 +00:00
|
|
|
|
2023-01-11 12:13:22 +00:00
|
|
|
import { models } from './models/index.js';
|
|
|
|
|
|
|
|
import suppliers from './suppliersGln.js';
|
2022-12-05 12:53:30 +00:00
|
|
|
|
2023-01-10 12:24:43 +00:00
|
|
|
let tokenExpirationDate = await vnUtils.getClientToken(models);
|
2022-12-05 12:53:30 +00:00
|
|
|
|
|
|
|
try {
|
2023-01-11 12:13:22 +00:00
|
|
|
|
|
|
|
if (process.env.QUERYSUPPLIERS) {
|
|
|
|
|
|
|
|
console.log('Querying suppliers...');
|
|
|
|
console.log(suppliers.suppliers.length + ' suppliers found');
|
|
|
|
|
2023-01-12 13:54:05 +00:00
|
|
|
const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
|
|
|
|
bar1.start(suppliers.suppliers.length, 0);
|
|
|
|
|
|
|
|
// vnUtils.getTradeitems(suppliers.suppliers[i].SupplierGLN)
|
2023-01-11 12:13:22 +00:00
|
|
|
for (let i = 0; i < suppliers.suppliers.length; i++) {
|
2023-01-12 13:54:05 +00:00
|
|
|
await vnUtils.getTradeitems(suppliers.suppliers[i].SupplierGLN).then(() => {
|
|
|
|
bar1.increment();
|
|
|
|
});
|
2023-01-11 12:13:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:54:05 +00:00
|
|
|
bar1.stop();
|
|
|
|
|
|
|
|
}
|
2023-01-11 12:13:22 +00:00
|
|
|
|
2022-12-19 13:34:39 +00:00
|
|
|
setInterval(async () => {
|
2023-01-09 10:59:07 +00:00
|
|
|
console.log('Querying the API to check for new data...');
|
2023-01-10 12:24:43 +00:00
|
|
|
|
2023-01-11 12:13:22 +00:00
|
|
|
|
|
|
|
|
2023-01-09 10:59:07 +00:00
|
|
|
console.log('Current token expiration date: ', tokenExpirationDate);
|
2022-12-19 13:34:39 +00:00
|
|
|
|
|
|
|
if (moment().isAfter(tokenExpirationDate)) {
|
2023-01-09 10:59:07 +00:00
|
|
|
console.log('Token expired, getting a new one...');
|
2023-01-10 12:24:43 +00:00
|
|
|
tokenExpirationDate = await vnUtils.getClientToken(models);
|
2022-12-19 13:34:39 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:13:22 +00:00
|
|
|
}, process.env.STATUS == 'development' ? 15000 : 30000);
|
2022-12-05 12:53:30 +00:00
|
|
|
} catch (error) {
|
2023-01-09 10:59:07 +00:00
|
|
|
console.error('Unable to connect to the database:', error);
|
2022-12-05 12:53:30 +00:00
|
|
|
}
|
2022-12-19 12:21:35 +00:00
|
|
|
|
|
|
|
console.log = function () {
|
2023-01-09 10:59:07 +00:00
|
|
|
let args = Array.prototype.slice.call(arguments);
|
|
|
|
args.unshift(new moment().format('HH:mm:ss') + ' -');
|
2022-12-19 13:34:39 +00:00
|
|
|
console.info.apply(console, args);
|
2023-01-12 13:54:05 +00:00
|
|
|
};
|