diff --git a/db/versions/11088-bronzeAspidistra/00-firstScript.sql b/db/versions/11088-bronzeAspidistra/00-firstScript.sql index 2cc4e715e..98203c9d1 100644 --- a/db/versions/11088-bronzeAspidistra/00-firstScript.sql +++ b/db/versions/11088-bronzeAspidistra/00-firstScript.sql @@ -1,4 +1,3 @@ --- Place your SQL code here DROP TABLE IF EXISTS vn.wagonTypeTray; CREATE TABLE vn.wagonTypeTray ( @@ -20,7 +19,3 @@ ALTER TABLE vn.wagonConfig ADD CONSTRAINT wagonConfig_wagonTypeColor_FK FOREIGN ALTER TABLE vn.wagonTypeTray DROP FOREIGN KEY wagonTypeTray_wagonType_FK; ALTER TABLE vn.wagonTypeTray ADD CONSTRAINT wagonTypeTray_wagonType_FK FOREIGN KEY (wagonTypeFk) REFERENCES vn.wagonType(id) ON DELETE CASCADE ON UPDATE RESTRICT; - - - --- insertar datos por defecto \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index e2be5d013..47601ae41 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -367,5 +367,9 @@ "It has been invoiced but the PDF of refund not be generated": "Se ha facturado pero no se ha podido generar el PDF del abono", "Payment method is required": "El método de pago es obligatorio", "Cannot send mail": "Não é possível enviar o email", - "CONSTRAINT `supplierAccountTooShort` failed for `vn`.`supplier`": "La cuenta debe tener exactamente 10 dígitos" -} + "CONSTRAINT `supplierAccountTooShort` failed for `vn`.`supplier`": "La cuenta debe tener exactamente 10 dígitos", + "There is already a tray with the same height": "Ya existe una bandeja con la misma altura", + "You must define wagon and height": "Debes definir un tipo de vagón y la altura", + "The maximum height of the wagon is": "La altura máxima es %d", + "The height must be greater than": "The height must be greater than %d" +} \ No newline at end of file diff --git a/modules/wagon/back/models/wagon-type-tray.js b/modules/wagon/back/models/wagon-type-tray.js index e32938743..5bcb1d3c7 100644 --- a/modules/wagon/back/models/wagon-type-tray.js +++ b/modules/wagon/back/models/wagon-type-tray.js @@ -1,16 +1,28 @@ -// module.exports = Self => { -// Self.observe('before save', async ctx => { -// if (ctx.isNewInstance) { -// const models = Self.app.models; -// const config = await models.WagonConfig.findOne(); +const UserError = require('vn-loopback/util/user-error'); -// await models.WagonTypeTray.create({ -// wagonTypeFk: config.wagonTypeFk, -// height: config.height, -// wagonTypeColorFk: config.wagonTypeColorFk -// }, ctx.options); -// } -// if (ctx.instance < config.minHeightBetweenTrays) -// throw new Error('Height must be greater than ' + config.minHeightBetweenTrays); -// }); -// }; +module.exports = Self => { + Self.observe('before save', async ctx => { + if (ctx.isNewInstance) { + const models = Self.app.models; + const {wagonTypeFk, height} = ctx.instance; + const trays = await models.WagonTypeTray.find({where: {wagonTypeFk}}); + + const config = await models.WagonConfig.findOne(); + const tray = await models.WagonTypeTray.find({where: {wagonTypeFk, height}}); + + if (!trays.length) return; + + if (tray.length) + throw new UserError('There is already a tray with the same height'); + + if (!wagonTypeFk && !height) + throw new UserError('You must define wagon and height'); + + if (height < config.minHeightBetweenTrays) + throw new UserError('The height must be greater than', 'HEIGHT_GREATER_THAN', config.minHeightBetweenTrays); + + if (height > config.maxWagonHeight) + throw new UserError('The maximum height of the wagon is', 'MAX_WAGON_HEIGHT', config.maxWagonHeight); + } + }); +};