refs #4823 Changes in structure

This commit is contained in:
Guillermo Bonet 2023-04-06 10:56:52 +02:00
parent 69227f3853
commit 5a754967ba
5 changed files with 392 additions and 319 deletions

View File

@ -39,6 +39,8 @@ DB_USER = root
DB_PWD = root DB_PWD = root
DB_HOST = localhost DB_HOST = localhost
DB_DIALECT = mariadb DB_DIALECT = mariadb
DB_TIMEOUT_RECONECT = 30000
DB_MAX_CONN_POOL = 40
#GENERAL CONFIG #GENERAL CONFIG
IS_PRODUCTION = false IS_PRODUCTION = false
@ -46,6 +48,7 @@ SECRETS = true
FORCE_SYNC = true FORCE_SYNC = true
SYNC_SEQUENCE = true SYNC_SEQUENCE = true
SYNC_SUPPLIER = true SYNC_SUPPLIER = true
SYNC_CONN = true
SYNC_TRADEITEM = true SYNC_TRADEITEM = true
``` ```

View File

@ -1,6 +1,7 @@
import * as vnUtils from './utils.js'; import * as utils from './utils.js';
import { models } from './models/index.js'; import { models, checkConn, closeConn } from './models/index.js';
import moment from 'moment'; import moment from 'moment';
import chalk from 'chalk';
// Añade la hora a todos los console.log // Añade la hora a todos los console.log
// console.log = (...args) => console.info(`${new moment().format('HH:mm:ss')} -`, ...args); // console.log = (...args) => console.info(`${new moment().format('HH:mm:ss')} -`, ...args);
@ -8,27 +9,63 @@ const env = process.env;
class Floriday { class Floriday {
async start() { async start() {
try { try {
this.tokenExpirationDate = await vnUtils.getClientToken(models); this.tokenExpirationDate = await utils.requestToken(models);
if (JSON.parse(env.SYNC_SEQUENCE)) await vnUtils.syncSequence() if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence()
if (JSON.parse(env.SYNC_SUPPLIER)) await vnUtils.syncSuppliers(); if (JSON.parse(env.SYNC_SUPPLIER)) await utils.syncSuppliers();
await vnUtils.syncConnections(); if (JSON.parse(env.SYNC_CONN)) await utils.syncConn();
if (JSON.parse(env.SYNC_TRADEITEM)) await vnUtils.syncTradeItems(); if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems();
} catch (err) { } catch (err) {
vnUtils.criticalError(err); utils.criticalError(err);
}
await this.troncal()
}
async tryConn() {
try {
utils.sleep(env.DB_TIMEOUT_RECONECT);
await checkConn();
await this.schedule();
}
catch (err) {
throw new Error(err);
} }
} }
async schedule() { async schedule () {
try {
const intervalTime = JSON.parse(env.IS_PRODUCTION) ? 300000 : 5000;
setInterval(async () => {
try {
await this.troncal();
}
catch (err) {
await this.tryConn();
}
}, intervalTime);
} catch (err) {
throw new Error(err);
}
}
async troncal() {
try{ try{
if (moment().isAfter(await vnUtils.getCurrentTokenExpiration())) { if (moment().isAfter(await utils.getCurrentTokenExpiration())) {
this.tokenExpirationDate = await vnUtils.getClientToken(models); this.tokenExpirationDate = await utils.requestToken(models);
} }
await vnUtils.syncSupplyLines(); await utils.syncSupplyLines();
// Continuar con todo lo que haga falta realizar en el evento
} catch (err) { } catch (err) {
throw(err); throw new Error(err);
} }
} }
async stop() {
await closeConn();
console.log(chalk.dim('Bye, come back soon 👋'))
}
} }
export default Floriday; export default Floriday;

16
main.js
View File

@ -1,20 +1,18 @@
import Floriday from './floriday.js'; import Floriday from './floriday.js';
const env = process.env;
async function main() { async function main() {
const floriday = new Floriday(); const floriday = new Floriday();
await floriday.start(); await floriday.start();
process.on('SIGINT', async function() { process.on('SIGINT', async () => {
console.log('\nBye <3') await floriday.stop();
process.exit(); process.exit();
}); });
await floriday.schedule() try {
const intervalTime = JSON.parse(env.IS_PRODUCTION) ? 300000 : 5000; await floriday.schedule()
setInterval(async () => { } catch (err) {
await floriday.schedule(); await floriday.tryConn();
}, intervalTime); }
} }
main(); main();

View File

@ -16,10 +16,11 @@ console.log(chalk.hex('#06c581')(
` `
)) ))
let sequelize = createConnection(); let sequelize, conSpinner
const conSpinner = ora('Creating connection...').start();
try { try {
await sequelize.authenticate(); conSpinner = ora('Creating connection...').start();
sequelize = createConn();
await checkConn();
conSpinner.succeed(); conSpinner.succeed();
} catch (err) { } catch (err) {
conSpinner.fail(); conSpinner.fail();
@ -86,81 +87,84 @@ let models = {
connection: connections(sequelize), connection: connections(sequelize),
}; };
/* Remove ID atribute from models */ try {
models.characteristic.removeAttribute('id'); /* Remove ID atribute from models */
models.seasonalPeriod.removeAttribute('id'); models.characteristic.removeAttribute('id');
models.package.removeAttribute('id'); models.seasonalPeriod.removeAttribute('id');
models.botanicalName.removeAttribute('id'); models.package.removeAttribute('id');
models.countryOfOriginIsoCode.removeAttribute('id'); models.botanicalName.removeAttribute('id');
/* ------------------------------ */ models.countryOfOriginIsoCode.removeAttribute('id');
/* ------------------------------ */
models.characteristic.belongsTo(models.tradeItem, { models.characteristic.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk', foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk', as: 'tradeItem_Fk',
targetKey: 'tradeItemId', targetKey: 'tradeItemId',
}); });
models.seasonalPeriod.belongsTo(models.tradeItem, { models.seasonalPeriod.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk', foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk', as: 'tradeItem_Fk',
targetKey: 'tradeItemId', targetKey: 'tradeItemId',
}); });
models.photo.belongsTo(models.tradeItem, { models.photo.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk', foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk', as: 'tradeItem_Fk',
targetKey: 'tradeItemId', targetKey: 'tradeItemId',
}); });
models.packingConfiguration.belongsTo(models.tradeItem, { models.packingConfiguration.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk', foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk', as: 'tradeItem_Fk',
targetKey: 'tradeItemId', targetKey: 'tradeItemId',
}); });
models.packingConfiguration.hasMany(models.package, { models.packingConfiguration.hasMany(models.package, {
foreignKey: 'packingConfigurationFk', foreignKey: 'packingConfigurationFk',
as: 'package_Fk', as: 'package_Fk',
targetKey: 'packingConfigurationId', targetKey: 'packingConfigurationId',
}); });
models.package.belongsTo(models.packingConfiguration, { models.package.belongsTo(models.packingConfiguration, {
foreignKey: 'packingConfigurationFk', foreignKey: 'packingConfigurationFk',
as: 'packingConfiguration_Fk', as: 'packingConfiguration_Fk',
targetKey: 'packingConfigurationId', targetKey: 'packingConfigurationId',
}); });
models.botanicalName.belongsTo(models.tradeItem, { models.botanicalName.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk', foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk', as: 'tradeItem_Fk',
targetKey: 'tradeItemId', targetKey: 'tradeItemId',
}); });
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, { models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk', foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk', as: 'tradeItem_Fk',
targetKey: 'tradeItemId', targetKey: 'tradeItemId',
}); });
models.volumePrice.belongsTo(models.supplyLine, { models.volumePrice.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineFk', foreignKey: 'supplyLineFk',
as: 'supplyLine_Fk', as: 'supplyLine_Fk',
targetKey: 'supplyLineId', targetKey: 'supplyLineId',
}); });
models.supplyLine.belongsTo(models.tradeItem, { models.supplyLine.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk', foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk', as: 'tradeItem_Fk',
targetKey: 'tradeItemId', targetKey: 'tradeItemId',
}); });
models.tradeItem.belongsTo(models.supplier, {
foreignKey: 'supplierOrganizationId',
as: 'supplierOrganization_Id',
targetKey: 'organizationId',
});
models.tradeItem.belongsTo(models.supplier, {
foreignKey: 'supplierOrganizationId',
as: 'supplierOrganization_Id',
targetKey: 'organizationId',
});
} catch (err) {
throw new Error(err)
}
let action, isForce; let action, isForce;
if (JSON.parse(process.env.FORCE_SYNC)) { if (JSON.parse(process.env.FORCE_SYNC)) {
@ -195,23 +199,44 @@ catch (err) {
/** /**
* Creates the connection to the database. * Creates the connection to the database.
* *
* @example
* let sequelize = createConnection();
*
* @returns {Sequelize} Sequelize instance with the connection to the database. * @returns {Sequelize} Sequelize instance with the connection to the database.
*/ */
function createConnection() { function createConn() {
return new Sequelize(process.env.DB_SCHEMA, process.env.DB_USER, process.env.DB_PWD, { return new Sequelize(process.env.DB_SCHEMA, process.env.DB_USER, process.env.DB_PWD, {
host: process.env.DB_HOST, host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT, dialect: process.env.DB_DIALECT,
logging: false, logging: false,
pool: { pool: {
max: 40, max: parseInt(process.env.DB_MAX_CONN_POOL),
min: 0,
acquire: 60000, acquire: 60000,
idle: 10000, idle: 10000,
}, },
}); });
} }
export { models } ; /**
* Check if connection is ok
*/
async function checkConn() {
try {
await sequelize.authenticate();
} catch (err) {
throw new Error (err);
}
}
/**
* Close connection
*/
async function closeConn() {
const spinner = ora('Stopping connection...').start();
try {
await sequelize.close()
spinner.succeed();
} catch (err) {
spinner.fail();
criticalError(err)
}
}
export { models, checkConn, closeConn};

434
utils.js
View File

@ -17,7 +17,7 @@ const url = 'https://api.staging.floriday.io/customers-api/2022v2';
* @param {sequelize.models} models * @param {sequelize.models} models
* @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 getClientToken() { export async function requestToken() {
const clientConfigData = await models.clientConfig.findOne(); const clientConfigData = await models.clientConfig.findOne();
if (!clientConfigData) if (!clientConfigData)
@ -41,7 +41,7 @@ export async function getClientToken() {
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`) .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`)
.join('&'); .join('&');
const tokenRequest = await fetch(tokenEndpoint, { const response = await fetch(tokenEndpoint, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
@ -49,26 +49,22 @@ export async function getClientToken() {
body, body,
}); });
const tokenResponse = await tokenRequest.json(); const responseData = await response.json();
if (tokenRequest.ok) { if (response.ok) {
spinner.succeed(); spinner.succeed();
} else { } else {
spinner.fail(); spinner.fail();
criticalError(new Error(`Token request failed with status: ${tokenRequest.status} - ${tokenRequest.statusText}`)); criticalError(new Error(`Token request failed with status: ${response.status} - ${response.statusText}`));
} }
let tokenExpirationDate = moment()
const accessToken = tokenResponse.access_token; .add(responseData.expires_in, 's')
let now = moment().format('YYYY-MM-DD HH:mm:ss');
let tokenExpirationDate = moment(now)
.add(tokenResponse.expires_in, 's')
.format('YYYY-MM-DD HH:mm:ss'); .format('YYYY-MM-DD HH:mm:ss');
await updateClientConfig( await updateClientConfig(
clientId, clientId,
clientSecret, clientSecret,
accessToken, responseData.access_token,
tokenExpirationDate tokenExpirationDate
); );
@ -80,19 +76,28 @@ export async function getClientToken() {
} }
} }
/**
* Returns the current token
*
* @returns {string}
*/
export async function getCurrentToken() { export async function getCurrentToken() {
let data = await models.clientConfig.findOne().currentToken;
return data.currentToken;
}
export async function getCurrentTokenExpiration() {
let data = await models.clientConfig.findOne(); let data = await models.clientConfig.findOne();
return data.tokenExpiration; return data.currentToken
} }
/** /**
* Updates the Access Token in the client config table * Returns the expiration data of current token
*
* @returns {string}
*/
export async function getCurrentTokenExpiration() {
let data = await models.clientConfig.findOne();
return data.tokenExpiration
}
/**
* Updates the Access Token in the client config table
* *
* @param {sequelize.models} models * @param {sequelize.models} models
* @param {String} clientId * @param {String} clientId
@ -119,16 +124,6 @@ export async function updateClientConfig(clientId, clientSecret, accessToken, to
} }
} }
/**
* returns the current Access Token
*
* @returns
*/
export async function getJWT() {
const clientConfigData = await models.clientConfig.findAll();
return clientConfigData[0].currentToken;
}
/** /**
* pauses the execution of the script for the given amount of milliseconds * pauses the execution of the script for the given amount of milliseconds
* *
@ -203,7 +198,7 @@ export async function syncSequence(current = 0, model = null , maximumSequenceNu
spinner.succeed(); spinner.succeed();
} catch (err) { } catch (err) {
spinner.fail(); spinner.fail();
throw(err); throw new Error(err);
} }
} else if (current) { } else if (current) {
@ -248,7 +243,7 @@ export async function syncSuppliers(){
try { try {
let headers = { let headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Bearer ${await getJWT()}`, 'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY 'X-Api-Key': process.env.API_KEY
}; };
@ -256,6 +251,12 @@ export async function syncSuppliers(){
method: 'GET', method: 'GET',
headers headers
}); });
if (!response.ok) {
spinner.fail();
criticalError(new Error(`Max sequence request failed with status: ${response.status} - ${response.statusText}`));
}
const maxSequenceNumber = await response.json(); const maxSequenceNumber = await response.json();
let timeFinish, timeToGoSec, timeToGoMin, timeLeft; let timeFinish, timeToGoSec, timeToGoMin, timeLeft;
for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) { for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
@ -305,65 +306,72 @@ export async function syncSuppliers(){
} }
catch (err) { catch (err) {
spinner.fail(); spinner.fail();
throw(err); throw new Error(err);
} }
} }
export async function syncConnections(){ export async function syncConn(){
let connections = await models.connection.findAll(); const spinner = ora(`Syncing connections...`).start();
try {
let connections = await models.connection.findAll();
let headers = { let headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Bearer ${await getJWT()}`, 'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY 'X-Api-Key': process.env.API_KEY
}; };
let remoteConnections = await fetch(`${url}/connections`, { let remoteConnections = await fetch(`${url}/connections`, {
method: 'GET', method: 'GET',
headers headers
}); });
remoteConnections = await remoteConnections.json(); remoteConnections = await remoteConnections.json();
for (let connection of connections){
if (connection.isConnected == false){
continue;
}
let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.organizationId);
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}`, {
method: 'PUT',
headers
});
await models.connection.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
}
});
await models.supplier.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
}
});
} else {
console.log('Connection: ', connection, 'exists in the remote server');
await models.connection.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
}
});
await models.supplier.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
}
});
let i = 1;
for (let connection of connections){
spinner.text = `Syncing ${i++} connections...`
if (connection.isConnected == false){
continue;
}
let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.organizationId);
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}`, {
method: 'PUT',
headers
});
await models.connection.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
}
});
await models.supplier.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
}
});
} else {
await models.connection.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
}
});
await models.supplier.update({ isConnected: true }, {
where: {
organizationId: connection.organizationId
}
});
}
} }
spinner.succeed();
} catch (err) {
spinner.fail();
throw new Error(err);
} }
} }
export async function syncTradeItems(){ export async function syncTradeItems(){
@ -378,7 +386,7 @@ export async function syncTradeItems(){
let query = `${url}/trade-items?supplierOrganizationId=${supplier.organizationId}`; let query = `${url}/trade-items?supplierOrganizationId=${supplier.organizationId}`;
let headers = { let headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Bearer ${await getJWT()}`, 'Authorization': `Bearer ${await getCurrentToken()}`,
'X-Api-Key': process.env.API_KEY 'X-Api-Key': process.env.API_KEY
}; };
try { try {
@ -391,7 +399,6 @@ export async function syncTradeItems(){
let tradeItems = await request.json(); let tradeItems = await request.json();
if (!tradeItems.length) { if (!tradeItems.length) {
console.log('No trade items for supplier: ', supplier.commercialName);
continue; continue;
} }
@ -419,119 +426,85 @@ export async function syncTradeItems(){
* to do this, it fetches all the supply lines for every tradeitem of the suppliers * to do this, it fetches all the supply lines for every tradeitem of the suppliers
*/ */
export async function syncSupplyLines(){ export async function syncSupplyLines(){
try {
let currentSequenceNumber = await models.sequenceNumber.findOne({ let currentSequenceNumber = await models.sequenceNumber.findOne({ // TODO: Mirar como manejar este error
where: { where: {
model: 'supplyLines' model: 'supplyLines'
}
});
const spinner = ora(`Syncing trade items...`).start();
let suppliers = await models.supplier.findAll({
where: {
isConnected: true
}
});
let tradeItems = await models.tradeItem.findAll({
where: {
supplierOrganizationId: suppliers.map(supplier => supplier.organizationId)
}
});
let promises = [];
let headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${await getJWT()}`,
'X-Api-Key': process.env.API_KEY
};
// 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`
const params = new URLSearchParams({
supplierOrganizationId: supplier.organizationId,
tradeItemId: tradeItem.tradeItemId,
postFilterSelectedTradeItems: false
});
url.search = params;
let request = await fetch(url.toString(), {
method: 'GET',
headers
});
let supplyLines = await request.json();
if (supplyLines.length == 0) {
console.log('No supply lines for supplier: ', supplier.commercialName, ' - ' , tradeItem.name);
resolve([]);
return;
}
resolve(supplyLines);
} catch (error) {
console.log('Error while syncing supply lines for: ', supplier.commercialName, ' - ' , tradeItem.name);
console.log(error);
resolve([]);
} }
}); });
promises.push(promise); const spinner = ora(`Syncing trade items...`).start();
let suppliers = await models.supplier.findAll({
where: {
isConnected: true
}
});
} let tradeItems = await models.tradeItem.findAll({
where: {
supplierOrganizationId: suppliers.map(supplier => supplier.organizationId)
}
});
let supplyLines = await Promise.all(promises); let promises = [];
let maximumSequenceNumber;
for (let supplyLine of supplyLines) { let headers = {
maximumSequenceNumber = supplyLine.maximumSequenceNumber; 'Content-Type': 'application/json',
supplyLine = supplyLine.results; 'Authorization': `Bearer ${await getCurrentToken()}`,
try { 'X-Api-Key': process.env.API_KEY
for (let line of supplyLine) { };
let tradeItem = await models.tradeItem.findOne({ // Launch a promise for each supplier
where: { for (let tradeItem of tradeItems) {
tradeItemId: line.tradeItemId 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`
const params = new URLSearchParams({
supplierOrganizationId: supplier.organizationId,
tradeItemId: tradeItem.tradeItemId,
postFilterSelectedTradeItems: false
});
url.search = params;
if (!tradeItem) { let request = await fetch(url.toString(), {
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 queryTradeItem = await fetch(urlTradeItem, {
method: 'GET', method: 'GET',
headers headers
}); });
let tradeItem = await queryTradeItem.json(); let supplyLines = await request.json();
if (tradeItem.length == 0) { if (supplyLines.length == 0) {
console.log('No supply lines for supplier: ', supplier.commercialName, ' - ' , tradeItem.name);
console.log('Trade item not found for supply line: ', line.supplyLineId); resolve([]);
console.log('Trade item id: ', line.tradeItemId); return;
continue;
} }
let supplier = await models.supplier.findOne({ resolve(supplyLines);
where: {
organizationId: tradeItem[0].supplierOrganizationId
}
});
await insertItem(tradeItem[0], supplier); } catch (error) {
console.log('Error while syncing supply lines for: ', supplier.commercialName, ' - ' , tradeItem.name);
console.log(error);
resolve([]);
}
});
tradeItem = await models.tradeItem.findOne({ promises.push(promise);
}
let supplyLines = await Promise.all(promises);
let maximumSequenceNumber;
for (let supplyLine of supplyLines) {
maximumSequenceNumber = supplyLine.maximumSequenceNumber;
supplyLine = supplyLine.results;
try {
for (let line of supplyLine) {
let tradeItem = await models.tradeItem.findOne({
where: { where: {
tradeItemId: line.tradeItemId tradeItemId: line.tradeItemId
} }
@ -539,44 +512,81 @@ export async function syncSupplyLines(){
if (!tradeItem) { if (!tradeItem) {
console.log('Trade item not found for supply line: ', line.supplyLineId); console.log('Trade item not found for supply line: ', line.supplyLineId);
console.log('Trade item id: ', line.tradeItemId); console.log('Requesting data for trade item id: ', line.tradeItemId);
continue;
let urlTradeItem = `${url}/trade-items?tradeItemIds=${line.tradeItemId}`;
let queryTradeItem = await fetch(urlTradeItem, {
method: 'GET',
headers
});
let tradeItem = await queryTradeItem.json();
if (tradeItem.length == 0) {
console.log('Trade item not found for supply line: ', line.supplyLineId);
console.log('Trade item id: ', line.tradeItemId);
continue;
}
let supplier = await models.supplier.findOne({
where: {
organizationId: tradeItem[0].supplierOrganizationId
}
});
await insertItem(tradeItem[0], supplier);
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: line.pricePerPiece,
numberOfPieces: line.numberOfPieces,
deliveryPeriod: line.deliveryPeriod,
orderPeriod: line.orderPeriod,
wharehouseId: line.wharehouseId,
sequenceNumber: line.sequenceNumber,
type: line.type,
isDeleted: line.isDeleted,
salesUnit: line.salesUnit,
agreementReferenceCode: line.agreementReference.code,
agreementReferenceDescription: line.agreementReference.description,
isLimited: line.isLimited,
isCustomerSpecific: line.isCustomerSpecific,
tradeItemFk: line.tradeItemId,
});
} }
} catch (err) {
await models.supplyLine.upsert({ spinner.fail();
supplyLineId: line.supplyLineId, console.log('Error while syncing supply lines - ' + supplyLine.results[0].tradeItemId);
status: line.status, throw new Error(err);
supplierOrganizationId: line.supplierOrganizationId,
pricePerPiece: line.pricePerPiece,
numberOfPieces: line.numberOfPieces,
deliveryPeriod: line.deliveryPeriod,
orderPeriod: line.orderPeriod,
wharehouseId: line.wharehouseId,
sequenceNumber: line.sequenceNumber,
type: line.type,
isDeleted: line.isDeleted,
salesUnit: line.salesUnit,
agreementReferenceCode: line.agreementReference.code,
agreementReferenceDescription: line.agreementReference.description,
isLimited: line.isLimited,
isCustomerSpecific: line.isCustomerSpecific,
tradeItemFk: line.tradeItemId,
});
} }
} catch (error) {
spinner.fail();
console.log('Error while syncing supply lines - ' + supplyLine.results[0].tradeItemId);
console.log(error);
} }
} spinner.succeed();
spinner.succeed(); console.log('Found', suppliers.length, 'connected suppliers');
console.log('Found', suppliers.length, 'connected suppliers');
await syncSequence(currentSequenceNumber,'supplyLines' ,maximumSequenceNumber); await syncSequence(currentSequenceNumber,'supplyLines' ,maximumSequenceNumber);
} catch (err) {
throw new Error(err);
}
} }
export async function insertItem(tradeItem, supplier) { export async function insertItem(tradeItem, supplier) {