From 53b11dfca0b987979974e7fe8b2f24047fecfd18 Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Wed, 1 Feb 2023 13:23:06 +0100 Subject: [PATCH] change models --- models/{ => conf}/clientConfig.js | 3 +++ models/index.js | 24 +++++++++--------- models/{ => supplier}/suppliers.js | 0 models/{ => supplyLine}/supplyLine.js | 0 models/{ => supplyLine}/volumePrices.js | 0 models/{ => tradeItem}/botanicalNames.js | 0 models/{ => tradeItem}/characteristics.js | 0 .../countryOfOriginIsoCodes.js | 0 models/{ => tradeItem}/package.js | 0 .../{ => tradeItem}/packingConfigurations.js | 0 models/{ => tradeItem}/photos.js | 0 models/{ => tradeItem}/seasonalPeriod.js | 0 models/{ => tradeItem}/tradeItem.js | 0 utils.js | 25 +++++++++++++++---- 14 files changed, 35 insertions(+), 17 deletions(-) rename models/{ => conf}/clientConfig.js (91%) rename models/{ => supplier}/suppliers.js (100%) rename models/{ => supplyLine}/supplyLine.js (100%) rename models/{ => supplyLine}/volumePrices.js (100%) rename models/{ => tradeItem}/botanicalNames.js (100%) rename models/{ => tradeItem}/characteristics.js (100%) rename models/{ => tradeItem}/countryOfOriginIsoCodes.js (100%) rename models/{ => tradeItem}/package.js (100%) rename models/{ => tradeItem}/packingConfigurations.js (100%) rename models/{ => tradeItem}/photos.js (100%) rename models/{ => tradeItem}/seasonalPeriod.js (100%) rename models/{ => tradeItem}/tradeItem.js (100%) diff --git a/models/clientConfig.js b/models/conf/clientConfig.js similarity index 91% rename from models/clientConfig.js rename to models/conf/clientConfig.js index bc642c9..0888525 100644 --- a/models/clientConfig.js +++ b/models/conf/clientConfig.js @@ -12,6 +12,9 @@ const clientConfig = { clientSecret: { type: Sequelize.STRING, }, + requestLimit: { + type: Sequelize.INTEGER, + }, currentToken: { type: Sequelize.STRING(2000), }, diff --git a/models/index.js b/models/index.js index 6961b4b..0b8fcf8 100644 --- a/models/index.js +++ b/models/index.js @@ -4,18 +4,18 @@ dotenv.config(); let sequelize = createConnection(); -import botanicalNames from './botanicalNames.js'; -import countryOfOriginIsoCodes from './countryOfOriginIsoCodes.js'; -import packageModel from './package.js'; -import packingConfigurations from './packingConfigurations.js'; -import photos from './photos.js'; -import seasonalPeriod from './seasonalPeriod.js'; -import tradeItem from './tradeItem.js'; -import clientConfig from './clientConfig.js'; -import characteristics from './characteristics.js'; -import supplyLine from './supplyLine.js'; -import volumePrices from './volumePrices.js'; -import suppliers from './suppliers.js'; +import botanicalNames from './tradeItem/botanicalNames.js'; +import countryOfOriginIsoCodes from './tradeItem/countryOfOriginIsoCodes.js'; +import packageModel from './tradeItem/package.js'; +import packingConfigurations from './tradeItem/packingConfigurations.js'; +import photos from './tradeItem/photos.js'; +import seasonalPeriod from './tradeItem/seasonalPeriod.js'; +import tradeItem from './tradeItem/tradeItem.js'; +import clientConfig from './conf/clientConfig.js'; +import characteristics from './tradeItem/characteristics.js'; +import supplyLine from './supplyLine/supplyLine.js'; +import volumePrices from './supplyLine/volumePrices.js'; +import suppliers from './supplier/suppliers.js'; /** * Contains all the models that are related to the application. diff --git a/models/suppliers.js b/models/supplier/suppliers.js similarity index 100% rename from models/suppliers.js rename to models/supplier/suppliers.js diff --git a/models/supplyLine.js b/models/supplyLine/supplyLine.js similarity index 100% rename from models/supplyLine.js rename to models/supplyLine/supplyLine.js diff --git a/models/volumePrices.js b/models/supplyLine/volumePrices.js similarity index 100% rename from models/volumePrices.js rename to models/supplyLine/volumePrices.js diff --git a/models/botanicalNames.js b/models/tradeItem/botanicalNames.js similarity index 100% rename from models/botanicalNames.js rename to models/tradeItem/botanicalNames.js diff --git a/models/characteristics.js b/models/tradeItem/characteristics.js similarity index 100% rename from models/characteristics.js rename to models/tradeItem/characteristics.js diff --git a/models/countryOfOriginIsoCodes.js b/models/tradeItem/countryOfOriginIsoCodes.js similarity index 100% rename from models/countryOfOriginIsoCodes.js rename to models/tradeItem/countryOfOriginIsoCodes.js diff --git a/models/package.js b/models/tradeItem/package.js similarity index 100% rename from models/package.js rename to models/tradeItem/package.js diff --git a/models/packingConfigurations.js b/models/tradeItem/packingConfigurations.js similarity index 100% rename from models/packingConfigurations.js rename to models/tradeItem/packingConfigurations.js diff --git a/models/photos.js b/models/tradeItem/photos.js similarity index 100% rename from models/photos.js rename to models/tradeItem/photos.js diff --git a/models/seasonalPeriod.js b/models/tradeItem/seasonalPeriod.js similarity index 100% rename from models/seasonalPeriod.js rename to models/tradeItem/seasonalPeriod.js diff --git a/models/tradeItem.js b/models/tradeItem/tradeItem.js similarity index 100% rename from models/tradeItem.js rename to models/tradeItem/tradeItem.js diff --git a/utils.js b/utils.js index e0f2d03..70fa887 100644 --- a/utils.js +++ b/utils.js @@ -34,7 +34,7 @@ async function getClientToken() { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, - body: `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}&scope=role:app catalog:read supply:read organization:read network:read`, + body: `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}&scope=role:app catalog:read supply:read organization:read network:write network:read`, }); const tokenResponse = await tokenRequest.json(); @@ -205,6 +205,7 @@ async function getTradeitems(organizationGln) { const organizationsResponse = await organizationsRequest.json(); const organizationId = organizationsResponse.organizationId; + console.log('Organization ID: ', organizationId); // Get the tradeitems from the organization @@ -471,6 +472,11 @@ async function getTradeItem(tradeItemId) { } +/** + * Gets the stock for all suppliers + * + * Does this by getting all suppliers and then calling the getStockBySupplier function for each supplier + */ async function getStock() { const suppliers = await models.suppliers.findAll(); @@ -485,6 +491,12 @@ async function getStock() { } +/** + * Returns the stock for a given supplier + * + * @param {*} supplierId + * @returns void - inserts the stock into the database + */ async function getStockBySupplier(supplierId) { supplierId = await removeVnPrefix(supplierId); @@ -558,11 +570,14 @@ async function getStockBySupplier(supplierId) { tradeItemFk: await vnUuid(supplyLine.tradeItemId), }); - await models.volumePrices.upsert({ - unit: supplyLine.volumePrices.unit, - price: supplyLine.volumePrices.price, - supplyLineFk: await vnUuid(supplyLine.supplyLineId), + supplyLine.volumePrices.forEach(async (volumePrice) => { + await models.volumePrices.upsert({ + supplyLineFk: supplyLine.supplyLineId, + unit: volumePrice.unit, + pricePerPiece: volumePrice.pricePerPiece, + }); }); + } catch (error) { if (error.name == 'SequelizeForeignKeyConstraintError') {