refs #4823 Added variables API_URL && API_ENDPOINT

This commit is contained in:
Guillermo Bonet 2023-04-24 14:11:25 +02:00
parent 90a27c8691
commit 090c050729
3 changed files with 15 additions and 21 deletions

View File

@ -32,6 +32,8 @@ npm i
CLIENT_ID = xxxxxxxxxx CLIENT_ID = xxxxxxxxxx
CLIENT_SECRET = xxxxxxxxxx CLIENT_SECRET = xxxxxxxxxx
API_KEY = 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 #SEQUELIZE CONFIG
DB_SCHEMA = schema DB_SCHEMA = schema

View File

@ -10,14 +10,14 @@ class Floriday {
async start() { async start() {
try { try {
this.tokenExpirationDate = await utils.requestToken(models); 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_SUPPLIER)) await utils.syncSuppliers();
if (JSON.parse(env.SYNC_CONN)) await utils.syncConn(); if (JSON.parse(env.SYNC_CONN)) await utils.syncConn();
if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems(); if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems();
} catch (err) { } catch (err) {
utils.criticalError(err); utils.criticalError(err);
} }
await this.trunk() await this.trunk();
} }
async tryConn() { async tryConn() {

View File

@ -5,12 +5,6 @@ import { v4 as uuidv4 } from 'uuid';
import chalk from 'chalk'; import chalk from 'chalk';
import ora from 'ora'; 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 * Gets the Access Token from the client config table
* *
@ -41,7 +35,7 @@ export async function requestToken() {
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`) .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`)
.join('&'); .join('&');
const response = await fetch(tokenEndpoint, { const response = await fetch(env.API_ENDPOINT, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
@ -247,7 +241,7 @@ export async function syncSuppliers(){
'X-Api-Key': process.env.API_KEY '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', method: 'GET',
headers headers
}); });
@ -262,7 +256,7 @@ export async function syncSuppliers(){
for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) { for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
let timeStart = new moment(); 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, { let response = await fetch(query, {
method: 'GET', method: 'GET',
headers headers
@ -302,6 +296,7 @@ export async function syncSuppliers(){
else else
timeLeft = `${timeToGoMin} min` timeLeft = `${timeToGoMin} min`
} }
spinner.text = `Syncing suppliers...`;
spinner.succeed() spinner.succeed()
} }
catch (err) { catch (err) {
@ -321,7 +316,7 @@ export async function syncConn(){
'X-Api-Key': process.env.API_KEY 'X-Api-Key': process.env.API_KEY
}; };
let remoteConnections = await fetch(`${url}/connections`, { let remoteConnections = await fetch(`${env.API_URL}/connections`, {
method: 'GET', method: 'GET',
headers headers
}); });
@ -339,7 +334,7 @@ export async function syncConn(){
if (remoteConnection == undefined){ if (remoteConnection == undefined){
console.log('Connection: ', connection, 'does not exist in the remote server'); console.log('Connection: ', connection, 'does not exist in the remote server');
console.log('Creating remote connection'); console.log('Creating remote connection');
await fetch(`${url}/connections/${connection.organizationId}`, { await fetch(`${env.API_URL}/connections/${connection.organizationId}`, {
method: 'PUT', method: 'PUT',
headers headers
}); });
@ -381,7 +376,7 @@ export async function syncTradeItems(){
for (let supplier of suppliers) { for (let supplier of suppliers) {
if (!supplier.isConnected) continue; if (!supplier.isConnected) continue;
let query = `${url}/trade-items?supplierOrganizationId=${supplier.organizationId}`; let query = `${env.API_URL}/trade-items?supplierOrganizationId=${supplier.organizationId}`;
let headers = { let headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Bearer ${await getCurrentToken()}`, 'Authorization': `Bearer ${await getCurrentToken()}`,
@ -451,23 +446,20 @@ export async function syncSupplyLines(){
// Launch a promise for each supplier // Launch a promise for each supplier
for (let tradeItem of tradeItems) { for (let tradeItem of tradeItems) {
let supplier = suppliers.find(supplier => supplier.organizationId == tradeItem.supplierOrganizationId); 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 // eslint-disable-next-line no-async-promise-executor
let promise = new Promise(async (resolve) => { let promise = new Promise(async (resolve) => {
try { try {
let url = `${url}/supply-lines/sync/0` let url = `${env.API_URL}/supply-lines/sync/0`
const params = new URLSearchParams({ const params = new URLSearchParams({
supplierOrganizationId: supplier.organizationId, supplierOrganizationId: supplier.organizationId,
tradeItemId: tradeItem.tradeItemId, tradeItemId: tradeItem.tradeItemId,
postFilterSelectedTradeItems: false postFilterSelectedTradeItems: false
}); });
url.search = params; let request = await fetch(`${url}?${params.toString()}`, {
let request = await fetch(url.toString(), {
method: 'GET', method: 'GET',
headers headers
}); });
let supplyLines = await request.json(); let supplyLines = await request.json();
if (supplyLines.length == 0) { 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('Trade item not found for supply line: ', line.supplyLineId);
console.log('Requesting data for trade item id: ', line.tradeItemId); 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, { let queryTradeItem = await fetch(urlTradeItem, {
method: 'GET', method: 'GET',