From d34a8629f74e54760cdeae95ba1bd11139547b44 Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 6 May 2022 13:09:37 +0200 Subject: [PATCH] fix(setSaleQuantity): save originalQuantity --- back/methods/collection/setSaleQuantity.js | 12 +++++++----- .../methods/collection/spec/setSaleQuantity.spec.js | 11 ++--------- modules/ticket/back/models/sale.json | 13 ++++++++----- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/back/methods/collection/setSaleQuantity.js b/back/methods/collection/setSaleQuantity.js index 82451b8be..644c44a60 100644 --- a/back/methods/collection/setSaleQuantity.js +++ b/back/methods/collection/setSaleQuantity.js @@ -1,5 +1,5 @@ module.exports = Self => { - Self.remoteMethodCtx('setSaleQuantity', { + Self.remoteMethod('setSaleQuantity', { description: 'Update sale quantity', accessType: 'WRITE', accepts: [{ @@ -24,11 +24,13 @@ module.exports = Self => { } }); - Self.setSaleQuantity = async ctx => { - const args = ctx.args; + Self.setSaleQuantity = async(saleId, quantity) => { const models = Self.app.models; - const sale = await models.Sale.findById(args.saleId,); - return await sale.updateAttribute('quantity', args.quantity); + const sale = await models.Sale.findById(saleId); + return await sale.updateAttributes({ + originalQuantity: sale.quantity, + quantity: quantity + }); }; }; diff --git a/back/methods/collection/spec/setSaleQuantity.spec.js b/back/methods/collection/spec/setSaleQuantity.spec.js index 4e3c8c4aa..5d06a4383 100644 --- a/back/methods/collection/spec/setSaleQuantity.spec.js +++ b/back/methods/collection/spec/setSaleQuantity.spec.js @@ -5,19 +5,12 @@ describe('setSaleQuantity()', () => { const saleId = 30; const newQuantity = 10; - const ctx = { - args: { - saleId: saleId, - quantity: newQuantity - } - }; - const originalSale = await models.Sale.findById(saleId); - await models.Collection.setSaleQuantity(ctx); + await models.Collection.setSaleQuantity(saleId, newQuantity); const updateSale = await models.Sale.findById(saleId); - expect(updateSale.quantity).toBeLessThan(originalSale.quantity); + expect(updateSale.originalQuantity).toEqual(originalSale.quantity); expect(updateSale.quantity).toEqual(newQuantity); }); }); diff --git a/modules/ticket/back/models/sale.json b/modules/ticket/back/models/sale.json index 767a3e59e..14a6bc2cf 100644 --- a/modules/ticket/back/models/sale.json +++ b/modules/ticket/back/models/sale.json @@ -14,7 +14,7 @@ "properties": { "id": { "id": true, - "type": "Number", + "type": "number", "description": "Identifier" }, "concept": { @@ -22,22 +22,25 @@ "required": true }, "quantity": { - "type": "Number" + "type": "number" }, "price": { - "type": "Number" + "type": "number" }, "discount": { - "type": "Number" + "type": "number" }, "reserved": { "type": "boolean" }, "isPicked": { - "type": "Number" + "type": "number" }, "created": { "type": "date" + }, + "originalQuantity":{ + "type": "number" } }, "relations": {