From 2455a11c26744ac45f81b869b618af686d868157 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 14 Oct 2022 14:29:21 +0200 Subject: [PATCH 1/9] feat: changeMultipleDiscount uses the same popover than changeDiscount --- modules/ticket/front/sale/index.html | 30 ++---------------------- modules/ticket/front/sale/index.js | 34 ++++++++++++---------------- 2 files changed, 17 insertions(+), 47 deletions(-) diff --git a/modules/ticket/front/sale/index.html b/modules/ticket/front/sale/index.html index 42eb10cb0..112476c30 100644 --- a/modules/ticket/front/sale/index.html +++ b/modules/ticket/front/sale/index.html @@ -300,7 +300,7 @@ ng-model="$ctrl.manaCode"> -
+

New price

{{$ctrl.getNewPrice() | currency: 'EUR': 2}} @@ -321,32 +321,6 @@

- - - - - -
-
- - -
-
- Mana: {{::$ctrl.edit.mana | currency: 'EUR': 0}} -
-
-
-
-
@@ -490,7 +464,7 @@ + ng-click="$ctrl.showEditDiscountPopover($event, sale)"> Update discount Date: Fri, 14 Oct 2022 14:30:37 +0200 Subject: [PATCH 2/9] fix: only can exists 'mana' or 'manaClaim' discount. Never both --- .../back/methods/ticket/updateDiscount.js | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/modules/ticket/back/methods/ticket/updateDiscount.js b/modules/ticket/back/methods/ticket/updateDiscount.js index b1291a45b..99daac512 100644 --- a/modules/ticket/back/methods/ticket/updateDiscount.js +++ b/modules/ticket/back/methods/ticket/updateDiscount.js @@ -115,10 +115,43 @@ module.exports = Self => { for (let sale of sales) { const oldDiscount = sale.discount; const value = ((-sale.price * newDiscount) / 100); - const newComponent = models.SaleComponent.upsert({ - saleFk: sale.id, - value: value, - componentFk: componentId}, myOptions); + + const manaComponent = await models.Component.findOne({ + where: {code: 'mana'} + }, myOptions); + + const manaClaimComponent = await models.Component.findOne({ + where: {code: 'manaClaim'} + }, myOptions); + + const oldComponent = await models.SaleComponent.find({ + where: { + and: [ + {saleFk: sale.id}, + {componentFk: {inq: [manaComponent.id, manaClaimComponent.id]}} + ] + } + }, myOptions); + + let newComponent; + if (oldComponent) { + const filter = { + saleFk: sale.id, + componentFk: oldComponent.componentFk + }; + await models.SaleComponent.destroyAll(filter, myOptions); + + newComponent = models.SaleComponent.create({ + saleFk: sale.id, + value: value, + componentFk: componentId + }, myOptions); + } else { + newComponent = models.SaleComponent.create({ + saleFk: sale.id, + value: value, + componentFk: componentId}, myOptions); + } const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions); From 40075e7e21633569a5c7eb29d5f60fbedbb4a9c1 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 14 Oct 2022 14:38:47 +0200 Subject: [PATCH 3/9] fix: testFront --- modules/ticket/front/sale/index.js | 1 - modules/ticket/front/sale/index.spec.js | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/modules/ticket/front/sale/index.js b/modules/ticket/front/sale/index.js index 714baa126..0d1d28b0e 100644 --- a/modules/ticket/front/sale/index.js +++ b/modules/ticket/front/sale/index.js @@ -273,7 +273,6 @@ class Controller extends Section { const hasChanges = sales.some(sale => { return sale.discount != newDiscount; }); - if (newDiscount != null && hasChanges) this.updateDiscount(sales); diff --git a/modules/ticket/front/sale/index.spec.js b/modules/ticket/front/sale/index.spec.js index 28d874932..a59eb3865 100644 --- a/modules/ticket/front/sale/index.spec.js +++ b/modules/ticket/front/sale/index.spec.js @@ -393,7 +393,7 @@ describe('Ticket', () => { secondSelectedSale.checked = true; const expectedSales = [firstSelectedSale, secondSelectedSale]; - controller.$.editDiscountDialog = {hide: jest.fn()}; + controller.$.editDiscount = {hide: jest.fn()}; controller.edit = { discount: 10, sales: expectedSales @@ -402,7 +402,7 @@ describe('Ticket', () => { controller.changeMultipleDiscount(); expect(controller.updateDiscount).toHaveBeenCalledWith(expectedSales); - expect(controller.$.editDiscountDialog.hide).toHaveBeenCalledWith(); + expect(controller.$.editDiscount.hide).toHaveBeenCalledWith(); }); it('should not call to the updateDiscount() method and then to the editDiscountDialog hide() method', () => { @@ -417,7 +417,7 @@ describe('Ticket', () => { secondSelectedSale.discount = 10; const expectedSales = [firstSelectedSale, secondSelectedSale]; - controller.$.editDiscountDialog = {hide: jest.fn()}; + controller.$.editDiscount = {hide: jest.fn()}; controller.edit = { discount: 10, sales: expectedSales @@ -426,7 +426,7 @@ describe('Ticket', () => { controller.changeMultipleDiscount(); expect(controller.updateDiscount).not.toHaveBeenCalledWith(expectedSales); - expect(controller.$.editDiscountDialog.hide).toHaveBeenCalledWith(); + expect(controller.$.editDiscount.hide).toHaveBeenCalledWith(); }); }); From a11ef73dfbb70a780dc503767cab76c3e7476112 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 08:01:55 +0200 Subject: [PATCH 4/9] refactor: code repetead --- .../back/methods/ticket/updateDiscount.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/modules/ticket/back/methods/ticket/updateDiscount.js b/modules/ticket/back/methods/ticket/updateDiscount.js index 99daac512..d452b03fe 100644 --- a/modules/ticket/back/methods/ticket/updateDiscount.js +++ b/modules/ticket/back/methods/ticket/updateDiscount.js @@ -141,17 +141,9 @@ module.exports = Self => { }; await models.SaleComponent.destroyAll(filter, myOptions); - newComponent = models.SaleComponent.create({ - saleFk: sale.id, - value: value, - componentFk: componentId - }, myOptions); - } else { - newComponent = models.SaleComponent.create({ - saleFk: sale.id, - value: value, - componentFk: componentId}, myOptions); - } + await createSaleComponent(sale.id, value, componentId, myOptions); + } else + await createSaleComponent(sale.id, value, componentId, myOptions); const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions); @@ -198,4 +190,14 @@ module.exports = Self => { throw e; } }; + + async function createSaleComponent(saleId, value, componentId, myOptions) { + const models = Self.app.models; + + newComponent = models.SaleComponent.create({ + saleFk: saleId, + value: value, + componentFk: componentId + }, myOptions); + } }; From 06ecceb478373820cfd0d0c9968ae200462c034c Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 08:02:19 +0200 Subject: [PATCH 5/9] refactor: lines to long --- .../back/methods/ticket/specs/componentUpdate.spec.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js index 2aa2a07c4..a8a4d5048 100644 --- a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js +++ b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js @@ -18,13 +18,17 @@ describe('ticket componentUpdate()', () => { beforeAll(async() => { const deliveryComponenet = await models.Component.findOne({where: {code: 'delivery'}}); deliveryComponentId = deliveryComponenet.id; - componentOfSaleSeven = `SELECT value FROM vn.saleComponent WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; - componentOfSaleEight = `SELECT value FROM vn.saleComponent WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`; - + componentOfSaleSeven = `SELECT value + FROM vn.saleComponent + WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; + componentOfSaleEight = `SELECT value + FROM vn.saleComponent + WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`; [componentValue] = await models.SaleComponent.rawSql(componentOfSaleSeven); firstvalueBeforeChange = componentValue.value; [componentValue] = await models.SaleComponent.rawSql(componentOfSaleEight); + secondvalueBeforeChange = componentValue.value; }); From cdce628038a2bea5aefd5589a4e68d13a900d69c Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 08:11:50 +0200 Subject: [PATCH 6/9] refactor: delete line break --- modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js index a8a4d5048..b3291c25a 100644 --- a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js +++ b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js @@ -28,7 +28,6 @@ describe('ticket componentUpdate()', () => { firstvalueBeforeChange = componentValue.value; [componentValue] = await models.SaleComponent.rawSql(componentOfSaleEight); - secondvalueBeforeChange = componentValue.value; }); From 787d10aac6c08fb7e69643b622505d9d1d3a47d4 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 08:29:09 +0200 Subject: [PATCH 7/9] fix: select one result only --- modules/ticket/back/methods/ticket/updateDiscount.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ticket/back/methods/ticket/updateDiscount.js b/modules/ticket/back/methods/ticket/updateDiscount.js index d452b03fe..bfa3cdbf6 100644 --- a/modules/ticket/back/methods/ticket/updateDiscount.js +++ b/modules/ticket/back/methods/ticket/updateDiscount.js @@ -124,7 +124,7 @@ module.exports = Self => { where: {code: 'manaClaim'} }, myOptions); - const oldComponent = await models.SaleComponent.find({ + const [oldComponent] = await models.SaleComponent.find({ where: { and: [ {saleFk: sale.id}, From f85c2e9700dcada8120b286a54a99fd97c333d8e Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 14:01:21 +0200 Subject: [PATCH 8/9] refactor: delete code repeated --- modules/ticket/back/methods/ticket/updateDiscount.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/ticket/back/methods/ticket/updateDiscount.js b/modules/ticket/back/methods/ticket/updateDiscount.js index bfa3cdbf6..7e7c4e06d 100644 --- a/modules/ticket/back/methods/ticket/updateDiscount.js +++ b/modules/ticket/back/methods/ticket/updateDiscount.js @@ -140,10 +140,9 @@ module.exports = Self => { componentFk: oldComponent.componentFk }; await models.SaleComponent.destroyAll(filter, myOptions); + } - await createSaleComponent(sale.id, value, componentId, myOptions); - } else - await createSaleComponent(sale.id, value, componentId, myOptions); + await createSaleComponent(sale.id, value, componentId, myOptions); const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions); From 925565d3d24037bb53d209823db0b5cef12bc83c Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 26 Oct 2022 08:23:36 +0200 Subject: [PATCH 9/9] =?UTF-8?q?refator:=20borrada=20variable=20global=20y?= =?UTF-8?q?=20a=C3=B1adida=20Promesa=20al=20array?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/ticket/back/methods/ticket/updateDiscount.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/ticket/back/methods/ticket/updateDiscount.js b/modules/ticket/back/methods/ticket/updateDiscount.js index 7e7c4e06d..a60d732b1 100644 --- a/modules/ticket/back/methods/ticket/updateDiscount.js +++ b/modules/ticket/back/methods/ticket/updateDiscount.js @@ -133,20 +133,20 @@ module.exports = Self => { } }, myOptions); - let newComponent; + let deletedComponent; if (oldComponent) { const filter = { saleFk: sale.id, componentFk: oldComponent.componentFk }; - await models.SaleComponent.destroyAll(filter, myOptions); + deletedComponent = await models.SaleComponent.destroyAll(filter, myOptions); } - await createSaleComponent(sale.id, value, componentId, myOptions); + const newComponent = await createSaleComponent(sale.id, value, componentId, myOptions); const updatedSale = sale.updateAttribute('discount', newDiscount, myOptions); - promises.push(newComponent, updatedSale); + promises.push(newComponent, updatedSale, deletedComponent); const change = `${oldDiscount}% ➔ *${newDiscount}%*`; changesMade += `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity}) ${change}`; @@ -193,7 +193,7 @@ module.exports = Self => { async function createSaleComponent(saleId, value, componentId, myOptions) { const models = Self.app.models; - newComponent = models.SaleComponent.create({ + return models.SaleComponent.create({ saleFk: saleId, value: value, componentFk: componentId