changes to supplyline model and method
This commit is contained in:
parent
68a71861fb
commit
10503d7759
|
@ -12,7 +12,6 @@ The Floriday service project should perform the following tasks:
|
|||
This is done using the [Sequelize](https://sequelize.org/) ORM and storing the data as it is received from the API,
|
||||
so it can be compared with the previous data.
|
||||
|
||||
# EVERY UUID GENERATED IN THIS PROJECT IS PREPENDED W/ 'Vn-' BECAUSE OF A BUG IN SEQUELIZE
|
||||
|
||||
## Guidelines
|
||||
|
||||
|
|
7
index.js
7
index.js
|
@ -19,7 +19,7 @@ process.env.SYNC_SEQUENCE ? await vnUtils.syncSequence() : null;
|
|||
process.env.SYNC_SUPPLIER ? await vnUtils.syncSuppliers() : null;
|
||||
await vnUtils.syncConnections();
|
||||
process.env.SYNC_TRADEITEM ? await vnUtils.syncTradeItems() : null;
|
||||
await vnUtils.syncSupplyLines();
|
||||
console.log('Synced trade items');
|
||||
|
||||
try {
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
|
@ -33,15 +33,16 @@ try {
|
|||
tokenExpirationDate = await vnUtils.getClientToken(models);
|
||||
}
|
||||
|
||||
await vnUtils.syncSupplyLines();
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
if (process.env.STATUS == 'development') {
|
||||
await vnUtils.sleep(15000);
|
||||
await vnUtils.sleep(120000);
|
||||
} else {
|
||||
await vnUtils.sleep(30000);
|
||||
await vnUtils.sleep(300000);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
314
utils.js
314
utils.js
|
@ -4,7 +4,6 @@ import dotenv from 'dotenv';
|
|||
import { models } from './models/index.js';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
//import cliProgress from 'cli-progress';
|
||||
import suppliersGln from './suppliersGln.js';
|
||||
dotenv.config();
|
||||
|
||||
/**
|
||||
|
@ -365,12 +364,204 @@ async function syncTradeItems(){
|
|||
|
||||
for (let tradeItem of tradeItems) {
|
||||
|
||||
await insertItem(tradeItem, supplier);
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
async function syncSupplyLines(){
|
||||
|
||||
let currentSequenceNumber = await models.sequenceNumber.findOne({
|
||||
where: {
|
||||
model: 'supplyLines'
|
||||
}
|
||||
});
|
||||
|
||||
console.log('Syncing supply lines');
|
||||
let suppliers = await models.supplier.findAll({
|
||||
where: {
|
||||
isConnected: true
|
||||
}
|
||||
});
|
||||
|
||||
let tradeItems = await models.tradeItem.findAll({
|
||||
where: {
|
||||
supplierOrganizationId: suppliers.map(supplier => supplier.organizationId)
|
||||
}
|
||||
});
|
||||
|
||||
console.log('Found', suppliers.length, 'connected suppliers');
|
||||
|
||||
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 = `${BASE_CUSTOMER_URL}supply-lines/sync/0?supplierOrganizationId=${supplier.organizationId}&tradeItemId=${tradeItem.tradeItemId}&limit=100&postFilterSelectedTradeItems=false`;
|
||||
|
||||
let request = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: 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);
|
||||
|
||||
}
|
||||
|
||||
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: {
|
||||
tradeItemId: line.tradeItemId
|
||||
}
|
||||
});
|
||||
|
||||
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}`;
|
||||
|
||||
let queryTradeItem = await fetch(urlTradeItem, {
|
||||
method: 'GET',
|
||||
headers: 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 (error) {
|
||||
console.log('Error while syncing supply lines - ' + supplyLine.results[0].tradeItemId);
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Synced supply lines');
|
||||
|
||||
await syncSequence(currentSequenceNumber,'supplyLines' ,maximumSequenceNumber);
|
||||
}
|
||||
|
||||
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,
|
||||
|
@ -472,129 +663,8 @@ async function syncTradeItems(){
|
|||
console.log('Error while syncing trade items for: ', supplier.commercialName);
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
async function syncSupplyLines(){
|
||||
console.log('Syncing supply lines');
|
||||
let suppliers = await models.supplier.findAll(/*{
|
||||
where: {
|
||||
isConnected: true
|
||||
}
|
||||
}*/);
|
||||
|
||||
console.log('Found', suppliers.length, 'connected suppliers');
|
||||
|
||||
let promises = [];
|
||||
|
||||
// Launch a promise for each supplier
|
||||
for (let supplier of suppliers) {
|
||||
|
||||
let headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${await getJWT()}`,
|
||||
'X-Api-Key': process.env.API_KEY
|
||||
};
|
||||
|
||||
console.log('Requesting supply lines for CATALOG');
|
||||
// eslint-disable-next-line no-async-promise-executor
|
||||
let promise = new Promise(async (resolve, reject) => {
|
||||
let queryCATALOG = `${BASE_CUSTOMER_URL}supply-lines?supplierOrganizationId=${supplier.organizationId}&supplyType=CATALOG`;
|
||||
let queryBATCH = `${BASE_CUSTOMER_URL}supply-lines?supplierOrganizationId=${supplier.organizationId}&supplyType=BATCH`;
|
||||
|
||||
try {
|
||||
let requestCATALOG = await fetch(queryCATALOG, {
|
||||
method: 'GET',
|
||||
headers: headers
|
||||
});
|
||||
|
||||
let requestBATCH = await fetch(queryBATCH, {
|
||||
method: 'GET',
|
||||
headers: headers
|
||||
});
|
||||
|
||||
let supplyLinesCATALOG = await requestCATALOG.json();
|
||||
let supplyLinesBATCH = await requestBATCH.json();
|
||||
|
||||
if (supplyLinesBATCH.length == 0) {
|
||||
console.log('No supply lines for BATCH on: ', supplier.commercialName);
|
||||
}
|
||||
|
||||
if (supplyLinesCATALOG.length == 0) {
|
||||
console.log('No supply lines for CATALOG on:', supplier.commercialName);
|
||||
}
|
||||
|
||||
let supplyLines = supplyLinesCATALOG.concat(supplyLinesBATCH);
|
||||
|
||||
if (supplyLines.length > 0) {
|
||||
console.log('Syncing supply lines for: ', supplier.commercialName);
|
||||
console.log('Found', supplyLines.length, 'supply lines');
|
||||
}
|
||||
|
||||
resolve(supplyLines);
|
||||
|
||||
} catch (error) {
|
||||
console.log('Error while syncing supply lines for: ', supplier.commercialName);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
|
||||
promises.push(promise);
|
||||
|
||||
}
|
||||
|
||||
let supplyLines = await Promise.all(promises);
|
||||
|
||||
for (let supplyLine of supplyLines) {
|
||||
for (let line of supplyLine) {
|
||||
|
||||
let 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,
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Synced supply lines');
|
||||
}
|
||||
|
||||
export { getClientToken, updateClientConfig, getJWT, sleep, asyncQueue, syncSequence, syncSuppliers, syncTradeItems, syncConnections, syncSupplyLines };
|
Loading…
Reference in New Issue