From 090c0507297bfe3022ed8cfcba39b8925d5b57cf Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 24 Apr 2023 14:11:25 +0200 Subject: [PATCH] refs #4823 Added variables API_URL && API_ENDPOINT --- README.md | 2 ++ floriday.js | 4 ++-- utils.js | 30 +++++++++++------------------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index abbd904..38c226a 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ npm i CLIENT_ID = xxxxxxxxxx CLIENT_SECRET = xxxxxxxxxx API_KEY = xxxxxxxxxx +API_URL = https://api.staging.floriday.io/customers-api/2022v2 +API_ENDPOINT = https://idm.staging.floriday.io/oauth2/ausmw6b47z1BnlHkw0h7/v1/token #SEQUELIZE CONFIG DB_SCHEMA = schema diff --git a/floriday.js b/floriday.js index bd98762..90c2856 100644 --- a/floriday.js +++ b/floriday.js @@ -10,14 +10,14 @@ class Floriday { async start() { try { this.tokenExpirationDate = await utils.requestToken(models); - 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_CONN)) await utils.syncConn(); if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems(); } catch (err) { utils.criticalError(err); } - await this.trunk() + await this.trunk(); } async tryConn() { diff --git a/utils.js b/utils.js index facdac9..c2a8fdb 100644 --- a/utils.js +++ b/utils.js @@ -5,12 +5,6 @@ import { v4 as uuidv4 } from 'uuid'; import chalk from 'chalk'; import ora from 'ora'; -// The Endpoint where the Token is requested -const tokenEndpoint = 'https://idm.staging.floriday.io/oauth2/ausmw6b47z1BnlHkw0h7/v1/token'; - -// URL of API -const url = 'https://api.staging.floriday.io/customers-api/2022v2'; - /** * Gets the Access Token from the client config table * @@ -41,7 +35,7 @@ export async function requestToken() { .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`) .join('&'); - const response = await fetch(tokenEndpoint, { + const response = await fetch(env.API_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', @@ -247,7 +241,7 @@ export async function syncSuppliers(){ 'X-Api-Key': process.env.API_KEY }; - const response = await fetch(`${url}/organizations/current-max-sequence`, { + const response = await fetch(`${env.API_URL}/organizations/current-max-sequence`, { method: 'GET', headers }); @@ -262,7 +256,7 @@ export async function syncSuppliers(){ for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) { let timeStart = new moment(); - let query = `${url}/organizations/sync/${curSequenceNumber}?organizationType=SUPPLIER`; + let query = `${env.API_URL}/organizations/sync/${curSequenceNumber}?organizationType=SUPPLIER`; let response = await fetch(query, { method: 'GET', headers @@ -302,6 +296,7 @@ export async function syncSuppliers(){ else timeLeft = `${timeToGoMin} min` } + spinner.text = `Syncing suppliers...`; spinner.succeed() } catch (err) { @@ -321,7 +316,7 @@ export async function syncConn(){ 'X-Api-Key': process.env.API_KEY }; - let remoteConnections = await fetch(`${url}/connections`, { + let remoteConnections = await fetch(`${env.API_URL}/connections`, { method: 'GET', headers }); @@ -339,7 +334,7 @@ export async function syncConn(){ if (remoteConnection == undefined){ console.log('Connection: ', connection, 'does not exist in the remote server'); console.log('Creating remote connection'); - await fetch(`${url}/connections/${connection.organizationId}`, { + await fetch(`${env.API_URL}/connections/${connection.organizationId}`, { method: 'PUT', headers }); @@ -381,7 +376,7 @@ export async function syncTradeItems(){ for (let supplier of suppliers) { if (!supplier.isConnected) continue; - let query = `${url}/trade-items?supplierOrganizationId=${supplier.organizationId}`; + let query = `${env.API_URL}/trade-items?supplierOrganizationId=${supplier.organizationId}`; let headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${await getCurrentToken()}`, @@ -451,23 +446,20 @@ export async function syncSupplyLines(){ // Launch a promise for each supplier for (let tradeItem of tradeItems) { let supplier = suppliers.find(supplier => supplier.organizationId == tradeItem.supplierOrganizationId); - console.log('Requesting supply lines for ' + supplier.commercialName + ' - ' + tradeItem.name); // eslint-disable-next-line no-async-promise-executor let promise = new Promise(async (resolve) => { try { - let url = `${url}/supply-lines/sync/0` + let url = `${env.API_URL}/supply-lines/sync/0` const params = new URLSearchParams({ supplierOrganizationId: supplier.organizationId, tradeItemId: tradeItem.tradeItemId, postFilterSelectedTradeItems: false }); - url.search = params; - - let request = await fetch(url.toString(), { + let request = await fetch(`${url}?${params.toString()}`, { method: 'GET', headers }); - + let supplyLines = await request.json(); if (supplyLines.length == 0) { @@ -508,7 +500,7 @@ export async function syncSupplyLines(){ console.log('Trade item not found for supply line: ', line.supplyLineId); console.log('Requesting data for trade item id: ', line.tradeItemId); - let urlTradeItem = `${url}/trade-items?tradeItemIds=${line.tradeItemId}`; + let urlTradeItem = `${env.API_URL}/trade-items?tradeItemIds=${line.tradeItemId}`; let queryTradeItem = await fetch(urlTradeItem, { method: 'GET',