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. * * @param {Boolean} isForce Force to request new token */ export async function requestToken(isForce = false) { let spinner = ora(`Requesting new token...`).start(); try { const clientConfigData = await models.clientConfig.findOne(); let tokenExpiration, token; if (clientConfigData) { token = clientConfigData.currentToken; tokenExpiration = clientConfigData.tokenExpiration; } if (isForce || !token || !tokenExpiration || moment().isAfter(tokenExpiration)) { const clientId = JSON.parse(env.USE_SECRETS_DB) ? clientConfigData.clientId || env.CLIENT_ID : env.CLIENT_ID; const clientSecret = JSON.parse(env.USE_SECRETS_DB) ? clientConfigData.clientSecret || env.CLIENT_SECRET : env.CLIENT_SECRET; const data = new URLSearchParams({ grant_type: 'client_credentials', client_id: clientId, client_secret: clientSecret, scope: 'role:app catalog:read supply:read organization:read network:write network:read' }).toString(); const headers = { 'Content-Type': 'application/x-www-form-urlencoded' }; const response = (await vnRequest('POST', env.API_ENDPOINT, data, headers)).data; const tokenExpiration = moment() .add(response.expires_in, 's') .format('YYYY-MM-DD HH:mm:ss'); await updateClientConfig({ clientId, clientSecret, currentToken: response.access_token, tokenExpiration, }); spinner.succeed(); } else spinner.succeed('Using stored token...'); } catch (err) { spinner.fail(); throw err; } } /** * Returns the current token. * * @returns {string} */ export async function getCurrentToken() { 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 {Array} clientConfig [clientId, clientSecret, currenToken, tokenExpiration] */ export async function updateClientConfig(clientConfig) { try { if (!JSON.parse(process.env.USE_SECRETS_DB)) clientId = clientSecret = null await models.clientConfig.upsert({ id: 1, ...clientConfig, }); } catch (err) { throw(err); } } /** * Pauses the execution of the script for the specified number of milliseconds. * * @param {Integer} ms */ export async function sleep(ms) { await new Promise(resolve => setTimeout(resolve, ms)); } /** * Sync the organizations. */ export async function syncOrganizations(){ let spinner = ora('Syncing organizations...').start(); let i = 1; try { const maxSequenceNumber = (await vnRequest('GET', `${env.API_URL}/organizations/current-max-sequence`)).data; for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) { const params = new URLSearchParams({ organizationType: 'SUPPLIER' }).toString(); let response = (await vnRequest('GET', `${env.API_URL}/organizations/sync/${curSequenceNumber}?${params}`)).data; curSequenceNumber = response.maximumSequenceNumber; const orgs = response.results; for (let org of orgs) { spinner.text = `Syncing ${i} organizations, ${maxSequenceNumber - curSequenceNumber} missing...` if (JSON.parse(env.APPLY_ORG_FILTER) && org.companyGln && !org.endDate) { // Filtro para quitar los que parecen test await insertOrganization(org); spinner.text = `Syncing ${i++} organizations, ${maxSequenceNumber - curSequenceNumber} missing...` } }; } spinner.succeed(); } catch (err) { spinner.fail(); throw new Error(err); } } /** * Create the connections in Floriday. */ export async function syncConnections(){ await deleteConnections(); let spinner; try { let connectionsInDb = await models.supplyLine.findAll({ include : { model: models.organization, where: { isConnected: true, } }, attributes: ['organizationId'], group: ['organizationId'], }); const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`)).data; let isExists = false, connectionsToPut = []; for (let connectionInDb of connectionsInDb) { for (let connectionInFloriday of connectionsInFloriday) if (connectionInFloriday == connectionInDb.organizationId) { isExists = true; break; } if (!isExists) connectionsToPut.push(connectionInDb.organizationId) isExists = false; } if (connectionsToPut.length) spinner = ora(`Creating connections in Floriday...`).start(); let i = 1; for (let connection of connectionsToPut) { spinner.text = `Creating ${i++} of ${connectionsToPut.length} connections in Floriday...` await vnRequest('PUT', `${env.API_URL}/connections/${connection}`); } if (spinner) spinner.succeed(); } catch (err) { if (spinner) spinner.fail(); throw new Error(err); } } /** * Sync the trade items for organizations that are connected. */ export async function syncTradeItems(){ const spinner = ora(`Syncing trade items...`).start(); const orgs = await models.organization.findAll({ attributes: ['organizationId'], where: { isConnected: true } }); let i = 0, x = 0; for (let org of orgs) { try { const params = new URLSearchParams({ supplierOrganizationId: org.organizationId, }).toString(); let tradeItems = (await vnRequest('GET', `${env.API_URL}/trade-items?${params}`)).data spinner.text = `Syncing ${i} trade items of [${x++}|${orgs.length}] organizations...` if (!tradeItems.length) continue; for (let tradeItem of tradeItems) { await insertItem(tradeItem); spinner.text = `Syncing ${i++} trade items of [${x}|${orgs.length}] organizations...` }; } catch (err) { spinner.fail(); throw err; } } spinner.succeed() } /** * Sync the supply lines for organizations that are connected. * * If necessary, create the dependences. */ export async function syncSupplyLines() { const spinner = ora(`Syncing supply lines...`).start(); try { let conOrgs = await models.organization.findAll({ attributes: ['organizationId'], where: { isConnected: true } }); let i = 0, x = 1; for (let org of conOrgs) { spinner.text = `Syncing ${i} supply lines of [${x++}|${conOrgs.length}] organizations...` const params = new URLSearchParams({ supplierOrganizationId: org.organizationId, }).toString(); let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`)).data; if (!supplyLines.length) continue for (let supplyLine of supplyLines) { // Check if the warehouse exists, and if it doesn't, create it let warehouse = await models.warehouses.findOne({ where: { warehouseId: supplyLine.warehouseId } }); if (!warehouse) { let warehouse = (await vnRequest('GET', `${env.API_URL}/warehouses/${supplyLine.warehouseId}`)).data; // Check if the organization exists, and if it doesn't, create it let organization = await models.organization.findOne({ where: { organizationId: warehouse.organizationId } }); if (!organization) { let organization = (await vnRequest('GET', `${env.API_URL}/organizations/${warehouse.organizationId}`)).data; await insertOrganization(organization); } await insertWarehouse(warehouse); } // Check if the trade item exists, and if it doesn't, create it let tradeItem = await models.tradeItem.findOne({ where: { tradeItemId: supplyLine.tradeItemId } }); if (!tradeItem) { let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items/${supplyLine.tradeItemId}`)).data; await insertItem(tradeItem); } spinner.text = `Syncing ${i++} supply lines of [${x}|${conOrgs.length}] organizations...` await models.supplyLine.upsert({ ...supplyLine, organizationId: supplyLine.supplierOrganizationId, pricePerPiece_currency: supplyLine.pricePerPiece?.currency ?? null, pricePerPiece_value: supplyLine.pricePerPiece?.value ?? null, deliveryPeriod_startDateTime: supplyLine.deliveryPeriod?.startDateTime ?? null, deliveryPeriod_endDateTime: supplyLine.deliveryPeriod?.endDateTime ?? null, orderPeriod_startDateTime: supplyLine.orderPeriod?.startDateTime ?? null, orderPeriod_endDateTime: supplyLine.orderPeriod?.endDateTime ?? null, agreementReference_code: supplyLine.agreementReference?.code ?? null, agreementReference_description: supplyLine.agreementReference?.description ?? null, lastSync: moment(), }); for (let volumePrice of supplyLine.volumePrices) await models.volumePrices.upsert({ supplyLineId: supplyLine.supplyLineId, ...volumePrice, }); } } spinner.succeed(); } catch (err) { spinner.fail(); throw err; } } /** * Insert trade item and dependences in the database. * * @param {Array} tradeItem */ export async function insertItem(tradeItem) { const tx = await models.sequelize.transaction(); try { // Upsert trade item await models.tradeItem.upsert({ ...tradeItem, organizationId: tradeItem.supplierOrganizationId, lastSync: moment(), }, { transaction: tx }); // Upsert characteristics if (tradeItem.characteristics) if (tradeItem.characteristics.length) for (const characteristic of tradeItem.characteristics) { await models.characteristic.upsert({ tradeItemId: tradeItem.tradeItemId, ...characteristic, }, { transaction: tx }); } // Upsert seasonal periods if (tradeItem.seasonalPeriods) if (tradeItem.seasonalPeriods.length) for (const seasonalPeriod of tradeItem.seasonalPeriods) { await models.seasonalPeriod.upsert({ tradeItemId: tradeItem.tradeItemId, ...seasonalPeriod, }, { transaction: tx }); } // Upsert photos if (tradeItem.photos) if (tradeItem.photos.length) for (const photo of tradeItem.photos) { await models.photo.upsert({ ...photo, tradeItemId: tradeItem.tradeItemId, }, { transaction: tx }); } // Upsert packing configurations if (tradeItem.packingConfigurations) if (tradeItem.packingConfigurations.length) for (const packingConfiguration of tradeItem.packingConfigurations) { const uuid = uuidv4(); await models.packingConfiguration.upsert({ packingConfigurationId: uuid, ...packingConfiguration, additionalPricePerPiece_currency: packingConfiguration.additionalPricePerPiece.currency, additionalPricePerPiece_value: packingConfiguration.additionalPricePerPiece.value, tradeItemId: tradeItem.tradeItemId, }, { transaction: tx }); await models.package.upsert({ ...packingConfiguration.package, packingConfigurationId: uuid, }, { transaction: tx }); } // Upsert country of origin ISO codes if (tradeItem.countryOfOriginIsoCodes) if (tradeItem.countryOfOriginIsoCodes.length) for (const isoCode of tradeItem.countryOfOriginIsoCodes || []) { await models.countryOfOriginIsoCode.upsert({ isoCode, tradeItemId: tradeItem.tradeItemId, }, { transaction: tx }); } // Upsert botanical names if (tradeItem.botanicalNames) if (tradeItem.botanicalNames.length) for (const botanicalName of tradeItem.botanicalNames) { await models.botanicalName.upsert({ botanicalNameId: uuidv4(), name: botanicalName, tradeItemId: tradeItem.tradeItemId, }, { transaction: tx }); } await tx.commit(); } catch (err) { await tx.rollback(); throw err; } } /** * Insert warehouse in the database. * * @param {Array} warehouse */ export async function insertWarehouse(warehouse) { const tx = await models.sequelize.transaction(); try { await models.warehouses.upsert({ ...warehouse, location_gln: warehouse.location.gln, location_address_addressLine: warehouse.location.address.addressLine, location_address_city: warehouse.location.address.city, location_address_countryCode: warehouse.location.address.countryCode, location_address_postalCode: warehouse.location.address.postalCode, location_address_stateOrProvince: warehouse.location.address.stateOrProvince, lastSync: moment(), }); await tx.commit(); } catch (err) { await tx.rollback(); throw err; } } /** * Insert organization in the database. * * @param {Array} organization */ export async function insertOrganization(organization) { const tx = await models.sequelize.transaction(); try { await models.organization.upsert({ ...organization, isConnected: JSON.parse(env.ORGS_ALWAYS_CONN), lastSync: moment(), }); await tx.commit(); } catch (err) { await tx.rollback(); throw err; } } /** * Sync the warehouses for organizations that are connected. **/ export async function syncWarehouses(){ let spinner = ora('Syncing warehouses...').start(); try { const orgs = await models.organization.findAll({ where: { isConnected: true } }); let x = 0, i = 0; for (let org of orgs) { spinner.text = `Syncing ${i} warehouses of [${x++}|${orgs.length}] organizations...` const warehouses = (await vnRequest('GET', `${env.API_URL}/organizations/supplier/${org.organizationId}/warehouses`)).data; for (let warehouse of warehouses) { spinner.text = `Syncing ${i++} warehouses of [${x}|${orgs.length}] organizations...` await insertWarehouse(warehouse); } } spinner.succeed(); } catch (err) { spinner.fail(); throw new Error(err); } } /** * Removes Floriday connections that we don't have in the database. **/ export async function deleteConnections() { let spinner; try { let i = 1; const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`)).data; const connectionsInDb = await models.organization.findAll({ attributes: ['organizationId'], where: { isConnected: true } }); let isExists = false, ghostConnections = []; for (let connectionInFloriday of connectionsInFloriday) { for (let connectionInDb of connectionsInDb) if (connectionInFloriday == connectionInDb.organizationId) { isExists = true; break; } if (!isExists) ghostConnections.push(connectionInFloriday) isExists = false; } if (ghostConnections.length) spinner = ora(`Deleting connections that aren't in the db...`).start(); for (let connection of ghostConnections) { await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`); spinner.text = `Deleting ${i++} of ${ghostConnections.length} that aren't in the db...` } if (spinner) spinner.succeed(); } catch (err) { if (spinner) spinner.fail(); 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, url, data, headers) { if (!headers) headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${await getCurrentToken()}`, 'X-Api-Key': process.env.API_KEY, }; while(true) { try { return (['GET', 'DELETE'].includes(method)) ? await axios({method, url, headers}) : await axios({method, url, data, headers}); } catch (err) { switch (err.code) { case 'ECONNRESET': // Client network socket TLS case 'EAI_AGAIN': // getaddrinfo warning(err); await sleep(1000); break; case 'ECONNABORTED': case 'ECONNREFUSED': case 'ERR_BAD_REQUEST': switch (err.response.status) { case 504: case 502: warning(err); await sleep(1000); break; case 429: // Too Many Requests warning(err); await sleep(3400); // Stipulated by Floriday break; case 401: // Unauthorized warning(err); await requestToken(true); headers.Authorization ? headers.Authorization = `Bearer ${await getCurrentToken()}` : criticalError(err); break; default: warning(err); await sleep(env.MS_RETRY_UNHANDLED_ERROR); break; } break; default: warning(err); await sleep(env.MS_RETRY_UNHANDLED_ERROR); break; } } } } /** * 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) { (err.response?.status && err.response?.data?.message) ? (console.log(chalk.yellow.bold(`[WARNING]`), chalk.yellow(`${err.response.status} - ${err.response.data.message}`))) : (console.log(chalk.yellow.bold(`[WARNING]`), chalk.yellow(err.message))); }