diff --git a/floriday.js b/floriday.js index d83a58c..ec2446e 100644 --- a/floriday.js +++ b/floriday.js @@ -59,7 +59,7 @@ class Floriday { await utils.syncSupplyLines(); // Deshabilitado porque no hay de momento - // await utils.syncClockPresalesSupply(); + // await utils.syncClockPresaleSupply(); } catch (err) { if (err.name === 'SequelizeConnectionRefusedError') throw err; diff --git a/models/sequelize.js b/models/sequelize.js index 6768ff8..aaffb1e 100644 --- a/models/sequelize.js +++ b/models/sequelize.js @@ -2,6 +2,7 @@ import { Sequelize } from 'sequelize'; import dotenv from 'dotenv'; import chalk from 'chalk'; import ora from 'ora'; +import fs from 'fs'; import { criticalError } from '../utils.js'; dotenv.config(); @@ -78,8 +79,9 @@ let models = { package: packageModel(sequelize), }; -// Foreign Keys try { + // Foreign Keys + /* TODO: Aplicar dependiendo de cada caso onDelete: 'CASCADE', onUpdate: 'CASCADE', @@ -156,6 +158,10 @@ try { foreignKey: 'organizationId', targetKey: 'organizationId', }); + + // Views + sequelize.query(fs.readFileSync('views//supplyOffer.sql', 'utf-8')); + } catch (err) { criticalError(err); } diff --git a/utils.js b/utils.js index 1f2bbc5..44e446d 100644 --- a/utils.js +++ b/utils.js @@ -316,7 +316,7 @@ export async function syncSupplyLines() { /** * Sync the lock presales supply for supply lines of today. */ -export async function syncClockPresalesSupply() { +export async function syncClockPresaleSupply() { const spinner = ora(`Syncing clock presales supply...`).start(); const supplyLines = await models.supplyLine.findAll({ attributes: ['supplyLineId'], diff --git a/views/supplyOffer.sql b/views/supplyOffer.sql new file mode 100644 index 0000000..f870b0b --- /dev/null +++ b/views/supplyOffer.sql @@ -0,0 +1,24 @@ +CREATE OR REPLACE VIEW supplyOffer AS +SELECT sl.supplyLineId, + ti.name tradeItemName, + o.commercialName organizationName, + sl.type, + sl.salesUnit, + slvp.pricePerPiece, + sl.numberOfPieces, + sl.deliveryPeriodStartDateTime , + sl.deliveryPeriodEndDateTime, + sl.orderPeriodStartDateTime, + sl.orderPeriodEndDateTime, + tip.url, + sl.lastSync +FROM supplyLine sl + JOIN supplyLineVolumePrice slvp ON slvp.supplyLineId = sl.supplyLineId + JOIN organization o ON o.organizationId = sl.organizationId + JOIN tradeItem ti ON ti.tradeItemId = sl.tradeItemId + JOIN tradeItemPhoto tip ON tip.tradeItemId = ti.tradeItemId + AND tip.primary +WHERE sl.status = 'AVAILABLE' + AND NOT sl.isDeleted + AND NOW() BETWEEN sl.orderPeriodStartDateTime AND sl.orderPeriodEndDateTime +GROUP BY sl.supplyLineId \ No newline at end of file