From cf559b2ba58b2e5cf31e4292d5b2b6dc91c22639 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 8 Jun 2023 09:19:57 +0200 Subject: [PATCH] =?UTF-8?q?refs=20#5774=20feat:=20solo=20se=20pueden=20a?= =?UTF-8?q?=C3=B1adir=20cantidades=20negativas=20si=20es=20un=20ticket=20d?= =?UTF-8?q?e=20abono?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/dump/fixtures.sql | 3 +- loopback/locale/es.json | 5 +- .../methods/sale/specs/updateQuantity.spec.js | 49 +++++++++++++++++++ .../back/methods/sale/updateQuantity.js | 7 +++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 3476bfe48..99ed993f0 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2833,7 +2833,8 @@ INSERT INTO `vn`.`workerConfig` (`id`, `businessUpdated`, `roleFk`, `payMethodFk INSERT INTO `vn`.`ticketRefund`(`refundTicketFk`, `originalTicketFk`) VALUES - (1, 12); + (1, 12), + (8, 10); INSERT INTO `vn`.`deviceProductionModels` (`code`) VALUES diff --git a/loopback/locale/es.json b/loopback/locale/es.json index d88a4ebc9..3b1c331c7 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -84,7 +84,7 @@ "The current ticket can't be modified": "El ticket actual no puede ser modificado", "The current claim can't be modified": "La reclamación actual no puede ser modificada", "The sales of this ticket can't be modified": "Las lineas de este ticket no pueden ser modificadas", - "The sales do not exists": "La(s) línea(s) seleccionada(s) no existe(n)", + "The sales do not exists": "La(s) línea(s) seleccionada(s) no existe(n)", "Please select at least one sale": "Por favor selecciona al menos una linea", "All sales must belong to the same ticket": "Todas las lineas deben pertenecer al mismo ticket", "NO_ZONE_FOR_THIS_PARAMETERS": "Para este día no hay ninguna zona configurada", @@ -293,5 +293,6 @@ "comercialName": "Comercial", "Invalid NIF for VIES": "Invalid NIF for VIES", "Ticket does not exist": "Este ticket no existe", - "Ticket is already signed": "Este ticket ya ha sido firmado" + "Ticket is already signed": "Este ticket ya ha sido firmado", + "You can only add negative amounts in refund tickets": "Solo se puede añadir cantidades negativas en tickets abono" } diff --git a/modules/ticket/back/methods/sale/specs/updateQuantity.spec.js b/modules/ticket/back/methods/sale/specs/updateQuantity.spec.js index 80adb0bd1..8064ea30b 100644 --- a/modules/ticket/back/methods/sale/specs/updateQuantity.spec.js +++ b/modules/ticket/back/methods/sale/specs/updateQuantity.spec.js @@ -110,4 +110,53 @@ describe('sale updateQuantity()', () => { throw e; } }); + + it('should throw an error if the quantity is negative and it is not a refund ticket', async() => { + const ctx = { + req: { + accessToken: {userId: 1}, + headers: {origin: 'localhost:5000'}, + __: () => {} + } + }; + const saleId = 17; + const newQuantity = -10; + + const tx = await models.Sale.beginTransaction({}); + + let error; + try { + const options = {transaction: tx}; + + await models.Sale.updateQuantity(ctx, saleId, newQuantity, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } + + expect(error).toEqual(new Error('You can only add negative amounts in refund tickets')); + }); + + it('should update a negative quantity when is a ticket refund', async() => { + const tx = await models.Sale.beginTransaction({}); + const saleId = 13; + const newQuantity = -10; + + try { + const options = {transaction: tx}; + + await models.Sale.updateQuantity(ctx, saleId, newQuantity, options); + + const modifiedLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options); + + expect(modifiedLine.quantity).toEqual(newQuantity); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); }); diff --git a/modules/ticket/back/methods/sale/updateQuantity.js b/modules/ticket/back/methods/sale/updateQuantity.js index 421c74702..edbc34e42 100644 --- a/modules/ticket/back/methods/sale/updateQuantity.js +++ b/modules/ticket/back/methods/sale/updateQuantity.js @@ -68,6 +68,13 @@ module.exports = Self => { if (newQuantity > sale.quantity && !isRoleAdvanced) throw new UserError('The new quantity should be smaller than the old one'); + const ticketRefund = await models.TicketRefund.findOne({ + where: {refundTicketFk: sale.ticketFk}, + fields: ['id']} + , myOptions); + if (newQuantity < 0 && !ticketRefund) + throw new UserError('You can only add negative amounts in refund tickets'); + const oldQuantity = sale.quantity; const result = await sale.updateAttributes({quantity: newQuantity}, myOptions);