trying to solve sequelize uuid bug

This commit is contained in:
Pau 2023-01-11 13:13:22 +01:00
parent 8500175cef
commit af1d804cb1
15 changed files with 4685 additions and 119 deletions

View File

@ -12,6 +12,8 @@ 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
- In case a new model is created, it should follow the following structure:

View File

@ -4,16 +4,32 @@ import * as vnUtils from './utils.js';
import dotenv from 'dotenv';
dotenv.config();
import models from './models/index.js';
import { models } from './models/index.js';
import suppliers from './suppliersGln.js';
let tokenExpirationDate = await vnUtils.getClientToken(models);
try {
if (process.env.QUERYSUPPLIERS) {
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(250);
}
}
setInterval(async () => {
console.log('Querying the API to check for new data...');
vnUtils.getTradeitems(8714231154815);
console.log('Current token expiration date: ', tokenExpirationDate);
if (moment().isAfter(tokenExpirationDate)) {
@ -21,7 +37,7 @@ try {
tokenExpirationDate = await vnUtils.getClientToken(models);
}
}, process.env.STATUS == 'development' ? 10000 : 20000);
}, process.env.STATUS == 'development' ? 15000 : 30000);
} catch (error) {
console.error('Unable to connect to the database:', error);
}

View File

@ -2,7 +2,7 @@ import { Sequelize } from 'sequelize';
const botanicalNames = {
tradeItemFk: {
type: Sequelize.INTEGER,
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,

View File

@ -2,12 +2,12 @@ import { Sequelize } from 'sequelize';
const characteristics = {
tradeItemFk: {
type: Sequelize.INTEGER,
},
vnbCode: {
type: Sequelize.STRING,
},
vnbValueCode: {
vbnCode: {
type: Sequelize.STRING,
},
vbnValueCode: {
type: Sequelize.STRING,
},
};

View File

@ -2,7 +2,7 @@ import { Sequelize } from 'sequelize';
const countryOfOriginIsoCodes = {
tradeItemFk: {
type: Sequelize.INTEGER,
type: Sequelize.STRING,
},
isoCode: {
type: Sequelize.STRING,

View File

@ -2,11 +2,7 @@ import { Sequelize } from 'sequelize';
import dotenv from 'dotenv';
dotenv.config();
let sequelize = new Sequelize(process.env.DB_SCHEMA, process.env.DB_USER, process.env.DB_PWD, {
host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT,
logging: false,
});
let sequelize = createConnection();
import additionalPricePerPiece from './additionalPricePerPiece.js';
import botanicalNames from './botanicalNames.js';
@ -32,16 +28,31 @@ let models = {
characteristics: characteristics(sequelize),
};
if (process.env.FORCE_SYNC === 'true') {
if (process.env.FORCE_SYNC) {
console.log('Syncing the models...');
await sequelize.sync({ force: true });
}
if (process.env.SECRETS === 'true') {
if (process.env.SECRETS) {
await models.clientConfig.create({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
});
}
export default models;
function createConnection() {
console.log('Creating the connection...');
return new Sequelize(process.env.DB_SCHEMA, process.env.DB_USER, process.env.DB_PWD, {
host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT,
logging: false,
pool: {
max: 20,
min: 0,
acquire: 60000,
idle: 10000,
},
});
}
export { models } ;

View File

@ -12,9 +12,6 @@ const PackageModel = {
vbnPackageCode: {
type: Sequelize.INTEGER,
},
customPackageId: {
type: Sequelize.UUID,
},
};
export default (sequelize) => {

View File

@ -7,7 +7,7 @@ const packingConfigurations = {
autoIncrement: true,
},
tradeItemFk: {
type: Sequelize.INTEGER,
type: Sequelize.STRING,
},
piecesPerPackage: {
type: Sequelize.INTEGER,

View File

@ -2,10 +2,10 @@ import { Sequelize } from 'sequelize';
const photos = {
tradeItemFk: {
type: Sequelize.INTEGER,
type: Sequelize.STRING,
},
photoId: {
type: Sequelize.UUID,
type: Sequelize.STRING,
},
url: {
type: Sequelize.STRING,

View File

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

View File

@ -7,11 +7,11 @@ const tradeItem = {
autoIncrement: true,
},
tradeItemId: {
type: Sequelize.UUID,
type: Sequelize.STRING,
unique: true,
},
supplierOrganizationId: {
type: Sequelize.UUID
type: Sequelize.STRING
},
code: {
type: Sequelize.STRING

13
package-lock.json generated
View File

@ -882,6 +882,13 @@
"uuid": "^8.3.2",
"validator": "^13.7.0",
"wkx": "^0.5.0"
},
"dependencies": {
"uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
}
}
},
"sequelize-pool": {
@ -964,9 +971,9 @@
}
},
"uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg=="
},
"validator": {
"version": "13.7.0",

View File

@ -4,16 +4,18 @@
"type": "module",
"scripts": {
"start": "node index.js",
"dev-sync": "FORCE_SYNC=true node index.js",
"dev-secrets": "SECRETS=true node index.js",
"dev-both": "FORCE_SYNC=true SECRETS=true node index.js"
"dev-sync": "FORCE_SYNC=true node --max-old-space-size=4096 index.js",
"dev-secrets": "SECRETS=true node --max-old-space-size=4096 index.js",
"dev-both": "FORCE_SYNC=true SECRETS=true node --max-old-space-size=4096 index.js",
"dev-query": "QUERYSUPPLIERS=true FORCE_SYNC=true SECRETS=true node --max-old-space-size=4096 index.js"
},
"dependencies": {
"dotenv": "^16.0.3",
"mariadb": "^3.0.2",
"moment": "^2.29.4",
"node-fetch": "^3.3.0",
"sequelize": "^6.26.0"
"sequelize": "^6.26.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"eslint": "^8.31.0"

4462
suppliersGln.js Normal file

File diff suppressed because it is too large Load Diff

239
utils.js
View File

@ -1,7 +1,8 @@
import moment from 'moment';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
import models from './models/index.js';
import { models } from './models/index.js';
import { v4 as uuidv4 } from 'uuid';
dotenv.config();
/**
* The Endpoint where the Access Token is requested
@ -106,6 +107,24 @@ async function getJWT() {
return clientConfigData[0].currentToken;
}
/**
* 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);
});
}
/**
*
* returns every tradeitem from the organization
*
* @param {*} organizationGln
*/
async function getTradeitems(organizationGln) {
const headers = {
@ -115,7 +134,6 @@ async function getTradeitems(organizationGln) {
};
const BASE_CUSTOMER_URL = 'https://api.staging.floriday.io/customers-api/2022v2/';
//const BASE_SUPPLIER_URL = 'https://api.staging.floriday.io/suppliers-api/2022v2/';
// Get the organization id from the organizationGln
@ -136,103 +154,154 @@ async function getTradeitems(organizationGln) {
method: 'GET',
headers: headers,
});
const tradeitemsResponse = await tradeitemsRequest.json();
tradeitemsResponse.forEach(async item => {
try{
models.tradeItem.upsert({
tradeItemId: item.tradeItemId,
supplierOrganizationId: item.supplierOrganizationId,
code: item.code,
gtin: item.gtin,
vbnProductCode: item.vbnProductCode,
name: item.name,
isDeleted: item.isDeleted,
sequenceNumber: item.sequenceNumber,
tradeItemVersion: item.tradeItemVersion,
isCustomerSpecific: item.isCustomerSpecific,
isHiddenInCatalog: item.isHiddenInCatalog,
});
// if there is at least one tradeitem, save it in the database
item.characteristics.forEach((characteristic) => {
models.characteristics.upsert({
tradeItemFk: item.tradeItemId,
vnbCode: characteristic.vnbCode,
vnbValueCode: characteristic.vnbValueCode,
});
});
//console.log(tradeitemsResponse[0]);
item.seasonalPeriods.forEach((seasonalPeriod) => {
models.seasonalPeriod.upsert({
tradeItemFk: item.tradeItemId,
startWeek: seasonalPeriod.startWeek,
endWeek: seasonalPeriod.endWeek,
});
try {
});
if (tradeitemsResponse.length > 0 && tradeitemsResponse != null) {
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,
});
});
console.log(`Tradeitems for the organization ${organizationGln} : `, tradeitemsResponse.length);
item.packingConfigurations.forEach( async (packagingConfiguration) => {
let createdPackingConfig = await models.packingConfigurations.upsert({
tradeItemFk: item.tradeItemId,
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 tradeitemsResponse.forEach(async item => {
try {
if (createdPackingConfig.isNewRecord) {
item.tradeItemId = 'Vn-' + item.tradeItemId;
models.package.upsert({
packingConfigurationsFk: createdPackingConfig[0].id,
vbnPackageCode: packagingConfiguration.package.vbnPackageCode,
vbnPackageValueCode: packagingConfiguration.package.vbnPackageValueCode,
await models.tradeItem.upsert({
tradeItemId: item.tradeItemId,
supplierOrganizationId: organizationGln,
code: item.code,
gtin: item.gtin,
vbnProductCode: item.vbnProductCode,
name: item.name,
isDeleted: item.isDeleted,
sequenceNumber: item.sequenceNumber,
tradeItemVersion: item.tradeItemVersion,
isCustomerSpecific: item.isCustomerSpecific,
isHiddenInCatalog: item.isHiddenInCatalog,
});
models.additionalPricePerPiece.upsert({
packingConfigurationsFk: createdPackingConfig.dataValues.id,
currency: packagingConfiguration.additionalPricePerPiece.currency,
value: packagingConfiguration.additionalPricePerPiece.value,
});
try {
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,
});
});
} 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;
models.photos.upsert({
tradeItemFk: item.tradeItemId,
photoId: photo.id,
url: photo.url,
seasonalPeriodFk: 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) => {
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,
});
});
} 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,
});
});
} 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,
});
});
} catch (error) {
console.log('There was a error while saving the countryOfOriginIsoCodes for the tradeitem: ', item.tradeItemId);
}
} catch (error) {
console.log('There was an error while saving the data to the database');
console.log(error);
}
});
item.botanicalNames.forEach((botanicalName) => {
models.botanicalNames.upsert({
tradeItemFk: item.tradeItemId,
name: botanicalName,
});
});
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);
}
});
} catch (error) {
console.log('There was an error while saving the data to the database');
throw new Error(error);
}
}
export { getClientToken, updateClientConfig, getJWT, getTradeitems };
export { getClientToken, updateClientConfig, getJWT, getTradeitems ,sleep };