floriday/main.js

51 lines
1.5 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;
2023-02-10 09:42:54 +00:00
await vnUtils.syncConnections();
2023-02-08 11:31:54 +00:00
process.env.SYNC_TRADEITEM ? await vnUtils.syncTradeItems() : null;
2023-02-14 12:34:54 +00:00
console.log('Synced trade items');
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);
2023-02-14 12:34:54 +00:00
2023-01-24 12:51:44 +00:00
if (moment().isAfter(tokenExpirationDate)) {
console.log('Token expired, getting a new one...');
tokenExpirationDate = await vnUtils.getClientToken(models);
}
2023-02-14 12:34:54 +00:00
await vnUtils.syncSupplyLines();
2023-02-10 09:42:54 +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') {
2023-02-14 12:34:54 +00:00
await vnUtils.sleep(120000);
2023-01-16 13:52:08 +00:00
} else {
2023-02-14 12:34:54 +00:00
await vnUtils.sleep(300000);
2023-01-16 13:52:08 +00:00
}
}
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
}