#536 updatePrice.js backend unit test

This commit is contained in:
Carlos Jimenez 2018-08-14 12:48:12 +02:00
parent be132e33cb
commit 0c3072fb13
2 changed files with 46 additions and 8 deletions

View File

@ -0,0 +1,31 @@
const app = require(`${servicesDir}/ticket/server/server`);
describe('sale updatePrice()', () => {
it('should throw an error if the price is not a number', async() => {
let error;
let params = {price: `this price is so wrong!`};
await app.models.Sale.updatePrice(params)
.catch(response => {
expect(response).toEqual(new Error('The value should be a number'));
error = response;
});
expect(error).toBeDefined();
});
it(`should throw an error if the price is undefined`, async() => {
let error;
let params = {ticketFk: 1, price: undefined};
await app.models.Sale.updatePrice(params)
.catch(response => {
expect(response).toEqual(new Error('The value should be a number'));
error = response;
});
expect(error).toBeDefined();
});
});

View File

@ -21,17 +21,14 @@ module.exports = Self => {
} }
}); });
Self.updatePrice = async params => { Self.getTicket = async params => {
if (isNaN(params.price))
throw new UserError(`The value should be a number`);
if (!params.price) params.price = 0;
let model = Self.app.models; let model = Self.app.models;
let thisTicketIsEditable = await model.Ticket.isEditable(params.ticketFk); let thisTicketIsEditable = await model.Ticket.isEditable(params.ticketFk);
if (!thisTicketIsEditable) if (!thisTicketIsEditable)
throw new UserError(`The sales of this ticket can't be modified`); throw new UserError(`The sales of this ticket can't be modified`);
let ticket = await model.Ticket.find({ return await model.Ticket.find({
where: { where: {
id: params.ticketFk id: params.ticketFk
}, },
@ -43,11 +40,21 @@ module.exports = Self => {
}], }],
fields: ['id', 'clientFk'] fields: ['id', 'clientFk']
}); });
};
Self.updatePrice = async params => {
if (isNaN(params.price))
throw new UserError(`The value should be a number`);
if (!params.price) params.price = 0;
let model = Self.app.models;
let ticket = await Self.getTicket(params);
let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'}); let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'});
let currentLine = await Self.app.models.Sale.findOne({where: {id: params.id}, fields: 'price'}); let currentLine = await Self.app.models.Sale.findOne({where: {id: params.id}, fields: 'price'});
let componentToUse;
let componentToUse;
if (usesMana) if (usesMana)
componentToUse = 37; componentToUse = 37;
else else
@ -61,8 +68,8 @@ module.exports = Self => {
await Self.rawSql(query, [params.id, componentToUse, value, value]); await Self.rawSql(query, [params.id, componentToUse, value, value]);
await Self.app.models.Sale.update({id: params.id}, {price: params.price}); await Self.app.models.Sale.update({id: params.id}, {price: params.price});
query = `
call vn.manaSpellersRequery(?)`; query = `call vn.manaSpellersRequery(?)`;
await Self.rawSql(query, [ticket[0].client().salesPersonFk]); await Self.rawSql(query, [ticket[0].client().salesPersonFk]);
}; };
}; };