floriday/index.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-01-09 10:59:07 +00:00
import moment from 'moment';
import * as vnUtils from './utils.js';
2023-02-03 12:56:34 +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';
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
2023-02-03 12:56:34 +00:00
process.env.SYNC_SEQUENCE ? await vnUtils.syncSequence() : null;
process.env.SYNC_SUPPLIER ? await vnUtils.syncSuppliers() : null;
process.env.SYNC_TRADEITEM ? await vnUtils.syncTradeItems() : null;
2023-01-11 12:13:22 +00:00
2023-02-03 12:56:34 +00:00
try {
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);
if (moment().isAfter(tokenExpirationDate)) {
console.log('Token expired, getting a new one...');
tokenExpirationDate = await vnUtils.getClientToken(models);
}
2023-02-03 12:56:34 +00:00
2023-01-24 12:51:44 +00:00
} 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
}