From 0d61154b4c17b2d411fd80f022aa1b6b30539629 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 16 May 2023 10:24:04 +0200 Subject: [PATCH] refs #4823 Added warehouses --- floriday.js | 1 + models/sequelize.js | 8 +++++ models/supplier/warehouses.js | 59 +++++++++++++++++++++++++++++++++++ utils.js | 46 +++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 models/supplier/warehouses.js diff --git a/floriday.js b/floriday.js index d087fca..a8805a7 100644 --- a/floriday.js +++ b/floriday.js @@ -13,6 +13,7 @@ class Floriday { await utils.requestToken(); if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence(); if (JSON.parse(env.SYNC_SUPPLIER)) await utils.syncSuppliers(); + if (JSON.parse(env.SYNC_WAREHOUSE)) await utils.syncWarehouses(); if (JSON.parse(env.SYNC_CONN)) await utils.syncConn(); if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems(); } catch (err) { diff --git a/models/sequelize.js b/models/sequelize.js index 13cf5bb..0bd122d 100644 --- a/models/sequelize.js +++ b/models/sequelize.js @@ -40,6 +40,7 @@ import sequenceNumber from './conf/sequenceNumber.js'; // Supplier Models import supplier from './supplier/supplier.js'; import connections from './supplier/connections.js'; +import warehouses from './supplier/warehouses.js'; // TradeItem Models import tradeItem from './tradeItem/tradeItem.js'; @@ -87,6 +88,7 @@ let models = { supplier: supplier(sequelize), sequenceNumber: sequenceNumber(sequelize), connection: connections(sequelize), + warehouses: warehouses(sequelize), }; try { @@ -169,6 +171,12 @@ try { as: 'supplierOrganization_Id', targetKey: 'supplierOrganizationId', }); + + models.warehouses.belongsTo(models.supplier, { + foreignKey: 'supplierOrganizationId', + as: 'supplierOrganization_Id', + targetKey: 'supplierOrganizationId', + }); } catch (err) { criticalError(err); } diff --git a/models/supplier/warehouses.js b/models/supplier/warehouses.js new file mode 100644 index 0000000..bc23320 --- /dev/null +++ b/models/supplier/warehouses.js @@ -0,0 +1,59 @@ +import { Sequelize } from 'sequelize'; + +const warehouses = { + warehouseId: { + type: Sequelize.STRING, + primaryKey: true, + }, + name: { + type: Sequelize.STRING, + }, + location_gln: { + type: Sequelize.STRING, + }, + location_address_addressLine : { + type: Sequelize.STRING, + }, + location_address_city: { + type: Sequelize.STRING, + }, + location_address_countryCode: { + type: Sequelize.STRING, + }, + location_address_postalCode: { + type: Sequelize.STRING, + }, + location_address_stateOrProvince: { + type: Sequelize.STRING, + }, + isDeleted: { + type: Sequelize.BOOLEAN, + }, + sequenceNumber: { + type: Sequelize.INTEGER, + }, + supplierOrganizationId: { + type: Sequelize.STRING, + }, + lastSync: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.NOW, + }, + created: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.NOW, + }, +}; + +export default (sequelize) => { + const Warehouses = sequelize.define( + 'supplier_warehouses', + warehouses, { + timestamps: false, + freezeTableName: true, + } + ); + return Warehouses; +}; \ No newline at end of file diff --git a/utils.js b/utils.js index be6ce19..7fdc836 100644 --- a/utils.js +++ b/utils.js @@ -679,6 +679,45 @@ export async function insertItem(tradeItem) { } } +/** + * Sync the warehouses + **/ +export async function syncWarehouses(){ + let spinner = ora('Syncing warehouses...').start(); + try { + let headers = { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${await getCurrentToken()}`, + 'X-Api-Key': process.env.API_KEY, + }; + + const suppliers = await models.supplier.findAll(); + let x = 0, i = 1; + for (let supplier of suppliers) { + spinner.text = `Syncing ${i} warehouses of [${x++}|${suppliers.length}]...` + const warehouses = (await vnRequest('GET', `${env.API_URL}/organizations/supplier/${supplier.supplierOrganizationId}/warehouses`, null, headers)).data; + for (let warehouse of warehouses) { + spinner.text = `Syncing ${i++} warehouses of [${x}|${suppliers.length}]...` + await models.warehouses.upsert({ + ...warehouse, + supplierOrganizationId: warehouse.organizationId, + location_gln: warehouse.location.gln, + location_address_addressLine: warehouse.location.address.addressLine, + location_address_city: warehouse.location.address.city, + location_address_countryCode: warehouse.location.address.countryCode, + location_address_postalCode: warehouse.location.address.postalCode, + location_address_stateOrProvince: warehouse.location.address.stateOrProvince, + lastSync: moment(), + }); + } + } + } + catch (err) { + spinner.fail(); + throw new Error(err); + } +} + /** * Deletes the connections in Floriday **/ @@ -723,6 +762,7 @@ export async function vnRequest(method, url, data, headers) { } catch (err) { switch (err.code) { case 'ECONNRESET': // Client network socket TLS + case 'EAI_AGAIN': // getaddrinfo warning(err); await sleep(1000); break; @@ -730,9 +770,15 @@ export async function vnRequest(method, url, data, headers) { case 'ECONNREFUSED': case 'ERR_BAD_REQUEST': switch (err.response.status) { + case 504: + case 502: + warning(err); + await sleep(1000); + break; case 429: // Too Many Requests warning(err); await sleep(3400); // Stipulated by floryday + break; case 401: // Unauthorized warning(err); await requestToken(true);