floriday/index.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-01-09 10:59:07 +00:00
import moment from 'moment';
import * as vnUtils from './utils.js';
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-24 12:51:44 +00:00
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);
2022-12-05 12:53:30 +00:00
try {
2023-01-11 12:13:22 +00:00
if (process.env.QUERYSUPPLIERS) {
2023-01-16 13:52:08 +00:00
process.env.HowManySuppliers ??= suppliers.suppliers.length;
2023-01-11 12:13:22 +00:00
console.log('Querying suppliers...');
2023-01-16 13:52:08 +00:00
console.log(process.env.HowManySuppliers + ' suppliers will be queried.');
2023-01-11 12:13:22 +00:00
const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
2023-01-16 13:52:08 +00:00
bar1.start(process.env.HowManySuppliers, 0);
let functionQueue = [];
// vnUtils.getTradeitems(suppliers.suppliers[i].SupplierGLN)
2023-01-16 13:52:08 +00:00
for (let i = 0; i < process.env.HowManySuppliers; i++) {
functionQueue.push(async () => {
await vnUtils.getTradeitems(suppliers.suppliers[i].SupplierGLN);
bar1.increment();
});
2023-01-11 12:13:22 +00:00
}
2023-01-16 13:52:08 +00:00
await vnUtils.asyncQueue(functionQueue, 10);
bar1.stop();
2023-01-16 13:52:08 +00:00
console.log('Done querying trade items.');
}
2023-01-11 12:13:22 +00:00
2023-01-16 13:52:08 +00:00
// eslint-disable-next-line no-constant-condition
while (true) {
2023-01-24 12:51:44 +00:00
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);
}
2023-01-24 12:51:44 +00:00
2023-01-16 13:52:08 +00:00
if (process.env.STATUS == 'development') {
await vnUtils.sleep(15000);
} else {
await vnUtils.sleep(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);
2023-01-24 12:51:44 +00:00
}