2023-01-09 11:58:34 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
import fetch from 'node-fetch';
|
2023-01-10 12:24:43 +00:00
|
|
|
import dotenv from 'dotenv';
|
2023-01-11 12:13:22 +00:00
|
|
|
import { models } from './models/index.js';
|
2023-02-08 11:31:54 +00:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2023-02-03 12:56:34 +00:00
|
|
|
//import cliProgress from 'cli-progress';
|
2023-01-10 12:24:43 +00:00
|
|
|
dotenv.config();
|
2023-01-11 13:37:46 +00:00
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
/**
|
|
|
|
* The Endpoint where the Access Token is requested
|
|
|
|
*/
|
|
|
|
const _accessTokenEndpoint = 'https://idm.staging.floriday.io/oauth2/ausmw6b47z1BnlHkw0h7/v1/token';
|
2023-01-09 11:58:34 +00:00
|
|
|
|
2023-01-16 13:52:08 +00:00
|
|
|
const BASE_CUSTOMER_URL = 'https://api.staging.floriday.io/customers-api/2022v2/';
|
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
/**
|
|
|
|
* Gets the Access Token from the client config table
|
|
|
|
*
|
|
|
|
* @param {sequelize.models} models
|
|
|
|
* @returns {Date} tokenExpirationDate formated as YYYY-MM-DD HH:mm:ss
|
|
|
|
*/
|
2023-01-10 12:24:43 +00:00
|
|
|
async function getClientToken() {
|
2023-01-09 11:58:34 +00:00
|
|
|
const clientConfigData = await models.clientConfig.findAll();
|
|
|
|
|
|
|
|
const now = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
const tokenExpirationDate = clientConfigData[0].tokenExpiration;
|
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
if (clientConfigData[0].tokenExpiration == null || moment(now).isAfter(tokenExpirationDate)) {
|
2023-01-09 11:58:34 +00:00
|
|
|
let clientId = clientConfigData[0].clientId;
|
|
|
|
let clientSecret = clientConfigData[0].clientSecret;
|
|
|
|
|
|
|
|
const tokenRequest = await fetch(_accessTokenEndpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
2023-02-01 12:23:06 +00:00
|
|
|
body: `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}&scope=role:app catalog:read supply:read organization:read network:write network:read`,
|
2023-01-09 11:58:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const tokenResponse = await tokenRequest.json();
|
|
|
|
|
|
|
|
if (tokenRequest.status === 200) {
|
|
|
|
console.log('Token request successful');
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`Token request failed with status ${tokenRequest.status}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const accessToken = tokenResponse.access_token;
|
|
|
|
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');
|
|
|
|
|
2023-01-10 12:24:43 +00:00
|
|
|
await updateClientConfig(
|
2023-01-09 11:58:34 +00:00
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
accessToken,
|
|
|
|
tokenExpirationDate
|
|
|
|
);
|
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
return tokenExpirationDate;
|
2023-01-09 11:58:34 +00:00
|
|
|
} else {
|
|
|
|
console.log('Using the current token...');
|
2023-01-09 12:35:05 +00:00
|
|
|
return tokenExpirationDate;
|
2023-01-09 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 12:35:05 +00:00
|
|
|
/**
|
|
|
|
* Updates the Access Token in the client config table
|
|
|
|
*
|
|
|
|
* @param {sequelize.models} models
|
|
|
|
* @param {String} clientId
|
|
|
|
* @param {String} clientSecret
|
|
|
|
* @param {String} accessToken
|
|
|
|
* @param {String} tokenExpirationDate
|
|
|
|
*/
|
2023-01-10 12:24:43 +00:00
|
|
|
async function updateClientConfig(clientId, clientSecret, accessToken, tokenExpirationDate) {
|
2023-01-09 11:58:34 +00:00
|
|
|
try {
|
|
|
|
console.log('Updating the client config with the new token...');
|
2023-02-07 08:45:04 +00:00
|
|
|
await models.clientConfig.upsert({
|
|
|
|
id: 1,
|
|
|
|
clientId: clientId,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
currentToken: accessToken,
|
|
|
|
tokenExpiration: tokenExpirationDate,
|
|
|
|
requestLimit: 500,
|
2023-02-07 06:50:05 +00:00
|
|
|
});
|
2023-01-09 11:58:34 +00:00
|
|
|
console.log('Client config updated, new Token set');
|
|
|
|
console.log('New token expiration date: ', tokenExpirationDate);
|
|
|
|
} catch (error) {
|
|
|
|
console.log('There was a error while updating the client config');
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 12:24:43 +00:00
|
|
|
/**
|
|
|
|
* returns the current Access Token
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
async function getJWT() {
|
|
|
|
const clientConfigData = await models.clientConfig.findAll();
|
|
|
|
return clientConfigData[0].currentToken;
|
|
|
|
}
|
|
|
|
|
2023-01-11 12:13:22 +00:00
|
|
|
/**
|
|
|
|
* pauses the execution of the script for the given amount of milliseconds
|
|
|
|
*
|
|
|
|
* @param {integer} ms
|
|
|
|
* @returns A promise that resolves after ms milliseconds.
|
|
|
|
*/
|
|
|
|
async function sleep(ms) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:52:08 +00:00
|
|
|
/**
|
|
|
|
* Recieves an array of functions and executes them in a queue with the given concurrency
|
|
|
|
*
|
|
|
|
* @param {Array} fnArray
|
|
|
|
* @param {Number} concurrency
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
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
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-01-16 13:52:08 +00:00
|
|
|
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
|
|
|
|
|
2023-01-11 12:13:22 +00:00
|
|
|
/**
|
2023-02-03 12:56:34 +00:00
|
|
|
* Syncs the sequence number for the given model
|
|
|
|
* if no params are given it will reset all the sequence number to 0
|
2023-01-11 12:13:22 +00:00
|
|
|
*
|
2023-02-03 12:56:34 +00:00
|
|
|
* @param {Number} current - current sequence number
|
|
|
|
* @param {String} model - model name
|
|
|
|
* @param {Number} maximumSequenceNumber - maximum sequence number
|
|
|
|
* @returns
|
2023-01-11 12:13:22 +00:00
|
|
|
*/
|
2023-02-03 12:56:34 +00:00
|
|
|
async function syncSequence(current = 0, model = null ,maximumSequenceNumber = 0){
|
|
|
|
if (model == null && current == 0){
|
|
|
|
|
|
|
|
let mockModels = ['suppliers','tradeItems','supplyLines',];
|
|
|
|
|
|
|
|
for (let i = 0; i < mockModels.length; i++) {
|
|
|
|
const element = mockModels[i];
|
|
|
|
console.log('Syncing sequence for: ', element);
|
|
|
|
await syncSequence(0, element);
|
|
|
|
}
|
2023-01-12 13:54:05 +00:00
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
} else {
|
2023-01-10 12:24:43 +00:00
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
let tx = await models.sequelize.transaction();
|
|
|
|
|
|
|
|
try {
|
2023-01-10 12:24:43 +00:00
|
|
|
|
2023-02-07 08:41:54 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
2023-01-10 12:24:43 +00:00
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
await tx.commit();
|
2023-01-12 13:54:05 +00:00
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
} catch (error) {
|
|
|
|
await tx.rollback();
|
|
|
|
console.log('Error while syncing sequence number for: ', model);
|
2023-01-10 12:24:43 +00:00
|
|
|
}
|
2023-01-11 12:13:22 +00:00
|
|
|
}
|
2023-01-10 12:24:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
async function syncSuppliers(){
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
let headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${await getJWT()}`,
|
|
|
|
'X-Api-Key': process.env.API_KEY
|
|
|
|
};
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
let maximumSequenceNumber = await fetch(`${BASE_CUSTOMER_URL}organizations/current-max-sequence`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: headers
|
|
|
|
});
|
|
|
|
|
|
|
|
maximumSequenceNumber = await maximumSequenceNumber.json();
|
|
|
|
|
|
|
|
console.log('Maximum sequence number: ', maximumSequenceNumber);
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-07 13:38:54 +00:00
|
|
|
for (let i = 0; i < maximumSequenceNumber; i++) {
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
let query = `${BASE_CUSTOMER_URL}organizations/sync/${i}?organizationType=SUPPLIER&limit=1000`;
|
2023-02-07 13:38:54 +00:00
|
|
|
let response = await fetch(query, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: headers
|
|
|
|
});
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-07 13:38:54 +00:00
|
|
|
let data = await response.json();
|
|
|
|
|
|
|
|
let suppliers = data.results;
|
|
|
|
|
|
|
|
for (let supplier of suppliers) {
|
|
|
|
i = supplier.sequenceNumber;
|
2023-02-08 11:31:54 +00:00
|
|
|
await models.supplier.upsert({
|
2023-02-07 13:38:54 +00:00
|
|
|
isConnected: false,
|
|
|
|
commercialName: supplier.commercialName,
|
|
|
|
email: supplier.email,
|
|
|
|
phone: supplier.phone,
|
|
|
|
website: supplier.website,
|
|
|
|
mailingAddress: supplier.mailingAddress,
|
|
|
|
physicalAddress: supplier.physicalAddress,
|
|
|
|
pythosanitaryNumber: supplier.pythosanitaryNumber,
|
|
|
|
sequenceNumber: supplier.sequenceNumber,
|
|
|
|
organizationId: supplier.organizationId,
|
|
|
|
companyGln: supplier.companyGln,
|
|
|
|
name: supplier.name,
|
|
|
|
endDate: supplier.endDate,
|
|
|
|
rfhRelationId: supplier.rfhRelationId,
|
|
|
|
organizationType: supplier.organizationType,
|
|
|
|
paymentProviders: `${supplier.paymentProviders}`,
|
|
|
|
});
|
|
|
|
console.log('INSERTED:\t', supplier.commercialName, '\nsequenceNumber:\t', supplier.sequenceNumber);
|
|
|
|
}
|
|
|
|
await syncSequence(i, 'suppliers', maximumSequenceNumber);
|
|
|
|
}
|
2023-01-24 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 09:42:54 +00:00
|
|
|
async function syncConnections(){
|
|
|
|
let connections = await models.connection.findAll();
|
|
|
|
|
|
|
|
let headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${await getJWT()}`,
|
|
|
|
'X-Api-Key': process.env.API_KEY
|
|
|
|
};
|
|
|
|
|
|
|
|
let remoteConnections = await fetch(`${BASE_CUSTOMER_URL}connections`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: headers
|
|
|
|
});
|
|
|
|
|
|
|
|
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(`${BASE_CUSTOMER_URL}connections/${connection.organizationId}`, {
|
|
|
|
method: 'PUT',
|
|
|
|
headers: 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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
async function syncTradeItems(){
|
2023-01-16 13:52:08 +00:00
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
const suppliers = await models.supplier.findAll();
|
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
let headers = {
|
2023-01-16 13:52:08 +00:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${await getJWT()}`,
|
2023-02-03 12:56:34 +00:00
|
|
|
'X-Api-Key': process.env.API_KEY
|
2023-01-16 13:52:08 +00:00
|
|
|
};
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
let i = 0;
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
console.log('Syncing trade items');
|
|
|
|
for (let supplier of suppliers) {
|
|
|
|
i++;
|
2023-02-10 09:42:54 +00:00
|
|
|
if (!supplier.isConnected){
|
|
|
|
console.log('Supplier: ', supplier.commercialName, 'is not connected');
|
|
|
|
console.log('Skipping supplier', supplier.commercialName, '(', i, '/', suppliers.length, ')');
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-08 11:31:54 +00:00
|
|
|
let query = `${BASE_CUSTOMER_URL}trade-items?supplierOrganizationId=${supplier.organizationId}`;
|
2023-02-03 12:56:34 +00:00
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
try {
|
2023-02-03 12:56:34 +00:00
|
|
|
|
|
|
|
let request = await fetch(query, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: headers
|
|
|
|
});
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
let tradeItems = await request.json();
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
if (tradeItems.length == 0) {
|
|
|
|
console.log('No trade items for supplier: ', supplier.commercialName);
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
for (let tradeItem of tradeItems) {
|
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
await insertItem(tradeItem, supplier);
|
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
}
|
2023-01-24 12:51:44 +00:00
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
console.log('Synced trade items for: ', supplier.commercialName);
|
|
|
|
console.log('Remaining suppliers: ', suppliers.length - i, 'of', suppliers.length);
|
|
|
|
console.log('Total trade items: ', tradeItems.length);
|
|
|
|
} catch (error) {
|
|
|
|
console.log('Error while syncing trade items for: ', supplier.commercialName);
|
|
|
|
console.log(error);
|
2023-01-16 13:52:08 +00:00
|
|
|
}
|
2023-02-08 11:31:54 +00:00
|
|
|
|
2023-01-24 12:51:44 +00:00
|
|
|
}
|
2023-01-16 13:52:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 13:36:05 +00:00
|
|
|
/**
|
|
|
|
* Syncs the supply lines for suppliers that are connected
|
|
|
|
*/
|
2023-02-10 09:42:54 +00:00
|
|
|
async function syncSupplyLines(){
|
2023-02-14 12:34:54 +00:00
|
|
|
|
|
|
|
let currentSequenceNumber = await models.sequenceNumber.findOne({
|
|
|
|
where: {
|
|
|
|
model: 'supplyLines'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-10 09:42:54 +00:00
|
|
|
console.log('Syncing supply lines');
|
2023-02-14 12:34:54 +00:00
|
|
|
let suppliers = await models.supplier.findAll({
|
2023-02-10 09:42:54 +00:00
|
|
|
where: {
|
|
|
|
isConnected: true
|
|
|
|
}
|
2023-02-14 12:34:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let tradeItems = await models.tradeItem.findAll({
|
|
|
|
where: {
|
|
|
|
supplierOrganizationId: suppliers.map(supplier => supplier.organizationId)
|
|
|
|
}
|
|
|
|
});
|
2023-02-10 09:42:54 +00:00
|
|
|
|
|
|
|
console.log('Found', suppliers.length, 'connected suppliers');
|
|
|
|
|
|
|
|
let promises = [];
|
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
let headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${await getJWT()}`,
|
|
|
|
'X-Api-Key': process.env.API_KEY
|
|
|
|
};
|
2023-02-10 09:42:54 +00:00
|
|
|
// Launch a promise for each supplier
|
2023-02-14 12:34:54 +00:00
|
|
|
for (let tradeItem of tradeItems) {
|
|
|
|
let supplier = suppliers.find(supplier => supplier.organizationId == tradeItem.supplierOrganizationId);
|
|
|
|
console.log('Requesting supply lines for ' + supplier.commercialName + ' - ' + tradeItem.name);
|
2023-02-10 09:42:54 +00:00
|
|
|
// eslint-disable-next-line no-async-promise-executor
|
2023-02-14 12:34:54 +00:00
|
|
|
let promise = new Promise(async (resolve) => {
|
2023-02-10 09:42:54 +00:00
|
|
|
try {
|
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
let url = `${BASE_CUSTOMER_URL}supply-lines/sync/0?supplierOrganizationId=${supplier.organizationId}&tradeItemId=${tradeItem.tradeItemId}&limit=100&postFilterSelectedTradeItems=false`;
|
|
|
|
|
|
|
|
let request = await fetch(url, {
|
2023-02-10 09:42:54 +00:00
|
|
|
method: 'GET',
|
|
|
|
headers: headers
|
|
|
|
});
|
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
let supplyLines = await request.json();
|
2023-02-10 09:42:54 +00:00
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
if (supplyLines.length == 0) {
|
|
|
|
console.log('No supply lines for supplier: ', supplier.commercialName, ' - ' , tradeItem.name);
|
|
|
|
resolve([]);
|
|
|
|
return;
|
2023-02-10 09:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resolve(supplyLines);
|
|
|
|
|
|
|
|
} catch (error) {
|
2023-02-14 12:34:54 +00:00
|
|
|
console.log('Error while syncing supply lines for: ', supplier.commercialName, ' - ' , tradeItem.name);
|
|
|
|
console.log(error);
|
|
|
|
resolve([]);
|
2023-02-10 09:42:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
promises.push(promise);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let supplyLines = await Promise.all(promises);
|
2023-02-14 12:34:54 +00:00
|
|
|
let maximumSequenceNumber;
|
2023-02-10 09:42:54 +00:00
|
|
|
|
|
|
|
for (let supplyLine of supplyLines) {
|
2023-02-14 12:34:54 +00:00
|
|
|
maximumSequenceNumber = supplyLine.maximumSequenceNumber;
|
|
|
|
supplyLine = supplyLine.results;
|
|
|
|
try {
|
|
|
|
for (let line of supplyLine) {
|
2023-02-10 09:42:54 +00:00
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
let tradeItem = await models.tradeItem.findOne({
|
|
|
|
where: {
|
|
|
|
tradeItemId: line.tradeItemId
|
|
|
|
}
|
|
|
|
});
|
2023-02-10 09:42:54 +00:00
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
if (!tradeItem) {
|
|
|
|
console.log('Trade item not found for supply line: ', line.supplyLineId);
|
|
|
|
console.log('Requesting data for trade item id: ', line.tradeItemId);
|
|
|
|
|
|
|
|
let urlTradeItem = `${BASE_CUSTOMER_URL}trade-items?tradeItemIds=${line.tradeItemId}`;
|
2023-02-10 09:42:54 +00:00
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
let queryTradeItem = await fetch(urlTradeItem, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: headers
|
|
|
|
});
|
2023-02-10 09:42:54 +00:00
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
let tradeItem = await queryTradeItem.json();
|
2023-02-10 09:42:54 +00:00
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
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 (error) {
|
|
|
|
console.log('Error while syncing supply lines - ' + supplyLine.results[0].tradeItemId);
|
|
|
|
console.log(error);
|
2023-02-10 09:42:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Synced supply lines');
|
2023-02-14 12:34:54 +00:00
|
|
|
|
|
|
|
await syncSequence(currentSequenceNumber,'supplyLines' ,maximumSequenceNumber);
|
2023-02-10 09:42:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 12:34:54 +00:00
|
|
|
async function insertItem(tradeItem, supplier) {
|
|
|
|
|
|
|
|
let tx = await models.sequelize.transaction();
|
|
|
|
|
|
|
|
console.log('Syncing trade item: ', tradeItem.name);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// Temporal connection to all suppliers that have trade items
|
|
|
|
|
|
|
|
let currentSupp = await models.supplier.findOne({
|
|
|
|
where: {
|
|
|
|
organizationId: tradeItem.supplierOrganizationId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
currentSupp.isConnected = true;
|
|
|
|
|
|
|
|
await currentSupp.save({transaction: tx});
|
|
|
|
|
|
|
|
await models.connection.upsert({
|
|
|
|
organizationId: tradeItem.supplierOrganizationId,
|
|
|
|
connect: true,
|
|
|
|
}, {transaction: tx});
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
await models.tradeItem.upsert({
|
|
|
|
tradeItemId: tradeItem.tradeItemId,
|
|
|
|
supplierOrganizationId: tradeItem.supplierOrganizationId,
|
|
|
|
code: tradeItem.code,
|
|
|
|
gtin: tradeItem.gtin,
|
|
|
|
vbnProductCode: tradeItem.vbnProductCode,
|
|
|
|
name: tradeItem.name,
|
|
|
|
isDeleted: tradeItem.isDeleted,
|
|
|
|
sequenceNumber: tradeItem.sequenceNumber,
|
|
|
|
tradeItemVersion: tradeItem.tradeItemVersion,
|
|
|
|
isCustomerSpecific: tradeItem.isCustomerSpecific,
|
|
|
|
isHiddenInCatalog: tradeItem.isHiddenInCatalog,
|
|
|
|
},
|
|
|
|
{transaction: tx});
|
|
|
|
|
|
|
|
|
|
|
|
let characteristics = tradeItem.characteristics;
|
|
|
|
|
|
|
|
for (let characteristic of characteristics) {
|
|
|
|
await models.characteristic.upsert({
|
|
|
|
vbnCode: characteristic.vbnCode,
|
|
|
|
vbnValueCode: characteristic.vbnValueCode,
|
|
|
|
tradeItemFk: tradeItem.tradeItemId,
|
|
|
|
}, {transaction: tx});
|
|
|
|
}
|
|
|
|
|
|
|
|
let seasonalPeriods = tradeItem.seasonalPeriods;
|
|
|
|
|
|
|
|
for (let seasonalPeriod of seasonalPeriods) {
|
|
|
|
await models.seasonalPeriod.upsert({
|
|
|
|
startWeek: seasonalPeriod.startWeek,
|
|
|
|
endWeek: seasonalPeriod.endWeek,
|
|
|
|
tradeItemFk: tradeItem.tradeItemId,
|
|
|
|
}, {transaction: tx});
|
|
|
|
}
|
|
|
|
|
|
|
|
let photos = tradeItem.photos;
|
|
|
|
|
|
|
|
for (let photo of photos) {
|
|
|
|
await models.photo.upsert({
|
|
|
|
id: photo.id,
|
|
|
|
url: photo.url,
|
|
|
|
type: photo.type,
|
|
|
|
primary: photo.primary,
|
|
|
|
tradeItemFk: tradeItem.tradeItemId,
|
|
|
|
}, {transaction: tx});
|
|
|
|
}
|
|
|
|
|
|
|
|
let packingConfigurations = tradeItem.packingConfigurations;
|
|
|
|
|
|
|
|
for (let packingConfiguration of packingConfigurations) {
|
|
|
|
|
|
|
|
let uuid = uuidv4();
|
|
|
|
|
|
|
|
await models.packingConfiguration.upsert({
|
|
|
|
packingConfigurationId: uuid,
|
|
|
|
piecesPerPackage: packingConfiguration.piecesPerPackage,
|
|
|
|
bunchesPerPackage: packingConfiguration.bunchesPerPackage,
|
|
|
|
photoUrl: packingConfiguration.photoUrl,
|
|
|
|
packagesPerLayer: packingConfiguration.packagesPerLayer,
|
|
|
|
layersPerLoadCarrier: packingConfiguration.layersPerLoadCarrier,
|
|
|
|
additionalPricePerPiece : JSON.stringify(packingConfiguration.additionalPricePerPiece),
|
|
|
|
transportHeightInCm: packingConfiguration.transportHeightInCm,
|
|
|
|
loadCarrierType: packingConfiguration.loadCarrierType,
|
|
|
|
isPrimary: packingConfiguration.isPrimary,
|
|
|
|
tradeItemFk: tradeItem.tradeItemId,
|
|
|
|
}, {transaction: tx});
|
|
|
|
await models.package.upsert({
|
|
|
|
vbnPackageCode: packingConfiguration.package.vbnPackageCode,
|
|
|
|
customPackageId: packingConfiguration.package.customPackageId,
|
|
|
|
packingConfigurationFk: uuid,
|
|
|
|
}, {transaction: tx});
|
|
|
|
}
|
|
|
|
|
|
|
|
let countryOfOriginIsoCodes = tradeItem.countryOfOriginIsoCodes;
|
|
|
|
|
|
|
|
countryOfOriginIsoCodes ??= 0;
|
|
|
|
|
|
|
|
for (let countryOfOriginIsoCode of countryOfOriginIsoCodes) {
|
|
|
|
await models.countryOfOriginIsoCode.upsert({
|
|
|
|
isoCode: countryOfOriginIsoCode,
|
|
|
|
tradeItemFk: tradeItem.tradeItemId,
|
|
|
|
}, {transaction: tx});
|
|
|
|
}
|
|
|
|
|
|
|
|
let botanicalNames = tradeItem.botanicalNames;
|
|
|
|
|
|
|
|
for (let botanicalName of botanicalNames) {
|
|
|
|
await models.botanicalName.upsert({
|
|
|
|
name: botanicalName.name,
|
|
|
|
tradeItemFk: tradeItem.tradeItemId,
|
|
|
|
}, {transaction: tx});
|
|
|
|
}
|
|
|
|
|
|
|
|
await tx.commit();
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
await tx.rollback();
|
|
|
|
console.log('Error while syncing trade items for: ', supplier.commercialName);
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-10 09:42:54 +00:00
|
|
|
export { getClientToken, updateClientConfig, getJWT, sleep, asyncQueue, syncSequence, syncSuppliers, syncTradeItems, syncConnections, syncSupplyLines };
|