75 lines
2.1 KiB
JavaScript
75 lines
2.1 KiB
JavaScript
import moment from 'moment';
|
|
import * as vnUtils from './utils.js';
|
|
import cliProgress from 'cli-progress';
|
|
|
|
import dotenv from 'dotenv';
|
|
dotenv.config();
|
|
|
|
import { models } from './models/index.js';
|
|
|
|
import suppliers from './suppliersGln.js';
|
|
|
|
console.log = function () {
|
|
let args = Array.prototype.slice.call(arguments);
|
|
args.unshift(new moment().format('HH:mm:ss') + ' -');
|
|
console.info.apply(console, args);
|
|
};
|
|
|
|
let tokenExpirationDate = await vnUtils.getClientToken(models);
|
|
|
|
try {
|
|
|
|
if (process.env.QUERYSUPPLIERS) {
|
|
|
|
process.env.HowManySuppliers ??= suppliers.suppliers.length;
|
|
|
|
console.log('Querying suppliers...');
|
|
console.log(process.env.HowManySuppliers + ' suppliers will be queried.');
|
|
|
|
const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
|
|
bar1.start(process.env.HowManySuppliers, 0);
|
|
|
|
let functionQueue = [];
|
|
|
|
// vnUtils.getTradeitems(suppliers.suppliers[i].SupplierGLN)
|
|
for (let i = 0; i < process.env.HowManySuppliers; i++) {
|
|
functionQueue.push(async () => {
|
|
await vnUtils.getTradeitems(suppliers.suppliers[i].SupplierGLN);
|
|
bar1.increment();
|
|
});
|
|
}
|
|
|
|
await vnUtils.asyncQueue(functionQueue, 10);
|
|
|
|
bar1.stop();
|
|
|
|
console.log('Done querying trade items.');
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line no-constant-condition
|
|
while (true) {
|
|
try{
|
|
console.log('Querying the API to check for new data...');
|
|
console.log('Current token expiration date: ', tokenExpirationDate);
|
|
|
|
await vnUtils.getStock();
|
|
|
|
if (moment().isAfter(tokenExpirationDate)) {
|
|
console.log('Token expired, getting a new one...');
|
|
tokenExpirationDate = await vnUtils.getClientToken(models);
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
|
|
if (process.env.STATUS == 'development') {
|
|
await vnUtils.sleep(15000);
|
|
} else {
|
|
await vnUtils.sleep(30000);
|
|
}
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Unable to connect to the database:', error);
|
|
} |