floriday/utils.js

760 lines
21 KiB
JavaScript

import { models } from './models/sequelize.js';
import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';
import moment from 'moment';
import chalk from 'chalk';
import ora from 'ora';
const env = process.env;
/**
* Gets the Access Token from the client config table
*
* @param {sequelize.models} models
* @returns {Date} tokenExpirationDate formated as YYYY-MM-DD HH:mm:ss
*/
export async function requestToken() {
let spinner = ora(`Requesting new token...`).start();
try {
const clientConfigData = await models.clientConfig.findOne();
let tokenExpirationDate, token;
if (clientConfigData) {
token = clientConfigData.currentToken;
tokenExpirationDate = clientConfigData.tokenExpiration;
}
if (!token || !tokenExpirationDate || moment().isAfter(tokenExpirationDate)) {
let clientId, clientSecret
if (JSON.parse(env.USE_SECRETS_DB)) {
clientId = clientConfigData.clientId;
clientSecret = clientConfigData.clientSecret;
} else {
clientId = env.CLIENT_ID
clientSecret = env.CLIENT_SECRET
};
let data = {
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
scope: 'role:app catalog:read supply:read organization:read network:write network:read'
};
data = Object.keys(data)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`)
.join('&')
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
const response = (await vnRequest('GET', env.API_ENDPOINT, data, headers)).data;
if (response.statusText = 'OK')
spinner.succeed();
else {
spinner.fail();
criticalError(new Error(`Token request failed with status: ${response.status} - ${response.statusText}`));
}
let tokenExpirationDate = moment()
.add(response.expires_in, 's')
.format('YYYY-MM-DD HH:mm:ss');
await updateClientConfig(
clientId,
clientSecret,
response.access_token,
tokenExpirationDate
);
return tokenExpirationDate;
} else {
spinner.text = 'Using stored token...'
spinner.succeed();
return tokenExpirationDate;
}
} catch (err) {
spinner.fail();
throw err;
}
}
/**
* Returns the current token
*
* @returns {string}
*/
export async function getCurrentToken() {
if (moment().isAfter(await getCurrentTokenExpiration()))
return await requestToken(models);
else
return (await models.clientConfig.findOne()).currentToken;
}
/**
* Check the floriday data config
*/
export async function checkConfig() {
const spinner = ora(`Checking config...`).start();
const excludedEnvVars = ['VSCODE_GIT_ASKPASS_EXTRA_ARGS'];
const requiredEnvVars = Object.keys(env);
const filteredEnvVars = requiredEnvVars.filter(reqEnvVar => !excludedEnvVars.includes(reqEnvVar));
for (const reqEnvVar of filteredEnvVars) {
if (!process.env[reqEnvVar]) {
spinner.fail();
throw new Error(`You haven't provided the ${reqEnvVar} environment variable`);
}
}
const clientConfigData = await models.clientConfig.findOne();
if (!clientConfigData)
await updateClientConfig(env.CLIENT_ID, env.CLIENT_SECRET);
spinner.succeed();
}
/**
* Returns the expiration of current token
*
* @returns {string}
*/
export async function getCurrentTokenExpiration() {
return (await models.clientConfig.findOne()).tokenExpiration;
}
/**
* Updates the access token in the client config table
*
* @param {String} clientId
* @param {String} clientSecret
* @param {String} accessToken
* @param {String} tokenExpirationDate
*/
export async function updateClientConfig(clientId, clientSecret, currentToken, tokenExpiration) {
try {
const spinner = ora(`Updating token...`).start();
if (!JSON.parse(process.env.USE_SECRETS_DB))
clientId = clientSecret = null
await models.clientConfig.upsert({
id: 1,
clientId,
clientSecret,
currentToken,
tokenExpiration,
});
spinner.succeed();
} catch (err) {
spinner.fail();
throw(err);
}
}
/**
* pauses the execution of the script for the given amount of milliseconds
*
* @param {integer} ms
* @returns A promise that resolves after ms milliseconds.
*/
export async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
/**
* Recieves an array of functions and executes them in a queue with the given concurrency
*
* @param {Array} fnArray
* @param {Number} concurrency
* @returns
*/
export async function asyncQueue(fnArray, concurrency = 1) {
const results = []; // 1
const queue = fnArray.map((fn, index) => () => fn().then((result) => results[index] = result));
const run = async () => { // 2
const fn = queue.shift();
if (fn) {
await fn();
await run();
}
};
const promises = []; // 3
while (concurrency--) { // 4
promises.push(run());
}
await Promise.all(promises); // 5
return results;
}
// 1. Create an array of functions that will be executed in a queue
// 2. Create a function that will execute the functions in the queue
// 3. Create an array of promises that will execute the run function
// 4. Execute the run function while the concurrency is greater than 0
// 5. Return the results
/**
* Syncs the sequence number for the given model
* if no params are given it will reset all the sequence number to 0
*
* @param {Number} current - current sequence number
* @param {String} model - model name
* @param {Number} maximumSequenceNumber - maximum sequence number
* @returns
*/
export async function syncSequence(current = 0, model = null , maximumSequenceNumber = 0){
if (model == null && current == 0){
try {
const spinner = ora(`Syncing sequence...`).start();
let mockModels = [
'supplier',
'tradeItems',
'supplyLines',
];
let i = 1;
for (let mockModel in mockModels) {
spinner.text = `Syncing ${i++} sequences...`
const element = mockModels[mockModel];
await syncSequence(0, element);
}
spinner.succeed();
} catch (err) {
spinner.fail();
throw new Error(err);
}
} else if (current) {
let tx = await models.sequelize.transaction();
try {
let sequence = await models.sequenceNumber.findOrCreate({
where: {
model: model
},
defaults: {
model: model,
sequenceNumber: current,
maximumSequenceNumber: maximumSequenceNumber
},
transaction: tx
});
if (sequence[1] == false){
await models.sequenceNumber.update({
sequenceNumber: current,
maximumSequenceNumber: maximumSequenceNumber
}, {
where: {
model: model
},
transaction: tx
});
}
await tx.commit();
} catch (error) {
await tx.rollback();
console.log(`Error while syncing sequence number for: ${model}: ${error}`);
}
}
}
export async function syncSuppliers(){
let spinner = ora('Preparing to load suppliers...').start();
try {
let headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY,
};
const maxSequenceNumber = (await vnRequest('GET', `${env.API_URL}/organizations/current-max-sequence`, null, headers)).data;
let timeFinish, timeToGoSec, timeToGoMin, timeLeft;
for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
let timeStart = new moment();
let data = (await vnRequest('GET', `${env.API_URL}/organizations/sync/${curSequenceNumber}?organizationType=SUPPLIER`, null, headers)).data;
let suppliers = data.results;
for (let supplier of suppliers) {
curSequenceNumber = supplier.sequenceNumber;
spinner.text = `Syncing suppliers, ${maxSequenceNumber - curSequenceNumber} are missing`
if (timeFinish)
spinner.text = spinner.text + ` (${timeLeft})`
await models.supplier.upsert({
supplierOrganizationId: supplier.organizationId,
sequenceNumber: supplier.sequenceNumber,
companyGln: supplier.companyGln ? supplier.companyGln : null,
name: supplier.name ? supplier.name : null,
commercialName: supplier.commercialName ? supplier.commercialName : null,
email: supplier.email ? supplier.email : null,
phone: supplier.phone ? supplier.phone : null,
website: supplier.website ? supplier.website : null,
rfhRelationId: supplier.rfhRelationId ? supplier.rfhRelationId : null,
paymentProviders: supplier.paymentProviders.length ? `${supplier.paymentProviders}` : null,
endDate: supplier.endDate ? supplier.endDate : null,
mailingAddress: supplier.mailingAddress ? supplier.mailingAddress : null,
physicalAddress: supplier.physicalAddress ? supplier.physicalAddress : null,
});
};
await syncSequence(curSequenceNumber, 'supplier', maxSequenceNumber);
timeFinish = new moment();
timeToGoSec = (timeFinish.diff(timeStart, 'seconds') * (maxSequenceNumber - curSequenceNumber) / 1000)
timeToGoMin = Math.trunc(timeToGoSec / 60)
if (!timeToGoMin)
timeLeft = `${Math.trunc(timeToGoSec)} sec`
else
timeLeft = `${timeToGoMin} min`
}
spinner.text = `Syncing suppliers...`;
spinner.succeed()
}
catch (err) {
spinner.fail();
throw new Error(err);
}
}
export async function syncConn(){
const spinner = ora(`Creating connections...`).start();
try {
let connections = await models.connection.findAll();
let headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY
};
const remoteConnections = (await vnRequest('GET', `${env.API_URL}/connections`, null, headers)).data;
let i = 1;
for (let connection of connections){
spinner.text = `Creating ${i++} connections...`
let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.supplierOrganizationId);
if (!remoteConnection){
await vnRequest('PUT', `${env.API_URL}/connections/${connection.supplierOrganizationId}`, null, headers);
await models.connection.update({ isConnected: true }, {
where: {
supplierOrganizationId: connection.supplierOrganizationId
}
});
await models.supplier.update({ isConnected: true }, {
where: {
supplierOrganizationId: connection.supplierOrganizationId
}
});
} else {
await models.connection.update({ isConnected: true }, {
where: {
supplierOrganizationId: connection.supplierOrganizationId
}
});
await models.supplier.update({ isConnected: true }, {
where: {
supplierOrganizationId: connection.supplierOrganizationId
}
});
}
}
spinner.succeed();
} catch (err) {
spinner.fail();
throw new Error(err);
}
}
export async function syncTradeItems(){
const spinner = ora(`Syncing trade items...`).start();
const suppliers = await models.supplier.findAll({
where: { isConnected: true }
});
let i = 0;
let x = 0;
for (let supplier of suppliers) {
let headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY
};
try {
let tradeItems = (await vnRequest('GET', `${env.API_URL}/trade-items?supplierOrganizationId=${supplier.supplierOrganizationId}`, null, headers)).data
spinner.text = `Syncing ${i} trade items of [${x++}|${suppliers.length}] suppliers...`
if (!tradeItems.length) continue;
for (let tradeItem of tradeItems) {
await insertItem(tradeItem);
spinner.text = `Syncing ${i++} trade items of [${x++}|${suppliers.length}] suppliers...`
};
} catch (err) {
spinner.fail();
throw err;
}
}
spinner.succeed()
}
/**
* Syncs the supply lines for suppliers that are connected to do this,
* it searches all the supply lines for every tradeitem of the suppliers
*/
export async function syncSupplyLines(){
const spinner = ora(`Syncing supply lines...`).start();
try {
const currentSequenceNumber = await models.sequenceNumber.findOne({
where: { model: 'supplyLines' }
});
let connectedSuppliers = await models.supplier.findAll({
where: { isConnected: true }
});
let tradeItems = await models.tradeItem.findAll({
where: { supplierOrganizationId: connectedSuppliers.map(supplier => supplier.supplierOrganizationId) }
});
let promises = [];
let headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY
};
// Launch a promise for each supplier
for (let tradeItem of tradeItems) {
let supplier = connectedSuppliers.find(({ supplierOrganizationId }) => supplierOrganizationId === tradeItem.supplierOrganizationId);
// eslint-disable-next-line no-async-promise-executor
let promise = new Promise(async (resolve) => {
try {
const params = new URLSearchParams({
supplierOrganizationId: supplier.supplierOrganizationId,
tradeItemId: tradeItem.tradeItemId,
postFilterSelectedTradeItems: false
}).toString();
let supplyLines = (await vnRequest('GET', `${`${env.API_URL}/supply-lines`}?${params}`, null, headers)).data;
if (!supplyLines.results.length) {
resolve([]);
return;
}
resolve(supplyLines);
} catch (err) {
if (err.message = 'Request failed with status code 429')
console.log('Request failed with status code 429 - Too many request');
resolve([]);
}
});
promises.push(promise);
}
let supplyLines = await Promise.all(promises);
let maximumSequenceNumber;
for (let supplyLine of supplyLines) {
maximumSequenceNumber = supplyLine.maximumSequenceNumber;
supplyLine = supplyLine.results;
try {
if (supplyLine) {
for (let line of supplyLine) {
let tradeItem = await models.tradeItem.findOne({
where: {
tradeItemId: line.tradeItemId
}
});
if (!tradeItem) {
console.log('Trade item not found for supply line: ', line.supplyLineId);
console.log('Requesting data for trade item id: ', line.tradeItemId);
let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items?tradeItemIds=${line.tradeItemId}`, null, headers)).data;
if (!tradeItem.length) {
console.log('Trade item not found for supply line: ', line.supplyLineId);
console.log('Trade item id: ', line.tradeItemId);
continue;
}
await insertItem(tradeItem[0]);
tradeItem = await models.tradeItem.findOne({
where: {
tradeItemId: line.tradeItemId
}
});
if (!tradeItem) {
console.log('Trade item not found for supply line: ', line.supplyLineId);
console.log('Trade item id: ', line.tradeItemId);
continue;
}
}
await models.supplyLine.upsert({
supplyLineId: line.supplyLineId,
status: line.status,
supplierOrganizationId: line.supplierOrganizationId,
pricePerPiece_currency: line.pricePerPiece.currency,
pricePerPiece_value: line.pricePerPiece.value,
numberOfPieces: line.numberOfPieces,
deliveryPeriod_startDateTime: line.deliveryPeriod.startDateTime,
deliveryPeriod_endDateTime: line.deliveryPeriod.endDateTime,
orderPeriod_startDateTime: line.orderPeriod.startDateTime,
orderPeriod_endDateTime: line.orderPeriod.endDateTime,
warehouseId: line.warehouseId,
sequenceNumber: line.sequenceNumber,
type: line.type,
isDeleted: line.isDeleted,
salesUnit: line.salesUnit,
agreementReference_code: line.agreementReference.code ? line.agreementReference.code : null,
agreementReference_description: line.agreementReference.description ? line.agreementReference.description : null,
isLimited: line.isLimited,
isCustomerSpecific: line.isCustomerSpecific,
tradeItemId: line.tradeItemId,
});
for (let volumePrice of line.volumePrices)
await models.volumePrices.upsert({
unit: volumePrice.unit,
pricePerPiece: volumePrice.pricePerPiece,
supplyLineId: line.supplyLineId,
});
}
}
} catch (err) {
throw err;
}
}
spinner.succeed();
console.log('Found', connectedSuppliers.length, 'connected suppliers');
await syncSequence(currentSequenceNumber, 'supplyLines' ,maximumSequenceNumber);
} catch (err) {
spinner.fail();
throw err;
}
}
export async function newSyncSupplyLines() {
const spinner = ora(`(NEW) Syncing supply lines...`).start();
try {
let headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY
};
let suppliersWithTradeItem = await models.tradeItem.findAll({
attributes: ['supplierOrganizationId'],
group: ['supplierOrganizationId']
});
let connectedSuppliers = await models.supplier.findAll({
attributes: ['supplierOrganizationId'],
where: { isConnected: true }
});
let suppliers = suppliersWithTradeItem.filter(supplier => {
return connectedSuppliers.some(connectedSupplier => {
return connectedSupplier.supplierOrganizationId === supplier.supplierOrganizationId;
});
}).map(supplier => supplier.supplierOrganizationId);
let i = 1;
for (let supplier of suppliers) {
spinner.text = `(NEW) Syncing ${i++} supply lines...`
const params = new URLSearchParams({
supplierOrganizationId: supplier,
}).toString();
let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`, null, headers)).data;
if (!supplyLines.length) continue
for (let supplyLine of supplyLines)
console.log(supplyLine)
}
spinner.succeed();
}
catch (err) {
spinner.fail();
throw err;
}
}
/**
* Insert the trade item
*
* @param {array} tradeItem
*/
export async function insertItem(tradeItem) {
let tx;
try {
tx = await models.sequelize.transaction();
// Upsert supplier connection
await models.connection.upsert({
supplierOrganizationId: tradeItem.supplierOrganizationId,
}, { transaction: tx });
// Upsert trade item
await models.tradeItem.upsert({
...tradeItem,
isDeleted: tradeItem.isDeleted,
isCustomerSpecific: tradeItem.isCustomerSpecific,
isHiddenInCatalog: tradeItem.isHiddenInCatalog,
}, { transaction: tx });
// Upsert characteristics
if (tradeItem.characteristics)
for (const characteristic of tradeItem.characteristics) {
await models.characteristic.upsert({
...characteristic,
tradeItemId: tradeItem.tradeItemId,
}, { transaction: tx });
}
// Upsert seasonal periods
if (tradeItem.seasonalPeriods)
for (const seasonalPeriod of tradeItem.seasonalPeriods) {
await models.seasonalPeriod.upsert({
...seasonalPeriod,
tradeItemId: tradeItem.tradeItemId,
}, { transaction: tx });
}
// Upsert photos
if (tradeItem.photos)
for (const photo of tradeItem.photos) {
await models.photo.upsert({
...photo,
tradeItemId: tradeItem.tradeItemId,
}, { transaction: tx });
}
// Upsert packing configurations
if (tradeItem.packingConfigurations)
for (const packingConfiguration of tradeItem.packingConfigurations) {
const uuid = uuidv4();
await models.packingConfiguration.upsert({
packingConfigurationId: uuid,
...packingConfiguration,
additionalPricePerPiece: JSON.stringify(packingConfiguration.additionalPricePerPiece),
tradeItemId: tradeItem.tradeItemId,
}, { transaction: tx });
await models.package.upsert({
...packingConfiguration.package,
packingConfigurationFk: uuid,
}, { transaction: tx });
}
// Upsert country of origin ISO codes
if (tradeItem.countryOfOriginIsoCodes)
for (const isoCode of tradeItem.countryOfOriginIsoCodes || []) {
await models.countryOfOriginIsoCode.upsert({
isoCode,
tradeItemId: tradeItem.tradeItemId,
}, { transaction: tx });
}
// Upsert botanical names
if (tradeItem.botanicalNames)
for (const botanicalName of tradeItem.botanicalNames) {
await models.botanicalName.upsert({
...botanicalName,
tradeItemId: tradeItem.tradeItemId,
}, { transaction: tx });
}
await tx.commit();
} catch (err) {
await tx.rollback();
throw err;
}
}
/**
* Deletes the connections in Floriday
**/
export async function deleteConnections() {
const spinner = ora(`Deleting connections...`).start();
try {
let i = 1;
let headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY
};
let connections = (await vnRequest('GET', `${env.API_URL}/connections`, null, headers)).data;
for (let connection of connections) {
await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`, null, headers);
spinner.text = `Deleting ${i++} connections...`
}
spinner.succeed();
} catch (err) {
spinner.fail();
util.criticalError(err);
}
}
/**
* Perform a REST request
*
* @param {string} url
* @param {string} method
* @param {array} body
* @param {array} header
*
* @return {array}
**/
export async function vnRequest(method = 'GET', url, data = null, headers = {}) {
let response;
for(let i = 0; i < env.MAX_REQUEST_RETRIES; i++) {
try {
if (['GET', 'DELETE'].includes(method))
response = await axios({method, url, headers});
else
response = await axios({method, url, data, headers});
break;
} catch (err) {
if (err.code == 'ECONNRESET')
warning(err)
else
criticalError(err)
await sleep(1000)
}
}
return response;
}
/**
* Critical error
*
* @param {err}
**/
export async function criticalError(err) {
console.log(chalk.red.bold(`[CRITICAL]`), chalk.red(`${err.message}`));
process.exit();
}
/**
* Warning
*
* @param {err}
**/
export async function warning(err) {
console.log(chalk.yellow.bold(`[WARNING]`), chalk.yellow(`${err.message}`));
}