refs #4823 Added warehouses

This commit is contained in:
Guillermo Bonet 2023-05-16 10:24:04 +02:00
parent 7202a0c339
commit 0d61154b4c
4 changed files with 114 additions and 0 deletions

View File

@ -13,6 +13,7 @@ class Floriday {
await utils.requestToken(); await utils.requestToken();
if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence(); if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence();
if (JSON.parse(env.SYNC_SUPPLIER)) await utils.syncSuppliers(); 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_CONN)) await utils.syncConn();
if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems(); if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems();
} catch (err) { } catch (err) {

View File

@ -40,6 +40,7 @@ import sequenceNumber from './conf/sequenceNumber.js';
// Supplier Models // Supplier Models
import supplier from './supplier/supplier.js'; import supplier from './supplier/supplier.js';
import connections from './supplier/connections.js'; import connections from './supplier/connections.js';
import warehouses from './supplier/warehouses.js';
// TradeItem Models // TradeItem Models
import tradeItem from './tradeItem/tradeItem.js'; import tradeItem from './tradeItem/tradeItem.js';
@ -87,6 +88,7 @@ let models = {
supplier: supplier(sequelize), supplier: supplier(sequelize),
sequenceNumber: sequenceNumber(sequelize), sequenceNumber: sequenceNumber(sequelize),
connection: connections(sequelize), connection: connections(sequelize),
warehouses: warehouses(sequelize),
}; };
try { try {
@ -169,6 +171,12 @@ try {
as: 'supplierOrganization_Id', as: 'supplierOrganization_Id',
targetKey: 'supplierOrganizationId', targetKey: 'supplierOrganizationId',
}); });
models.warehouses.belongsTo(models.supplier, {
foreignKey: 'supplierOrganizationId',
as: 'supplierOrganization_Id',
targetKey: 'supplierOrganizationId',
});
} catch (err) { } catch (err) {
criticalError(err); criticalError(err);
} }

View File

@ -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;
};

View File

@ -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 * Deletes the connections in Floriday
**/ **/
@ -723,6 +762,7 @@ export async function vnRequest(method, url, data, headers) {
} catch (err) { } catch (err) {
switch (err.code) { switch (err.code) {
case 'ECONNRESET': // Client network socket TLS case 'ECONNRESET': // Client network socket TLS
case 'EAI_AGAIN': // getaddrinfo
warning(err); warning(err);
await sleep(1000); await sleep(1000);
break; break;
@ -730,9 +770,15 @@ export async function vnRequest(method, url, data, headers) {
case 'ECONNREFUSED': case 'ECONNREFUSED':
case 'ERR_BAD_REQUEST': case 'ERR_BAD_REQUEST':
switch (err.response.status) { switch (err.response.status) {
case 504:
case 502:
warning(err);
await sleep(1000);
break;
case 429: // Too Many Requests case 429: // Too Many Requests
warning(err); warning(err);
await sleep(3400); // Stipulated by floryday await sleep(3400); // Stipulated by floryday
break;
case 401: // Unauthorized case 401: // Unauthorized
warning(err); warning(err);
await requestToken(true); await requestToken(true);