From fa34a22c02915f864081d8d22d80039b1ee47c10 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 29 Jun 2023 07:50:05 +0200 Subject: [PATCH] refs #4823 Added new supplyLineClockPresale models --- models/sequelize.js | 5 +- models/supplyLine/clockPresaleSupply.js | 3 - .../supplyLine/clockPresaleSupplyPackage.js | 26 ++++++++ .../clockPresaleSupplyPackingConfiguration.js | 45 +++++++++++++ utils.js | 64 +++++++++++++++++-- 5 files changed, 134 insertions(+), 9 deletions(-) create mode 100644 models/supplyLine/clockPresaleSupplyPackage.js create mode 100644 models/supplyLine/clockPresaleSupplyPackingConfiguration.js diff --git a/models/sequelize.js b/models/sequelize.js index 885809b..1d49fb0 100644 --- a/models/sequelize.js +++ b/models/sequelize.js @@ -44,6 +44,8 @@ import supplyLine from './supplyLine/supplyLine.js'; import volumePrice from './supplyLine/volumePrice.js'; import supplyLinePackingConfiguration from './supplyLine/packingConfiguration.js'; import clockPresaleSupply from './supplyLine/clockPresaleSupply.js'; +import clockPresaleSupplyPackingConfiguration from './supplyLine/clockPresaleSupplyPackingConfiguration.js'; +import clockPresaleSupplyPackage from './supplyLine/clockPresaleSupplyPackage.js'; // TradeItem Models import tradeItem from './tradeItem/tradeItem.js'; @@ -54,7 +56,6 @@ import packingConfiguration from './tradeItem/packingConfiguration.js'; import photo from './tradeItem/photo.js'; import seasonalPeriod from './tradeItem/seasonalPeriod.js'; import characteristic from './tradeItem/characteristic.js'; -import { start } from 'repl'; /** * Contains all the models that are related to the application. @@ -75,6 +76,8 @@ let models = { seasonalPeriod: seasonalPeriod(sequelize), supplyLine: supplyLine(sequelize), supplyLinePackingConfiguration: supplyLinePackingConfiguration(sequelize), + clockPresaleSupplyPackingConfiguration: clockPresaleSupplyPackingConfiguration(sequelize), + clockPresaleSupplyPackage: clockPresaleSupplyPackage(sequelize), volumePrice: volumePrice(sequelize), clockPresaleSupply: clockPresaleSupply(sequelize), package: packageModel(sequelize), diff --git a/models/supplyLine/clockPresaleSupply.js b/models/supplyLine/clockPresaleSupply.js index b236abb..f7ca5be 100644 --- a/models/supplyLine/clockPresaleSupply.js +++ b/models/supplyLine/clockPresaleSupply.js @@ -23,9 +23,6 @@ const clockPresaleSupply = { numberOfPieces: { type: Sequelize.INTEGER, }, - packingConfigurations: { - type: Sequelize.JSON, - }, tradePeriod_startDateTime: { type: Sequelize.DATE, }, diff --git a/models/supplyLine/clockPresaleSupplyPackage.js b/models/supplyLine/clockPresaleSupplyPackage.js new file mode 100644 index 0000000..fcbdf73 --- /dev/null +++ b/models/supplyLine/clockPresaleSupplyPackage.js @@ -0,0 +1,26 @@ +import { Sequelize } from 'sequelize'; + +const clockPresaleSupplyPackage = { + packingConfigurationId: { + type: Sequelize.STRING, + primaryKey: true, + }, + vbnPackageCode: { + type: Sequelize.INTEGER, + primaryKey: true, + }, + customPackageId: { + type: Sequelize.STRING, + }, +}; + +export default (sequelize) => { + return sequelize.define( + 'supplyLineClockPresaleSupplyPackage', + clockPresaleSupplyPackage, + { + timestamps: false, + freezeTableName: true, + } + ); +}; diff --git a/models/supplyLine/clockPresaleSupplyPackingConfiguration.js b/models/supplyLine/clockPresaleSupplyPackingConfiguration.js new file mode 100644 index 0000000..aea4211 --- /dev/null +++ b/models/supplyLine/clockPresaleSupplyPackingConfiguration.js @@ -0,0 +1,45 @@ +import { Sequelize } from 'sequelize'; + +const supplyLineClockPresaleSupplyPackingConfiguration = { + clockPresaleSupplyPackingConfigurationId: { + type: Sequelize.STRING, + primaryKey: true, + }, + piecesPerPackage: { + type: Sequelize.INTEGER, + }, + bunchesPerPackage: { + type: Sequelize.INTEGER, + }, + packagesPerLayer: { + type: Sequelize.INTEGER, + }, + layersPerLoadCarrier: { + type: Sequelize.INTEGER, + }, + loadCarrier: { + type: Sequelize.STRING, + }, + additionalPricePerPieceCurrency: { + type: Sequelize.STRING, + }, + additionalPricePerPieceValue: { + type: Sequelize.INTEGER, + }, + photoUrl: { + type: Sequelize.STRING, + }, + supplyLineClockPresaleSupplyId: { + type: Sequelize.STRING, + }, +}; + +export default (sequelize) => { + return sequelize.define( + 'supplyLineClockPresaleSupplyPackingConfiguration', + supplyLineClockPresaleSupplyPackingConfiguration, { + timestamps: false, + freezeTableName: true, + } + ); +}; \ No newline at end of file diff --git a/utils.js b/utils.js index 932cf2c..111fcb2 100644 --- a/utils.js +++ b/utils.js @@ -252,7 +252,11 @@ export async function insertSequenceNumber(model, sequenceNumber) { export async function insertTradeItems(tradeItems) { const tx = await models.sequelize.transaction(); try { - const tradeItemsData = tradeItems.map((tradeItem) => ({ + let filteredTradeItems = []; + for (let tradeItem of tradeItems) + if (!tradeItem.isDeleted) filteredTradeItems.push(tradeItem); + + const tradeItemsData = filteredTradeItems.map((tradeItem) => ({ ...tradeItem, organizationId: tradeItem.supplierOrganizationId, lastSync: moment().format('YYYY-MM-DD HH:mm:ss'), @@ -402,7 +406,11 @@ export async function insertTradeItems(tradeItems) { export async function insertClockPresalesSupply(clockPresalesSupply) { const tx = await models.sequelize.transaction(); try { - const clockPresalesSupplyWithDefaults = clockPresalesSupply.map((clockPresaleSupply) => ({ + let filteredclockPresalesSupply = []; + for (let clockPresaleSupply of clockPresalesSupply) + if (!(clockPresaleSupply.status === 'UNAVAILABLE')) filteredclockPresalesSupply.push(clockPresaleSupply); + + const clockPresalesSupplyWithDefaults = filteredclockPresalesSupply.map((clockPresaleSupply) => ({ ...clockPresaleSupply, pricePerPiece_currency: clockPresaleSupply.pricePerPiece.currency, pricePerPiece_value: clockPresaleSupply.pricePerPiece.value, @@ -418,7 +426,6 @@ export async function insertClockPresalesSupply(clockPresalesSupply) { 'pricePerPiece_value', 'deliveryNoteReference', 'numberOfPieces', - 'packingConfigurations', 'tradePeriod_startDateTime', 'tradePeriod_endDateTime', 'organizationId', @@ -431,6 +438,45 @@ export async function insertClockPresalesSupply(clockPresalesSupply) { transaction: tx, }); + let packingConfigurations = []; + + for (const clockPresale of clockPresalesSupplyWithDefaults) { + if (clockPresale.packingConfigurations?.length) { + for (const packingConfiguration of clockPresale.packingConfigurations) { + const uuid = uuidv4(); + + packingConfigurations.push({ + clockPresaleSupplyPackingConfigurationId: uuid, + ...packingConfiguration, + additionalPricePerPieceCurrency: packingConfiguration.additionalPricePerPiece?.currency, + additionalPricePerPieceValue: packingConfiguration.additionalPricePerPiece?.value, + supplyLineClockPresaleSupplyId: clockPresale.supplyLineId, + }); + + models.clockPresaleSupplyPackage.upsert({ + ...packingConfiguration.package, + packingConfigurationId: uuid, + }, { transaction: tx }); + } + } + } + + if (packingConfigurations?.length) + await models.clockPresaleSupplyPackingConfiguration.bulkCreate(packingConfigurations, { + updateOnDuplicate: [ + 'clockPresaleSupplyPackingConfigurationId', + 'piecesPerPackage', + 'bunchesPerPackage', + 'packagesPerLayer', + 'layersPerLoadCarrier', + 'loadCarrier', + 'additionalPricePerPieceCurrency', + 'additionalPricePerPieceValue', + 'photoUrl', + ], + transaction: tx, + }); + await tx.commit(); } catch (err) { await tx.rollback(); @@ -446,7 +492,11 @@ export async function insertClockPresalesSupply(clockPresalesSupply) { export async function insertWarehouses(warehouses) { const tx = await models.sequelize.transaction(); try { - const warehousesWithDefaults = warehouses.map((warehouse) => ({ + let filteredWarehouses = []; + for (let warehouse of warehouses) + if (!warehouse.isDeleted) filteredWarehouses.push(warehouse); + + const warehousesWithDefaults = filteredWarehouses.map((warehouse) => ({ ...warehouse, location_gln: warehouse.location.gln, location_address_addressLine: warehouse.location.address.addressLine, @@ -564,7 +614,11 @@ export async function insertOrganizations(organizations) { export async function insertSupplyLines(supplyLines) { const tx = await models.sequelize.transaction(); try { - const supplyLinesData = supplyLines.map((supplyLine) => ({ + let filteredSupplyLines = []; + for (let supplyLine of supplyLines) + if (!supplyLine.isDeleted) filteredSupplyLines.push(supplyLine); + + const supplyLinesData = filteredSupplyLines.map((supplyLine) => ({ ...supplyLine, organizationId: supplyLine.supplierOrganizationId, deliveryPeriodStartDateTime: supplyLine.deliveryPeriod?.startDateTime ?? null,