back test for getComponentsSum

This commit is contained in:
Bernat Exposito Domenech 2020-09-08 08:13:54 +02:00
parent 174cea7bc3
commit d79cffb2c3
2 changed files with 41 additions and 22 deletions

View File

@ -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;
};
};

View File

@ -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);
});
});