279 lines
8.2 KiB
JavaScript
279 lines
8.2 KiB
JavaScript
|
import { Sequelize } from "sequelize";
|
||
|
import TradeItem from "./models/tradeItem.js";
|
||
|
import Characteristics from "./models/characteristics.js";
|
||
|
import SeasonalPeriod from "./models/seasonalPeriod.js";
|
||
|
import Photos from "./models/photos.js";
|
||
|
import PackagingConfigurations from "./models/packagingConfigurations.js";
|
||
|
import Package from "./models/package.js";
|
||
|
import AdditionalPricePerPiece from "./models/additionalPricePerPiece.js";
|
||
|
import BotanicalNames from "./models/botanicalNames.js";
|
||
|
import CountryOfOriginIsoCodes from "./models/countryOfOriginIsoCodes.js";
|
||
|
|
||
|
let sequelize = new Sequelize("edi", "root", "root", {
|
||
|
host: "localhost",
|
||
|
dialect: "mariadb",
|
||
|
logging: false,
|
||
|
});
|
||
|
|
||
|
const tradeItemModel = TradeItem(sequelize);
|
||
|
const characteristicsModel = Characteristics(sequelize);
|
||
|
const seasonalPeriodModel = SeasonalPeriod(sequelize);
|
||
|
const photosModel = Photos(sequelize);
|
||
|
const packagingConfigurationsModel = PackagingConfigurations(sequelize);
|
||
|
const packageModel = Package(sequelize);
|
||
|
const additionalPricePerPieceModel = AdditionalPricePerPiece(sequelize);
|
||
|
const botanicalNamesModel = BotanicalNames(sequelize);
|
||
|
const countryOfOriginIsoCodesModel = CountryOfOriginIsoCodes(sequelize);
|
||
|
|
||
|
try {
|
||
|
// Just there to hide the mock data insertion
|
||
|
characteristicsModel.belongsTo(tradeItemModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
targetKey: "id",
|
||
|
onDelete: "CASCADE",
|
||
|
onUpdate: "CASCADE",
|
||
|
});
|
||
|
seasonalPeriodModel.belongsTo(tradeItemModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
targetKey: "id",
|
||
|
onDelete: "CASCADE",
|
||
|
onUpdate: "CASCADE",
|
||
|
});
|
||
|
photosModel.belongsTo(tradeItemModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
targetKey: "id",
|
||
|
onDelete: "CASCADE",
|
||
|
onUpdate: "CASCADE",
|
||
|
});
|
||
|
photosModel.belongsTo(seasonalPeriodModel, {
|
||
|
foreignKey: "seasonalPeriodFk",
|
||
|
targetKey: "id",
|
||
|
onDelete: "CASCADE",
|
||
|
onUpdate: "CASCADE",
|
||
|
});
|
||
|
packagingConfigurationsModel.belongsTo(tradeItemModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
targetKey: "id",
|
||
|
onDelete: "CASCADE",
|
||
|
onUpdate: "CASCADE",
|
||
|
});
|
||
|
packageModel.belongsTo(packagingConfigurationsModel, {
|
||
|
foreignKey: "packingConfigurationsFk",
|
||
|
targetKey: "id",
|
||
|
onDelete: "CASCADE",
|
||
|
onUpdate: "CASCADE",
|
||
|
});
|
||
|
additionalPricePerPieceModel.belongsTo(packagingConfigurationsModel, {
|
||
|
foreignKey: "packingConfigurationsFk",
|
||
|
targetKey: "id",
|
||
|
onDelete: "CASCADE",
|
||
|
onUpdate: "CASCADE",
|
||
|
});
|
||
|
botanicalNamesModel.belongsTo(tradeItemModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
targetKey: "id",
|
||
|
onDelete: "CASCADE",
|
||
|
onUpdate: "CASCADE",
|
||
|
});
|
||
|
countryOfOriginIsoCodesModel.belongsTo(tradeItemModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
targetKey: "id",
|
||
|
onDelete: "CASCADE",
|
||
|
onUpdate: "CASCADE",
|
||
|
});
|
||
|
|
||
|
await sequelize.sync({
|
||
|
// alter: true,
|
||
|
force: true,
|
||
|
});
|
||
|
|
||
|
const tradeItem = await tradeItemModel.create({
|
||
|
tradeItemId: "123",
|
||
|
supplierOrganizationId: "123",
|
||
|
code: "123",
|
||
|
gtin: "123",
|
||
|
vbnProductCode: "123",
|
||
|
name: "123",
|
||
|
isDeleted: false,
|
||
|
sequenceNumber: 1,
|
||
|
tradeItemVersion: 1,
|
||
|
isCustomerSpecific: false,
|
||
|
isHiddenInCatalog: false,
|
||
|
});
|
||
|
const characteristics1 = await characteristicsModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
vnbCode: "123",
|
||
|
vnbValueCode: "123",
|
||
|
});
|
||
|
const characteristics2 = await characteristicsModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
vnbCode: "234",
|
||
|
vnbValueCode: "234",
|
||
|
});
|
||
|
const seasonalPeriod1 = await seasonalPeriodModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
startWeek: "40",
|
||
|
endWeek: "42",
|
||
|
});
|
||
|
const seasonalPeriod2 = await seasonalPeriodModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
startWeek: "43",
|
||
|
endWeek: "45",
|
||
|
});
|
||
|
const photos1 = await photosModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
seasonalPeriodFk: seasonalPeriod1.id,
|
||
|
photoId: "123",
|
||
|
url: "123",
|
||
|
type: "123",
|
||
|
primary: false,
|
||
|
});
|
||
|
const photos2 = await photosModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
seasonalPeriodFk: seasonalPeriod2.id,
|
||
|
photoId: "234",
|
||
|
url: "234",
|
||
|
type: "234",
|
||
|
primary: false,
|
||
|
});
|
||
|
const packagingConfigurations1 = await packagingConfigurationsModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
piecesPerPackage: 1,
|
||
|
bunchesPerPackage: 1,
|
||
|
photoUrl: photos1.url,
|
||
|
});
|
||
|
const packagingConfigurations2 = await packagingConfigurationsModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
piecesPerPackage: 2,
|
||
|
bunchesPerPackage: 2,
|
||
|
photoUrl: photos2.url,
|
||
|
});
|
||
|
const package1 = await packageModel.create({
|
||
|
packingConfigurationsFk: packagingConfigurations1.id,
|
||
|
vbnPackageCode: "123",
|
||
|
customPackageId: "123",
|
||
|
});
|
||
|
const package2 = await packageModel.create({
|
||
|
packingConfigurationsFk: packagingConfigurations2.id,
|
||
|
vbnPackageCode: "234",
|
||
|
customPackageId: "234",
|
||
|
});
|
||
|
const additionalPricePerPiece1 = await additionalPricePerPieceModel.create({
|
||
|
packingConfigurationsFk: packagingConfigurations1.id,
|
||
|
value: 1,
|
||
|
currency: "EUR",
|
||
|
});
|
||
|
const additionalPricePerPiece2 = await additionalPricePerPieceModel.create({
|
||
|
packingConfigurationsFk: packagingConfigurations2.id,
|
||
|
value: 2,
|
||
|
currency: "USD",
|
||
|
});
|
||
|
const botanicalNames1 = await botanicalNamesModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
name: "test123",
|
||
|
});
|
||
|
const botanicalNames2 = await botanicalNamesModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
name: "test234",
|
||
|
});
|
||
|
const countryOfOriginIsoCodes1 = await countryOfOriginIsoCodesModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
isoCode: "ES",
|
||
|
});
|
||
|
const countryOfOriginIsoCodes2 = await countryOfOriginIsoCodesModel.create({
|
||
|
tradeItemFk: tradeItem.id,
|
||
|
isoCode: "DE",
|
||
|
});
|
||
|
} catch (error) {
|
||
|
console.log(error);
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// Every 30 sec query the database
|
||
|
setInterval(async () => {
|
||
|
console.log("Querying the API to check for new data...");
|
||
|
|
||
|
const query = tradeItemModel.findAll({
|
||
|
include: [
|
||
|
{
|
||
|
model: characteristicsModel,
|
||
|
association: tradeItemModel.hasMany(characteristicsModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
sourceKey: "id",
|
||
|
}),
|
||
|
as: "characteristics",
|
||
|
},
|
||
|
{
|
||
|
model: photosModel,
|
||
|
association: tradeItemModel.hasMany(photosModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
sourceKey: "id",
|
||
|
}),
|
||
|
as: "photos",
|
||
|
include: [
|
||
|
{
|
||
|
model: seasonalPeriodModel,
|
||
|
association: photosModel.hasOne(seasonalPeriodModel, {
|
||
|
foreignKey: "id",
|
||
|
sourceKey: "seasonalPeriodFk",
|
||
|
}),
|
||
|
as: "seasonalPeriod",
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
{
|
||
|
model: packagingConfigurationsModel,
|
||
|
association: tradeItemModel.hasMany(packagingConfigurationsModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
sourceKey: "id",
|
||
|
}),
|
||
|
as: "packagingConfigurations",
|
||
|
include: [
|
||
|
{
|
||
|
model: packageModel,
|
||
|
association: packagingConfigurationsModel.hasOne(packageModel, {
|
||
|
foreignKey: "packingConfigurationsFk",
|
||
|
sourceKey: "id",
|
||
|
}),
|
||
|
as: "package",
|
||
|
},
|
||
|
{
|
||
|
model: additionalPricePerPieceModel,
|
||
|
association: packagingConfigurationsModel.hasOne(
|
||
|
additionalPricePerPieceModel,
|
||
|
{
|
||
|
foreignKey: "packingConfigurationsFk",
|
||
|
sourceKey: "id",
|
||
|
}
|
||
|
),
|
||
|
as: "additionalPricePerPiece",
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
{
|
||
|
model: botanicalNamesModel,
|
||
|
association: tradeItemModel.hasMany(botanicalNamesModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
sourceKey: "id",
|
||
|
}),
|
||
|
as: "botanicalNames",
|
||
|
},
|
||
|
{
|
||
|
model: countryOfOriginIsoCodesModel,
|
||
|
association: tradeItemModel.hasMany(countryOfOriginIsoCodesModel, {
|
||
|
foreignKey: "tradeItemFk",
|
||
|
sourceKey: "id",
|
||
|
}),
|
||
|
as: "countryOfOriginIsoCodes",
|
||
|
},
|
||
|
],
|
||
|
});
|
||
|
|
||
|
const result = await query;
|
||
|
|
||
|
console.log(JSON.stringify(result, null, 2));
|
||
|
}, 30000);
|
||
|
} catch (error) {
|
||
|
console.error("Unable to connect to the database:", error);
|
||
|
}
|