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
|
||||
|
||||
|
|
11
index.js
11
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
|
||||
|
@ -27,21 +27,22 @@ try {
|
|||
try{
|
||||
console.log('Querying the API to check for new data...');
|
||||
console.log('Current token expiration date: ', tokenExpirationDate);
|
||||
|
||||
|
||||
if (moment().isAfter(tokenExpirationDate)) {
|
||||
console.log('Token expired, getting a new one...');
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
426
utils.js
426
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,113 +364,8 @@ async function syncTradeItems(){
|
|||
|
||||
for (let tradeItem of tradeItems) {
|
||||
|
||||
let tx = await models.sequelize.transaction();
|
||||
|
||||
console.log('Syncing trade item: ', tradeItem.name);
|
||||
|
||||
try {
|
||||
|
||||
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);
|
||||
}
|
||||
await insertItem(tradeItem, supplier);
|
||||
|
||||
}
|
||||
|
||||
console.log('Synced trade items for: ', supplier.commercialName);
|
||||
|
@ -486,66 +380,64 @@ async function syncTradeItems(){
|
|||
}
|
||||
|
||||
async function syncSupplyLines(){
|
||||
|
||||
let currentSequenceNumber = await models.sequenceNumber.findOne({
|
||||
where: {
|
||||
model: 'supplyLines'
|
||||
}
|
||||
});
|
||||
|
||||
console.log('Syncing supply lines');
|
||||
let suppliers = await models.supplier.findAll(/*{
|
||||
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 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');
|
||||
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, 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`;
|
||||
|
||||
let promise = new Promise(async (resolve) => {
|
||||
try {
|
||||
let requestCATALOG = await fetch(queryCATALOG, {
|
||||
|
||||
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 requestBATCH = await fetch(queryBATCH, {
|
||||
method: 'GET',
|
||||
headers: headers
|
||||
});
|
||||
let supplyLines = await request.json();
|
||||
|
||||
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');
|
||||
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);
|
||||
reject(error);
|
||||
console.log('Error while syncing supply lines for: ', supplier.commercialName, ' - ' , tradeItem.name);
|
||||
console.log(error);
|
||||
resolve([]);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -554,47 +446,225 @@ async function syncSupplyLines(){
|
|||
}
|
||||
|
||||
let supplyLines = await Promise.all(promises);
|
||||
let maximumSequenceNumber;
|
||||
|
||||
for (let supplyLine of supplyLines) {
|
||||
for (let line of supplyLine) {
|
||||
maximumSequenceNumber = supplyLine.maximumSequenceNumber;
|
||||
supplyLine = supplyLine.results;
|
||||
try {
|
||||
for (let line of supplyLine) {
|
||||
|
||||
let tradeItem = await models.tradeItem.findOne({
|
||||
where: {
|
||||
tradeItemId: line.tradeItemId
|
||||
}
|
||||
});
|
||||
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;
|
||||
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,
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
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,
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export { getClientToken, updateClientConfig, getJWT, sleep, asyncQueue, syncSequence, syncSuppliers, syncTradeItems, syncConnections, syncSupplyLines };
|
Loading…
Reference in New Issue