diff --git a/models/sequelize.js b/models/sequelize.js index 1d49fb0..a54916b 100644 --- a/models/sequelize.js +++ b/models/sequelize.js @@ -76,11 +76,11 @@ let models = { seasonalPeriod: seasonalPeriod(sequelize), supplyLine: supplyLine(sequelize), supplyLinePackingConfiguration: supplyLinePackingConfiguration(sequelize), + volumePrice: volumePrice(sequelize), + package: packageModel(sequelize), + clockPresaleSupply: clockPresaleSupply(sequelize), clockPresaleSupplyPackingConfiguration: clockPresaleSupplyPackingConfiguration(sequelize), clockPresaleSupplyPackage: clockPresaleSupplyPackage(sequelize), - volumePrice: volumePrice(sequelize), - clockPresaleSupply: clockPresaleSupply(sequelize), - package: packageModel(sequelize), }; try { diff --git a/utils.js b/utils.js index 111fcb2..fc118d2 100644 --- a/utils.js +++ b/utils.js @@ -252,10 +252,14 @@ export async function insertSequenceNumber(model, sequenceNumber) { export async function insertTradeItems(tradeItems) { const tx = await models.sequelize.transaction(); try { + const dbData = await models.tradeItem.findAll(); let filteredTradeItems = []; - for (let tradeItem of tradeItems) - if (!tradeItem.isDeleted) filteredTradeItems.push(tradeItem); + tradeItems.forEach(value => { + if (!value.isDeleted || dbData.includes(value.tradeItemId)) + filteredTradeItems.push(value); + }); + const tradeItemsData = filteredTradeItems.map((tradeItem) => ({ ...tradeItem, organizationId: tradeItem.supplierOrganizationId, @@ -406,9 +410,13 @@ export async function insertTradeItems(tradeItems) { export async function insertClockPresalesSupply(clockPresalesSupply) { const tx = await models.sequelize.transaction(); try { + const dbData = await models.clockPresaleSupply.findAll(); let filteredclockPresalesSupply = []; - for (let clockPresaleSupply of clockPresalesSupply) - if (!(clockPresaleSupply.status === 'UNAVAILABLE')) filteredclockPresalesSupply.push(clockPresaleSupply); + + clockPresalesSupply.forEach(value => { + if (!(value.status === 'UNAVAILABLE') || dbData.includes(value.supplyLineId)) + filteredclockPresalesSupply.push(value); + }); const clockPresalesSupplyWithDefaults = filteredclockPresalesSupply.map((clockPresaleSupply) => ({ ...clockPresaleSupply, @@ -492,9 +500,13 @@ export async function insertClockPresalesSupply(clockPresalesSupply) { export async function insertWarehouses(warehouses) { const tx = await models.sequelize.transaction(); try { + const dbData = await models.warehouse.findAll(); let filteredWarehouses = []; - for (let warehouse of warehouses) - if (!warehouse.isDeleted) filteredWarehouses.push(warehouse); + + warehouses.forEach(value => { + if (!value.isDeleted || dbData.includes(value.warehouseId)) + filteredWarehouses.push(value); + }); const warehousesWithDefaults = filteredWarehouses.map((warehouse) => ({ ...warehouse, @@ -614,9 +626,13 @@ export async function insertOrganizations(organizations) { export async function insertSupplyLines(supplyLines) { const tx = await models.sequelize.transaction(); try { + const dbData = await models.supplyLine.findAll(); let filteredSupplyLines = []; - for (let supplyLine of supplyLines) - if (!supplyLine.isDeleted) filteredSupplyLines.push(supplyLine); + + supplyLines.forEach(value => { + if ((!value.isDeleted && !(value.status === 'UNAVAILABLE')) || dbData.includes(value.supplyLineId)) + filteredSupplyLines.push(value); + }); const supplyLinesData = filteredSupplyLines.map((supplyLine) => ({ ...supplyLine,