changes to supplyline model and method

This commit is contained in:
Pau 2023-02-10 10:42:54 +01:00
parent 771153b556
commit 50c943e1e6
3 changed files with 179 additions and 7 deletions

View File

@ -17,7 +17,9 @@ let tokenExpirationDate = await vnUtils.getClientToken(models);
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();
try {
// eslint-disable-next-line no-constant-condition
@ -31,6 +33,7 @@ try {
tokenExpirationDate = await vnUtils.getClientToken(models);
}
} catch (error) {
console.error(error);
}

View File

@ -1,14 +1,9 @@
import { Sequelize } from 'sequelize';
const supplyLine = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
supplyLineId: {
type: Sequelize.STRING,
unique: true,
primaryKey: true,
},
status: {
type: Sequelize.STRING,

176
utils.js
View File

@ -270,6 +270,63 @@ async function syncSuppliers(){
}
}
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
}
});
}
}
}
async function syncTradeItems(){
const suppliers = await models.supplier.findAll();
@ -285,6 +342,11 @@ async function syncTradeItems(){
console.log('Syncing trade items');
for (let supplier of suppliers) {
i++;
if (!supplier.isConnected){
console.log('Supplier: ', supplier.commercialName, 'is not connected');
console.log('Skipping supplier', supplier.commercialName, '(', i, '/', suppliers.length, ')');
continue;
}
let query = `${BASE_CUSTOMER_URL}trade-items?supplierOrganizationId=${supplier.organizationId}`;
try {
@ -423,4 +485,116 @@ async function syncTradeItems(){
}
}
export { getClientToken, updateClientConfig, getJWT, sleep, asyncQueue, syncSequence, syncSuppliers, syncTradeItems };
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 };