From d79cffb2c373b6d3abfd3f1233afcc448a2a61c9 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 8 Sep 2020 08:13:54 +0200 Subject: [PATCH] back test for getComponentsSum --- .../back/methods/ticket/getComponentsSum.js | 45 ++++++++++--------- .../ticket/specs/getComponentsSum.spec.js | 18 ++++++++ 2 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 modules/ticket/back/methods/ticket/specs/getComponentsSum.spec.js diff --git a/modules/ticket/back/methods/ticket/getComponentsSum.js b/modules/ticket/back/methods/ticket/getComponentsSum.js index 7646123ab..febf4eab1 100644 --- a/modules/ticket/back/methods/ticket/getComponentsSum.js +++ b/modules/ticket/back/methods/ticket/getComponentsSum.js @@ -20,33 +20,34 @@ module.exports = Self => { }); Self.getComponentsSum = async id => { const models = Self.app.models; - let sales = await models.Sale.find({where: {ticketFk: id}}); - let components = await models.Component.find(); let salesComponents = []; let componentsSum = []; - - for (let sale of sales) { - let myComponents = await models.SaleComponent.find({where: {saleFk: sale.id}}); - salesComponents = salesComponents.concat(myComponents); - } - - salesComponents.reduce((acumulator, currentValue) => { - if (!acumulator[currentValue.componentFk]) { - let defaultValue = 0; - let tarjetComponent = components.find(component => component.id === currentValue.componentFk); - - acumulator[currentValue.componentFk] = { - componentFk: currentValue.componentFk, - value: defaultValue, - name: tarjetComponent.name - }; - componentsSum.push(acumulator[currentValue.componentFk]); + let sales = await models.Sale.find({where: {ticketFk: id}}); + let components = await models.Component.find(); + if (sales.length > 0) { + for (let sale of sales) { + let myComponents = await models.SaleComponent.find({where: {saleFk: sale.id}}); + salesComponents = salesComponents.concat(myComponents); } - acumulator[currentValue.componentFk].value += currentValue.value; + salesComponents.reduce((acumulator, currentValue) => { + if (!acumulator[currentValue.componentFk]) { + let defaultValue = 0; + let tarjetComponent = components.find(component => component.id === currentValue.componentFk); - return acumulator; - }); + acumulator[currentValue.componentFk] = { + componentFk: currentValue.componentFk, + value: defaultValue, + name: tarjetComponent.name + }; + componentsSum.push(acumulator[currentValue.componentFk]); + } + + acumulator[currentValue.componentFk].value += currentValue.value; + + return acumulator; + }); + } return componentsSum; }; }; diff --git a/modules/ticket/back/methods/ticket/specs/getComponentsSum.spec.js b/modules/ticket/back/methods/ticket/specs/getComponentsSum.spec.js new file mode 100644 index 000000000..876e9f615 --- /dev/null +++ b/modules/ticket/back/methods/ticket/specs/getComponentsSum.spec.js @@ -0,0 +1,18 @@ +const app = require('vn-loopback/server/server'); + +fdescribe('ticket getComponentsSum()', () => { + it('should get the list of component of their sales of a given ticket', async() => { + let ticketId = 7; + let mana = await app.models.Ticket.getComponentsSum(ticketId); + + expect(mana.length).toBeGreaterThan(0); + expect(mana[0].componentFk).toBe(22); + }); + + it('should return 0 if the given ticket does not have sales', async() => { + let ticketWithoutSales = 21; + let mana = await app.models.Ticket.getComponentsSum(ticketWithoutSales); + + expect(mana.length).toEqual(0); + }); +});