refs #4823 Added warehouses
This commit is contained in:
parent
7202a0c339
commit
0d61154b4c
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
46
utils.js
46
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);
|
||||
|
|
Loading…
Reference in New Issue