refs #4823 Improved code
This commit is contained in:
parent
bfe36b1284
commit
1a97f16a11
|
@ -10,6 +10,7 @@ class Floriday {
|
||||||
async start() {
|
async start() {
|
||||||
try {
|
try {
|
||||||
this.tokenExpirationDate = await utils.requestToken(models);
|
this.tokenExpirationDate = await utils.requestToken(models);
|
||||||
|
if (!env.API_KEY) throw new Error(`You haven't provided the API key`)
|
||||||
if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence();
|
if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence();
|
||||||
if (JSON.parse(env.SYNC_SUPPLIER)) await utils.syncSuppliers();
|
if (JSON.parse(env.SYNC_SUPPLIER)) await utils.syncSuppliers();
|
||||||
if (JSON.parse(env.SYNC_CONN)) await utils.syncConn();
|
if (JSON.parse(env.SYNC_CONN)) await utils.syncConn();
|
||||||
|
|
|
@ -12,14 +12,11 @@ const clientConfig = {
|
||||||
clientSecret: {
|
clientSecret: {
|
||||||
type: Sequelize.STRING,
|
type: Sequelize.STRING,
|
||||||
},
|
},
|
||||||
requestLimit: {
|
|
||||||
type: Sequelize.INTEGER,
|
|
||||||
},
|
|
||||||
currentToken: {
|
currentToken: {
|
||||||
type: Sequelize.STRING(2000),
|
type: Sequelize.STRING(2000),
|
||||||
},
|
},
|
||||||
tokenExpiration: {
|
tokenExpiration: {
|
||||||
type: Sequelize.STRING,
|
type: Sequelize.DATE,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,11 @@ import { Sequelize } from 'sequelize';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import ora from 'ora';
|
import ora from 'ora';
|
||||||
import { criticalError } from './../utils.js';
|
import { criticalError, updateClientConfig } from './../utils.js';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
const env = process.env;
|
||||||
|
|
||||||
console.clear()
|
console.clear()
|
||||||
console.log(chalk.hex('#06c581')(
|
console.log(chalk.hex('#06c581')(
|
||||||
`
|
`
|
||||||
|
@ -178,17 +180,6 @@ if (JSON.parse(process.env.FORCE_SYNC)) {
|
||||||
const modSpinner = ora(`${action} models...`).start();
|
const modSpinner = ora(`${action} models...`).start();
|
||||||
try {
|
try {
|
||||||
await sequelize.sync({ force: isForce });
|
await sequelize.sync({ force: isForce });
|
||||||
if (process.env.SECRETS) {
|
|
||||||
await models.clientConfig.findOrCreate({
|
|
||||||
where: {
|
|
||||||
id: 1,
|
|
||||||
},
|
|
||||||
defaults: {
|
|
||||||
clientId: process.env.CLIENT_ID,
|
|
||||||
clientSecret: process.env.CLIENT_SECRET,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
modSpinner.succeed();
|
modSpinner.succeed();
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
|
47
utils.js
47
utils.js
|
@ -13,17 +13,28 @@ const env = process.env;
|
||||||
* @returns {Date} tokenExpirationDate formated as YYYY-MM-DD HH:mm:ss
|
* @returns {Date} tokenExpirationDate formated as YYYY-MM-DD HH:mm:ss
|
||||||
*/
|
*/
|
||||||
export async function requestToken() {
|
export async function requestToken() {
|
||||||
|
let spinner;
|
||||||
|
try {
|
||||||
|
spinner = ora(`Requesting new token...`).start();
|
||||||
|
|
||||||
|
if (!env.CLIENT_ID || !env.CLIENT_SECRET)
|
||||||
|
throw new Error(`You haven't provided the credentials`)
|
||||||
|
|
||||||
const clientConfigData = await models.clientConfig.findOne();
|
const clientConfigData = await models.clientConfig.findOne();
|
||||||
|
|
||||||
if (!clientConfigData)
|
let tokenExpirationDate;
|
||||||
throw new Error('No data found in the client config table')
|
if (clientConfigData)
|
||||||
|
tokenExpirationDate = clientConfigData.tokenExpiration;
|
||||||
|
|
||||||
const spinner = ora(`Requesting new token...`).start();
|
if (!tokenExpirationDate || moment().isAfter(tokenExpirationDate)) {
|
||||||
const tokenExpirationDate = clientConfigData.tokenExpiration;
|
let clientId, clientSecret
|
||||||
|
if (JSON.parse(env.USE_SECRETS_DB)) {
|
||||||
if (clientConfigData.tokenExpiration == null || moment().isAfter(tokenExpirationDate)) {
|
clientId = clientConfigData.clientId;
|
||||||
let clientId = clientConfigData.clientId;
|
clientSecret = clientConfigData.clientSecret;
|
||||||
let clientSecret = clientConfigData.clientSecret;
|
} else {
|
||||||
|
clientId = env.CLIENT_ID
|
||||||
|
clientSecret = env.CLIENT_SECRET
|
||||||
|
};
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
grant_type: 'client_credentials',
|
grant_type: 'client_credentials',
|
||||||
|
@ -46,9 +57,9 @@ export async function requestToken() {
|
||||||
|
|
||||||
const responseData = await response.json();
|
const responseData = await response.json();
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok)
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
} else {
|
else {
|
||||||
spinner.fail();
|
spinner.fail();
|
||||||
criticalError(new Error(`Token request failed with status: ${response.status} - ${response.statusText}`));
|
criticalError(new Error(`Token request failed with status: ${response.status} - ${response.statusText}`));
|
||||||
}
|
}
|
||||||
|
@ -69,6 +80,10 @@ export async function requestToken() {
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
return tokenExpirationDate;
|
return tokenExpirationDate;
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
spinner.fail();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -94,7 +109,6 @@ export async function getCurrentTokenExpiration() {
|
||||||
/**
|
/**
|
||||||
* Updates the Access Token in the client config table
|
* Updates the Access Token in the client config table
|
||||||
*
|
*
|
||||||
* @param {sequelize.models} models
|
|
||||||
* @param {String} clientId
|
* @param {String} clientId
|
||||||
* @param {String} clientSecret
|
* @param {String} clientSecret
|
||||||
* @param {String} accessToken
|
* @param {String} accessToken
|
||||||
|
@ -103,13 +117,15 @@ export async function getCurrentTokenExpiration() {
|
||||||
export async function updateClientConfig(clientId, clientSecret, accessToken, tokenExpirationDate) {
|
export async function updateClientConfig(clientId, clientSecret, accessToken, tokenExpirationDate) {
|
||||||
try {
|
try {
|
||||||
const spinner = ora(`Updating token...`).start();
|
const spinner = ora(`Updating token...`).start();
|
||||||
|
if (!JSON.parse(process.env.USE_SECRETS_DB))
|
||||||
|
clientId = clientSecret = null
|
||||||
|
|
||||||
await models.clientConfig.upsert({
|
await models.clientConfig.upsert({
|
||||||
id: 1,
|
id: 1,
|
||||||
clientId: clientId,
|
clientId: clientId,
|
||||||
clientSecret: clientSecret,
|
clientSecret: clientSecret,
|
||||||
currentToken: accessToken,
|
currentToken: accessToken,
|
||||||
tokenExpiration: tokenExpirationDate,
|
tokenExpiration: tokenExpirationDate
|
||||||
requestLimit: 500,
|
|
||||||
});
|
});
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -325,9 +341,8 @@ export async function syncConn(){
|
||||||
let i = 1;
|
let i = 1;
|
||||||
for (let connection of connections){
|
for (let connection of connections){
|
||||||
spinner.text = `Syncing ${i++} connections...`
|
spinner.text = `Syncing ${i++} connections...`
|
||||||
if (connection.isConnected == false){
|
if (connection.isConnected == false)
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.organizationId);
|
let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.organizationId);
|
||||||
|
|
||||||
if (remoteConnection == undefined){
|
if (remoteConnection == undefined){
|
||||||
|
@ -710,6 +725,6 @@ export async function insertItem(tradeItem, supplier) {
|
||||||
* @param {err}
|
* @param {err}
|
||||||
**/
|
**/
|
||||||
export async function criticalError(err) {
|
export async function criticalError(err) {
|
||||||
console.log(chalk.red.bold(`[ERROR]`), chalk.red(`${err.name}: ${err.message}`));
|
console.log(chalk.red.bold(`[CRITICAL]`), chalk.red(`${err.message}`));
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
Loading…
Reference in New Issue