rebase changes, progressbar, vn-b4 uuid

This commit is contained in:
Pau 2023-01-12 14:54:05 +01:00
parent 0df35432f9
commit 2de5e10e3e
13 changed files with 188 additions and 159 deletions

View File

@ -1,5 +1,6 @@
import moment from 'moment';
import * as vnUtils from './utils.js';
import cliProgress from 'cli-progress';
import dotenv from 'dotenv';
dotenv.config();
@ -17,13 +18,19 @@ try {
console.log('Querying suppliers...');
console.log(suppliers.suppliers.length + ' suppliers found');
for (let i = 0; i < suppliers.suppliers.length; i++) {
console.log(i , 'Querying supplier: ' + suppliers.suppliers[i].SupplierGLN);
vnUtils.getTradeitems(suppliers.suppliers[i].SupplierGLN);
await vnUtils.sleep(300);
}
}
const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
bar1.start(suppliers.suppliers.length, 0);
// vnUtils.getTradeitems(suppliers.suppliers[i].SupplierGLN)
for (let i = 0; i < suppliers.suppliers.length; i++) {
await vnUtils.getTradeitems(suppliers.suppliers[i].SupplierGLN).then(() => {
bar1.increment();
});
}
bar1.stop();
}
setInterval(async () => {
console.log('Querying the API to check for new data...');
@ -46,4 +53,4 @@ console.log = function () {
let args = Array.prototype.slice.call(arguments);
args.unshift(new moment().format('HH:mm:ss') + ' -');
console.info.apply(console, args);
};
};

View File

@ -1,31 +0,0 @@
import { Sequelize } from 'sequelize';
const additionalPricePerPiece = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
packingConfigurationsFk: {
type: Sequelize.INTEGER,
},
currency: {
type: Sequelize.STRING,
defaultValue: 'EUR',
},
value: {
type: Sequelize.DECIMAL(10, 2),
},
};
export default (sequelize) => {
const AdditionalPricePerPiece = sequelize.define(
'FDadditionalPricePerPiece',
additionalPricePerPiece,
{
timestamps: false,
freezeTableName: true,
}
);
return AdditionalPricePerPiece;
};

View File

@ -1,9 +1,6 @@
import { Sequelize } from 'sequelize';
const botanicalNames = {
tradeItemFk: {
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,
},

View File

@ -1,9 +1,6 @@
import { Sequelize } from 'sequelize';
const characteristics = {
tradeItemFk: {
type: Sequelize.STRING,
},
vbnCode: {
type: Sequelize.STRING,
},

View File

@ -1,9 +1,6 @@
import { Sequelize } from 'sequelize';
const countryOfOriginIsoCodes = {
tradeItemFk: {
type: Sequelize.STRING,
},
isoCode: {
type: Sequelize.STRING,
},

View File

@ -1,10 +1,10 @@
import { Sequelize } from 'sequelize';
import dotenv from 'dotenv';
import {sleep} from '../utils.js';
dotenv.config();
let sequelize = createConnection();
import additionalPricePerPiece from './additionalPricePerPiece.js';
import botanicalNames from './botanicalNames.js';
import countryOfOriginIsoCodes from './countryOfOriginIsoCodes.js';
import packageModel from './package.js';
@ -36,18 +36,78 @@ import characteristics from './characteristics.js';
* @type {Object.<string, Sequelize.Model>}
*/
let models = {
additionalPricePerPiece: additionalPricePerPiece(sequelize),
botanicalNames: botanicalNames(sequelize),
countryOfOriginIsoCodes: countryOfOriginIsoCodes(sequelize),
package: packageModel(sequelize),
tradeItem: tradeItem(sequelize),
packingConfigurations: packingConfigurations(sequelize),
photos: photos(sequelize),
seasonalPeriod: seasonalPeriod(sequelize),
tradeItem: tradeItem(sequelize),
clientConfig: clientConfig(sequelize),
characteristics: characteristics(sequelize),
countryOfOriginIsoCodes: countryOfOriginIsoCodes(sequelize),
package: packageModel(sequelize),
seasonalPeriod: seasonalPeriod(sequelize),
clientConfig: clientConfig(sequelize),
botanicalNames: botanicalNames(sequelize),
};
models.characteristics.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.seasonalPeriod.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.photos.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.photos.hasMany(models.seasonalPeriod, {
foreignKey: 'photoFk',
as: 'seasonalPeriod_Fk',
targetKey: 'photoId',
});
models.seasonalPeriod.belongsTo(models.photos, {
foreignKey: 'photoFk',
as: 'photo_Fk',
targetKey: 'photoId',
});
models.packingConfigurations.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.packingConfigurations.hasMany(models.package, {
foreignKey: 'packingConfigurationFk',
as: 'package_Fk',
targetKey: 'packingConfigurationId',
});
models.package.belongsTo(models.packingConfigurations, {
foreignKey: 'packingConfigurationFk',
as: 'packingConfiguration_Fk',
targetKey: 'packingConfigurationId',
});
models.botanicalNames.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.countryOfOriginIsoCodes.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemFk',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
if (process.env.FORCE_SYNC) {
console.log('Syncing the models...');
await sequelize.sync({ force: true });

View File

@ -6,12 +6,12 @@ const PackageModel = {
primaryKey: true,
autoIncrement: true,
},
packingConfigurationsFk: {
type: Sequelize.STRING,
},
vbnPackageCode: {
type: Sequelize.INTEGER,
},
customPackageId: {
type: Sequelize.STRING,
},
};
export default (sequelize) => {

View File

@ -6,7 +6,7 @@ const packingConfigurations = {
primaryKey: true,
autoIncrement: true,
},
tradeItemFk: {
packingConfigurationId: {
type: Sequelize.STRING,
},
piecesPerPackage: {
@ -24,6 +24,9 @@ const packingConfigurations = {
layersPerLoadCarrier: {
type: Sequelize.INTEGER,
},
additionalPricePerPiece: {
type: Sequelize.STRING,
},
transportHeightInCm: {
type: Sequelize.INTEGER,
},

View File

@ -1,9 +1,6 @@
import { Sequelize } from 'sequelize';
const photos = {
tradeItemFk: {
type: Sequelize.STRING,
},
photoId: {
type: Sequelize.STRING,
},

View File

@ -6,9 +6,6 @@ const seasonalPeriod = {
primaryKey: true,
autoIncrement: true,
},
tradeItemFk: {
type: Sequelize.STRING,
},
startWeek: {
type: Sequelize.INTEGER,
},

32
package-lock.json generated
View File

@ -124,8 +124,7 @@
"ansi-regex": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
"integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==",
"dev": true
"integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="
},
"ansi-styles": {
"version": "4.3.0",
@ -174,6 +173,14 @@
"supports-color": "^7.1.0"
}
},
"cli-progress": {
"version": "3.11.2",
"resolved": "https://registry.npmjs.org/cli-progress/-/cli-progress-3.11.2.tgz",
"integrity": "sha512-lCPoS6ncgX4+rJu5bS3F/iCz17kZ9MPZ6dpuTtI0KXKABkhyXIdYB3Inby1OpaGti3YlI3EeEkM9AuWpelJrVA==",
"requires": {
"string-width": "^4.2.3"
}
},
"color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
@ -249,6 +256,11 @@
"resolved": "https://registry.npmjs.org/dottie/-/dottie-2.0.2.tgz",
"integrity": "sha512-fmrwR04lsniq/uSr8yikThDTrM7epXHBAAjH9TbeH3rEA8tdCO7mRzB9hdmdGyJCxF8KERo9CITcm3kGuoyMhg=="
},
"emoji-regex": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
"integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A=="
},
"escape-string-regexp": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
@ -562,6 +574,11 @@
"integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==",
"dev": true
},
"is-fullwidth-code-point": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
"integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg=="
},
"is-glob": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz",
@ -911,11 +928,20 @@
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
"dev": true
},
"string-width": {
"version": "4.2.3",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
"integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==",
"requires": {
"emoji-regex": "^8.0.0",
"is-fullwidth-code-point": "^3.0.0",
"strip-ansi": "^6.0.1"
}
},
"strip-ansi": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
"integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==",
"dev": true,
"requires": {
"ansi-regex": "^5.0.1"
}

View File

@ -10,6 +10,7 @@
"dev-query": "QUERYSUPPLIERS=true FORCE_SYNC=true SECRETS=true node --max-old-space-size=4096 index.js"
},
"dependencies": {
"cli-progress": "^3.11.2",
"dotenv": "^16.0.3",
"mariadb": "^3.0.2",
"moment": "^2.29.4",

160
utils.js
View File

@ -3,6 +3,7 @@ import fetch from 'node-fetch';
import dotenv from 'dotenv';
import { models } from './models/index.js';
import { v4 as uuidv4 } from 'uuid';
import cliProgress from 'cli-progress';
dotenv.config();
/**
@ -158,6 +159,11 @@ async function getTradeitems(organizationGln) {
const tradeitemsResponse = await tradeitemsRequest.json();
// If there is no tradeitems
if (tradeitemsResponse.length == 0) {
return;
}
// if there is at least one tradeitem, save it in the database
//console.log(tradeitemsResponse[0]);
@ -166,9 +172,13 @@ async function getTradeitems(organizationGln) {
if (tradeitemsResponse.length > 0 && tradeitemsResponse != null) {
console.log(`Tradeitems for the organization ${organizationGln} : `, tradeitemsResponse.length);
let bar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_grey);
bar.start(tradeitemsResponse.length, 0);
await tradeitemsResponse.forEach(async item => {
bar.increment();
try {
item.tradeItemId = 'Vn-' + item.tradeItemId;
@ -187,116 +197,84 @@ async function getTradeitems(organizationGln) {
isHiddenInCatalog: item.isHiddenInCatalog,
});
try {
await item.characteristics.forEach((characteristic) => {
models.characteristics.upsert({
tradeItemFk: item.tradeItemId,
vbnCode: characteristic.vbnCode,
vbnValueCode: characteristic.vbnValueCode,
});
await item.characteristics.forEach((characteristic) => {
models.characteristics.upsert({
tradeItemFk: item.tradeItemId,
vbnCode: characteristic.vbnCode,
vbnValueCode: characteristic.vbnValueCode,
});
} catch (error) {
console.log('There was a error while saving the characteristics for the tradeitem: ', item.tradeItemId);
}
try {
await item.seasonalPeriods.forEach((seasonalPeriod) => {
models.seasonalPeriod.upsert({
tradeItemFk: item.tradeItemId,
startWeek: seasonalPeriod.startWeek,
endWeek: seasonalPeriod.endWeek,
});
});
await item.seasonalPeriods.forEach((seasonalPeriod) => {
models.seasonalPeriod.upsert({
tradeItemFk: item.tradeItemId,
startWeek: seasonalPeriod.startWeek,
endWeek: seasonalPeriod.endWeek,
});
} catch (error) {
console.log('There was a error while saving the seasonalPeriods for the tradeitem: ', item.tradeItemId);
}
try {
await item.photos.forEach((photo) => {
});
photo.id = 'Vn-' + photo.id;
await item.photos.forEach((photo) => {
models.photos.upsert({
tradeItemFk: item.tradeItemId,
photoId: photo.id,
url: photo.url,
seasonalPeriodFk: photo.seasonalPeriod,
type: photo.type,
primary: photo.primary,
});
photo.id = 'Vn-' + photo.id;
models.photos.upsert({
tradeItemFk: item.tradeItemId,
photoId: photo.id,
url: photo.url,
seasonalPeriodFk: JSON.stringify(photo.seasonalPeriod),
type: photo.type,
primary: photo.primary,
});
} catch (error) {
console.log('There was a error while saving the photos for the tradeitem: ', item.tradeItemId);
}
});
try {
await item.packingConfigurations.forEach(async (packagingConfiguration) => {
await item.packingConfigurations.forEach(async (packagingConfiguration) => {
let uuid = uuidv4();
uuid = 'Vn-' + uuid;
await models.packingConfigurations.upsert({
tradeItemFk: item.tradeItemId,
packingConfigurationId: uuid,
piecesPerPackage: packagingConfiguration.piecesPerPackage,
bunchesPerPackage: packagingConfiguration.bunchesPerPackage,
photoUrl: packagingConfiguration.photoUrl,
packagesPerLayer: packagingConfiguration.packagesPerLayer,
layersPerLoadCarrier: packagingConfiguration.layersPerLoadCarrier,
transportHeightInCm: packagingConfiguration.transportHeightInCm,
loadCarrierType: packagingConfiguration.loadCarrierType,
isPrimary: packagingConfiguration.isPrimary,
});
await models.package.upsert({
packingConfigurationsFk: uuid,
vbnPackageCode: packagingConfiguration.package.vbnPackageCode,
vbnPackageValueCode: packagingConfiguration.package.vbnPackageValueCode,
});
await models.additionalPricePerPiece.upsert({
packingConfigurationsFk: uuid,
currency: packagingConfiguration.additionalPricePerPiece.currency,
value: packagingConfiguration.additionalPricePerPiece.value,
});
let uuid = uuidv4();
uuid = 'Vn-' + uuid;
await models.packingConfigurations.upsert({
tradeItemFk: item.tradeItemId,
packingConfigurationId: uuid,
piecesPerPackage: packagingConfiguration.piecesPerPackage,
bunchesPerPackage: packagingConfiguration.bunchesPerPackage,
photoUrl: packagingConfiguration.photoUrl,
packagesPerLayer: packagingConfiguration.packagesPerLayer,
layersPerLoadCarrier: packagingConfiguration.layersPerLoadCarrier,
transportHeightInCm: packagingConfiguration.transportHeightInCm,
loadCarrierType: packagingConfiguration.loadCarrierType,
isPrimary: packagingConfiguration.isPrimary,
additionalPricePerPiece: JSON.stringify(packagingConfiguration.additionalPricePerPiece),
});
} catch (error) {
console.log('There was a error while saving the packingConfigurations for the tradeitem: ', item.tradeItemId);
}
try {
await item.botanicalNames.forEach((botanicalName) => {
models.botanicalNames.upsert({
tradeItemFk: item.tradeItemId,
name: botanicalName,
});
await models.package.upsert({
packingConfigurationsId: uuid,
vbnPackageCode: packagingConfiguration.package.vbnPackageCode,
vbnPackageValueCode: packagingConfiguration.package.vbnPackageValueCode,
});
} catch (error) {
console.log('There was a error while saving the botanicalNames for the tradeitem: ', item.tradeItemId);
}
try {
await item.countryOfOriginIsoCodes.forEach((countryOfOriginIsoCode) => {
models.countryOfOriginIsoCodes.upsert({
tradeItemFk: item.tradeItemId,
isoCode: countryOfOriginIsoCode,
});
});
await item.botanicalNames.forEach((botanicalName) => {
models.botanicalNames.upsert({
tradeItemFk: item.tradeItemId,
name: botanicalName,
});
} catch (error) {
console.log('There was a error while saving the countryOfOriginIsoCodes for the tradeitem: ', item.tradeItemId);
}
});
await item.countryOfOriginIsoCodes.forEach((countryOfOriginIsoCode) => {
models.countryOfOriginIsoCodes.upsert({
tradeItemFk: item.tradeItemId,
isoCode: countryOfOriginIsoCode,
});
});
} catch (error) {
console.log('There was an error while saving the data to the database');
console.log(error);
}
});
} else {
console.log('There are no tradeitems for the organization: ', organizationGln);
bar.stop();
}
} catch (error) {
@ -305,4 +283,4 @@ async function getTradeitems(organizationGln) {
}
}
export { getClientToken, updateClientConfig, getJWT, getTradeitems ,sleep };
export { getClientToken, updateClientConfig, getJWT, getTradeitems, sleep };