#536 updatePrice.js backend unit test
This commit is contained in:
parent
be132e33cb
commit
0c3072fb13
|
@ -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();
|
||||
});
|
||||
});
|
|
@ -21,17 +21,14 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.updatePrice = async params => {
|
||||
if (isNaN(params.price))
|
||||
throw new UserError(`The value should be a number`);
|
||||
if (!params.price) params.price = 0;
|
||||
Self.getTicket = async params => {
|
||||
let model = Self.app.models;
|
||||
let thisTicketIsEditable = await model.Ticket.isEditable(params.ticketFk);
|
||||
|
||||
if (!thisTicketIsEditable)
|
||||
throw new UserError(`The sales of this ticket can't be modified`);
|
||||
|
||||
let ticket = await model.Ticket.find({
|
||||
return await model.Ticket.find({
|
||||
where: {
|
||||
id: params.ticketFk
|
||||
},
|
||||
|
@ -43,11 +40,21 @@ module.exports = Self => {
|
|||
}],
|
||||
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 currentLine = await Self.app.models.Sale.findOne({where: {id: params.id}, fields: 'price'});
|
||||
let componentToUse;
|
||||
|
||||
let componentToUse;
|
||||
if (usesMana)
|
||||
componentToUse = 37;
|
||||
else
|
||||
|
@ -61,8 +68,8 @@ module.exports = Self => {
|
|||
await Self.rawSql(query, [params.id, componentToUse, value, value]);
|
||||
|
||||
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]);
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue