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.
|
* 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 = {
|
let models = {
|
||||||
sequelize: sequelize,
|
sequelize: sequelize,
|
||||||
|
@ -93,67 +76,61 @@ try {
|
||||||
foreignKey: 'tradeItemId',
|
foreignKey: 'tradeItemId',
|
||||||
targetKey: 'tradeItemId',
|
targetKey: 'tradeItemId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.seasonalPeriod.belongsTo(models.tradeItem, {
|
models.seasonalPeriod.belongsTo(models.tradeItem, {
|
||||||
foreignKey: 'tradeItemId',
|
foreignKey: 'tradeItemId',
|
||||||
targetKey: 'tradeItemId',
|
targetKey: 'tradeItemId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.photo.belongsTo(models.tradeItem, {
|
models.photo.belongsTo(models.tradeItem, {
|
||||||
foreignKey: 'tradeItemId',
|
foreignKey: 'tradeItemId',
|
||||||
targetKey: 'tradeItemId',
|
targetKey: 'tradeItemId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.packingConfiguration.belongsTo(models.tradeItem, {
|
models.packingConfiguration.belongsTo(models.tradeItem, {
|
||||||
foreignKey: 'tradeItemId',
|
foreignKey: 'tradeItemId',
|
||||||
targetKey: 'tradeItemId',
|
targetKey: 'tradeItemId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.packingConfiguration.hasMany(models.package, {
|
models.packingConfiguration.hasMany(models.package, {
|
||||||
foreignKey: 'packingConfigurationId',
|
foreignKey: 'packingConfigurationId',
|
||||||
targetKey: 'packingConfigurationId',
|
targetKey: 'packingConfigurationId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.package.belongsTo(models.packingConfiguration, {
|
models.package.belongsTo(models.packingConfiguration, {
|
||||||
foreignKey: 'packingConfigurationId',
|
foreignKey: 'packingConfigurationId',
|
||||||
targetKey: 'packingConfigurationId',
|
targetKey: 'packingConfigurationId',
|
||||||
});
|
|
||||||
|
|
||||||
|
});
|
||||||
models.botanicalName.belongsTo(models.tradeItem, {
|
models.botanicalName.belongsTo(models.tradeItem, {
|
||||||
foreignKey: 'tradeItemId',
|
foreignKey: 'tradeItemId',
|
||||||
targetKey: 'tradeItemId',
|
targetKey: 'tradeItemId',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
onUpdate: 'CASCADE',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
|
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
|
||||||
foreignKey: 'tradeItemId',
|
foreignKey: 'tradeItemId',
|
||||||
targetKey: 'tradeItemId',
|
targetKey: 'tradeItemId',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
onUpdate: 'CASCADE',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.volumePrices.belongsTo(models.supplyLine, {
|
models.volumePrices.belongsTo(models.supplyLine, {
|
||||||
foreignKey: 'supplyLineId',
|
foreignKey: 'supplyLineId',
|
||||||
targetKey: 'supplyLineId',
|
targetKey: 'supplyLineId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.supplyLine.belongsTo(models.tradeItem, {
|
models.supplyLine.belongsTo(models.tradeItem, {
|
||||||
foreignKey: 'tradeItemId',
|
foreignKey: 'tradeItemId',
|
||||||
targetKey: 'tradeItemId',
|
targetKey: 'tradeItemId',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
onUpdate: 'CASCADE',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.supplyLine.belongsTo(models.warehouses, {
|
models.supplyLine.belongsTo(models.warehouses, {
|
||||||
foreignKey: 'warehouseId',
|
foreignKey: 'warehouseId',
|
||||||
targetKey: 'warehouseId',
|
targetKey: 'warehouseId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.tradeItem.belongsTo(models.organization, {
|
models.tradeItem.belongsTo(models.organization, {
|
||||||
foreignKey: 'organizationId',
|
foreignKey: 'organizationId',
|
||||||
targetKey: 'organizationId',
|
targetKey: 'organizationId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.supplyLine.belongsTo(models.organization, {
|
models.supplyLine.belongsTo(models.organization, {
|
||||||
foreignKey: 'organizationId',
|
foreignKey: 'organizationId',
|
||||||
targetKey: 'organizationId',
|
targetKey: 'organizationId',
|
||||||
});
|
});
|
||||||
|
|
||||||
models.warehouses.belongsTo(models.organization, {
|
models.warehouses.belongsTo(models.organization, {
|
||||||
foreignKey: 'organizationId',
|
foreignKey: 'organizationId',
|
||||||
targetKey: '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
|
* Sync the supply lines for organizations that are connected
|
||||||
|
*
|
||||||
|
* If necessary, create the item or the warehouse
|
||||||
*/
|
*/
|
||||||
export async function syncSupplyLines() {
|
export async function syncSupplyLines() {
|
||||||
const spinner = ora(`Syncing supply lines...`).start();
|
const spinner = ora(`Syncing supply lines...`).start();
|
||||||
try {
|
try {
|
||||||
let suppliersWithTradeItem = await models.tradeItem.findAll({
|
|
||||||
attributes: ['organizationId'],
|
|
||||||
group: ['organizationId']
|
|
||||||
});
|
|
||||||
|
|
||||||
let connectedSuppliers = await models.organization.findAll({
|
let connectedSuppliers = await models.organization.findAll({
|
||||||
attributes: ['organizationId'],
|
attributes: ['organizationId'],
|
||||||
where: { isConnected: true }
|
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;
|
let i = 0, x = 1;
|
||||||
for (let supplier of suppliers) {
|
for (let supplier of connectedSuppliers) {
|
||||||
spinner.text = `Syncing ${i} supply lines of [${x++}|${suppliers.length}] suppliers...`
|
spinner.text = `Syncing ${i} supply lines of [${x++}|${connectedSuppliers.length}] suppliers...`
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
supplierOrganizationId: supplier,
|
supplierOrganizationId: supplier.organizationId,
|
||||||
}).toString();
|
}).toString();
|
||||||
let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`)).data;
|
let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`)).data;
|
||||||
if (!supplyLines.length) continue
|
if (!supplyLines.length) continue
|
||||||
|
|
||||||
for (let supplyLine of supplyLines) {
|
for (let supplyLine of supplyLines) {
|
||||||
|
// Check if the trade item exists, and if it doesn't, create it
|
||||||
let tradeItem = await models.tradeItem.findOne({
|
let tradeItem = await models.tradeItem.findOne({
|
||||||
where: { tradeItemId: supplyLine.tradeItemId }
|
where: { tradeItemId: supplyLine.tradeItemId }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!tradeItem) {
|
if (!tradeItem) {
|
||||||
let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items?tradeItemIds=${supplyLine.tradeItemId}`)).data;
|
let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items/${supplyLine.tradeItemId}`)).data;
|
||||||
insertItem(tradeItem[0])
|
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({
|
await models.supplyLine.upsert({
|
||||||
...supplyLine,
|
...supplyLine,
|
||||||
organizationId: supplyLine.supplierOrganizationId,
|
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
|
* @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
|
* 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;
|
const warehouses = (await vnRequest('GET', `${env.API_URL}/organizations/supplier/${supplier.organizationId}/warehouses`)).data;
|
||||||
for (let warehouse of warehouses) {
|
for (let warehouse of warehouses) {
|
||||||
spinner.text = `Syncing ${i++} warehouses of [${x}|${suppliers.length}] suppliers...`
|
spinner.text = `Syncing ${i++} warehouses of [${x}|${suppliers.length}] suppliers...`
|
||||||
await models.warehouses.upsert({
|
await insertWarehouse(warehouse);
|
||||||
...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(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
|
|
Loading…
Reference in New Issue