From ffc8c54899ac87c1235f82128c1bcc0c1d07c623 Mon Sep 17 00:00:00 2001 From: gerard Date: Thu, 28 Jun 2018 15:25:09 +0200 Subject: [PATCH] Tarea #355 ticket.lineas Modificar cantidad --- .../common/methods/sale/updateQuantity.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 services/loopback/common/methods/sale/updateQuantity.js diff --git a/services/loopback/common/methods/sale/updateQuantity.js b/services/loopback/common/methods/sale/updateQuantity.js new file mode 100644 index 000000000..eac9ac309 --- /dev/null +++ b/services/loopback/common/methods/sale/updateQuantity.js @@ -0,0 +1,34 @@ +module.exports = Self => { + Self.remoteMethod('updateQuantity', { + description: 'Changes the quantity of a sale', + accessType: '', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'sale ID', + http: {source: 'path'} + }, { + arg: 'quantity', + type: 'number', + required: true, + description: 'newQuantity' + }], + returns: { + type: 'string', + root: true + }, + http: { + path: `/:id/updateQuantity`, + verb: 'post' + } + }); + + Self.updateQuantity = async(id, quantity) => { + let currentLine = await Self.app.models.Sale.findOne({where: {id: id}, fields: ['quantity']}); + if (quantity > currentLine.quantity) + throw new Error('The new quantity should be smaller than the old one'); + + return await Self.app.models.Sale.update({id: id}, {quantity: quantity}); + }; +};