refs #4823 Now syncSupplyLines can create depend.
This commit is contained in:
parent
5a0f4726ed
commit
689371fd3f
|
@ -52,23 +52,6 @@ import characteristics from './tradeItem/characteristics.js';
|
|||
|
||||
/**
|
||||
* Contains all the models that are related to the application.
|
||||
*
|
||||
* @example
|
||||
* models.@modelName@.findAll();
|
||||
*
|
||||
* @example
|
||||
* models.tradeItem.findAll().then((data) => {
|
||||
* console.log(data);
|
||||
* });
|
||||
*
|
||||
* @example
|
||||
* models.tradeItem.create({
|
||||
* tradeItemName: 'Test',
|
||||
* foo: 'bar',
|
||||
* });
|
||||
*
|
||||
*
|
||||
* @type {Object.<string, Sequelize.Model>}
|
||||
*/
|
||||
let models = {
|
||||
sequelize: sequelize,
|
||||
|
@ -93,67 +76,61 @@ try {
|
|||
foreignKey: 'tradeItemId',
|
||||
targetKey: 'tradeItemId',
|
||||
});
|
||||
|
||||
models.seasonalPeriod.belongsTo(models.tradeItem, {
|
||||
foreignKey: 'tradeItemId',
|
||||
targetKey: 'tradeItemId',
|
||||
});
|
||||
|
||||
models.photo.belongsTo(models.tradeItem, {
|
||||
foreignKey: 'tradeItemId',
|
||||
targetKey: 'tradeItemId',
|
||||
});
|
||||
|
||||
models.packingConfiguration.belongsTo(models.tradeItem, {
|
||||
foreignKey: 'tradeItemId',
|
||||
targetKey: 'tradeItemId',
|
||||
});
|
||||
|
||||
models.packingConfiguration.hasMany(models.package, {
|
||||
foreignKey: 'packingConfigurationId',
|
||||
targetKey: 'packingConfigurationId',
|
||||
});
|
||||
|
||||
models.package.belongsTo(models.packingConfiguration, {
|
||||
foreignKey: 'packingConfigurationId',
|
||||
targetKey: 'packingConfigurationId',
|
||||
|
||||
});
|
||||
|
||||
models.botanicalName.belongsTo(models.tradeItem, {
|
||||
foreignKey: 'tradeItemId',
|
||||
targetKey: 'tradeItemId',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
});
|
||||
|
||||
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
|
||||
foreignKey: 'tradeItemId',
|
||||
targetKey: 'tradeItemId',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
});
|
||||
|
||||
models.volumePrices.belongsTo(models.supplyLine, {
|
||||
foreignKey: 'supplyLineId',
|
||||
targetKey: 'supplyLineId',
|
||||
});
|
||||
|
||||
models.supplyLine.belongsTo(models.tradeItem, {
|
||||
foreignKey: 'tradeItemId',
|
||||
targetKey: 'tradeItemId',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
});
|
||||
|
||||
models.supplyLine.belongsTo(models.warehouses, {
|
||||
foreignKey: 'warehouseId',
|
||||
targetKey: 'warehouseId',
|
||||
});
|
||||
|
||||
models.tradeItem.belongsTo(models.organization, {
|
||||
foreignKey: 'organizationId',
|
||||
targetKey: 'organizationId',
|
||||
});
|
||||
|
||||
models.supplyLine.belongsTo(models.organization, {
|
||||
foreignKey: 'organizationId',
|
||||
targetKey: 'organizationId',
|
||||
});
|
||||
|
||||
models.warehouses.belongsTo(models.organization, {
|
||||
foreignKey: 'organizationId',
|
||||
targetKey: 'organizationId',
|
||||
|
|
77
utils.js
77
utils.js
|
@ -301,47 +301,47 @@ export async function syncTradeItems(){
|
|||
|
||||
/**
|
||||
* Sync the supply lines for organizations that are connected
|
||||
*
|
||||
* If necessary, create the item or the warehouse
|
||||
*/
|
||||
export async function syncSupplyLines() {
|
||||
const spinner = ora(`Syncing supply lines...`).start();
|
||||
try {
|
||||
let suppliersWithTradeItem = await models.tradeItem.findAll({
|
||||
attributes: ['organizationId'],
|
||||
group: ['organizationId']
|
||||
});
|
||||
|
||||
let connectedSuppliers = await models.organization.findAll({
|
||||
attributes: ['organizationId'],
|
||||
where: { isConnected: true }
|
||||
});
|
||||
|
||||
let suppliers = suppliersWithTradeItem.filter(supplier => {
|
||||
return connectedSuppliers.some(connectedSupplier => {
|
||||
return connectedSupplier.organizationId === supplier.organizationId;
|
||||
});
|
||||
}).map(supplier => supplier.organizationId);
|
||||
|
||||
let i = 0, x = 1;
|
||||
for (let supplier of suppliers) {
|
||||
spinner.text = `Syncing ${i} supply lines of [${x++}|${suppliers.length}] suppliers...`
|
||||
for (let supplier of connectedSuppliers) {
|
||||
spinner.text = `Syncing ${i} supply lines of [${x++}|${connectedSuppliers.length}] suppliers...`
|
||||
const params = new URLSearchParams({
|
||||
supplierOrganizationId: supplier,
|
||||
supplierOrganizationId: supplier.organizationId,
|
||||
}).toString();
|
||||
let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`)).data;
|
||||
if (!supplyLines.length) continue
|
||||
|
||||
for (let supplyLine of supplyLines) {
|
||||
|
||||
// Check if the trade item exists, and if it doesn't, create it
|
||||
let tradeItem = await models.tradeItem.findOne({
|
||||
where: { tradeItemId: supplyLine.tradeItemId }
|
||||
});
|
||||
|
||||
if (!tradeItem) {
|
||||
let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items?tradeItemIds=${supplyLine.tradeItemId}`)).data;
|
||||
insertItem(tradeItem[0])
|
||||
let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items/${supplyLine.tradeItemId}`)).data;
|
||||
await insertItem(tradeItem);
|
||||
}
|
||||
|
||||
spinner.text = `Syncing ${i++} supply lines of [${x}|${suppliers.length}] suppliers...`
|
||||
// Check if the warehouse exists, and if it doesn't, create it
|
||||
let warehouse = await models.warehouses.findOne({
|
||||
where: { warehouseId: supplyLine.warehouseId }
|
||||
});
|
||||
if (!warehouse) {
|
||||
let warehouse = (await vnRequest('GET', `${env.API_URL}/warehouses/${supplyLine.warehouseId}`)).data;
|
||||
await insertWarehouse(warehouse);
|
||||
}
|
||||
|
||||
|
||||
spinner.text = `Syncing ${i++} supply lines of [${x}|${connectedSuppliers.length}] suppliers...`
|
||||
await models.supplyLine.upsert({
|
||||
...supplyLine,
|
||||
organizationId: supplyLine.supplierOrganizationId,
|
||||
|
@ -372,7 +372,7 @@ export async function syncSupplyLines() {
|
|||
}
|
||||
|
||||
/**
|
||||
* Insert the trade item
|
||||
* Insert trade item and dependences in db
|
||||
*
|
||||
* @param {array} tradeItem
|
||||
*/
|
||||
|
@ -465,6 +465,32 @@ export async function insertItem(tradeItem) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert warehouse in db
|
||||
*
|
||||
* @param {array} warehouse
|
||||
*/
|
||||
export async function insertWarehouse(warehouse) {
|
||||
let tx;
|
||||
try {
|
||||
tx = await models.sequelize.transaction();
|
||||
await models.warehouses.upsert({
|
||||
...warehouse,
|
||||
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(),
|
||||
});
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
await tx.rollback();
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync the warehouses for organizations that are connected
|
||||
**/
|
||||
|
@ -481,16 +507,7 @@ export async function syncWarehouses(){
|
|||
const warehouses = (await vnRequest('GET', `${env.API_URL}/organizations/supplier/${supplier.organizationId}/warehouses`)).data;
|
||||
for (let warehouse of warehouses) {
|
||||
spinner.text = `Syncing ${i++} warehouses of [${x}|${suppliers.length}] suppliers...`
|
||||
await models.warehouses.upsert({
|
||||
...warehouse,
|
||||
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(),
|
||||
});
|
||||
await insertWarehouse(warehouse);
|
||||
}
|
||||
}
|
||||
spinner.succeed();
|
||||
|
|
Loading…
Reference in New Issue