#1488 #1489 ticket sale updatePrice and backTest
gitea/salix/dev This commit looks good Details

This commit is contained in:
Bernat 2019-06-03 08:01:24 +02:00
parent 68f67f12d7
commit 62fe9d760c
7 changed files with 133 additions and 166 deletions

View File

@ -442,7 +442,7 @@ INSERT INTO `vn`.`ticket`(`id`, `agencyModeFk`,`warehouseFk`,`routeFk`, `shipped
(5, 3, 3, 3, DATE_ADD(CURDATE(), INTERVAL -3 DAY), DATE_ADD(CURDATE(), INTERVAL -3 DAY), 103, 'address 23', 123, 'T3333333', 0, DATE_ADD(CURDATE(), INTERVAL -3 DAY)),
(6, 3, 3, 4, DATE_ADD(CURDATE(), INTERVAL -2 DAY), DATE_ADD(CURDATE(), INTERVAL -2 DAY), 103, 'address 23', 123, 'T4444444', 0, DATE_ADD(CURDATE(), INTERVAL -2 DAY)),
(7, 4, 4, 4, DATE_ADD(CURDATE(), INTERVAL -1 DAY), DATE_ADD(CURDATE(), INTERVAL -1 DAY), 104, 'address 24', 124, 'T4444444', 0, DATE_ADD(CURDATE(), INTERVAL -1 DAY)),
(8, 1, 1, 4, DATE_ADD(CURDATE(), INTERVAL +1 MONTH), DATE_ADD(CURDATE(), INTERVAL +1 MONTH), 104, 'address 24', 124, NULL, 0, DATE_ADD(CURDATE(), INTERVAL +1 MONTH)),
(8, 1, 1, 4, CURDATE(), CURDATE(), 104, 'address 24', 124, NULL, 0, CURDATE()),
(9, 5, 5, 4, DATE_ADD(CURDATE(), INTERVAL -2 MONTH), DATE_ADD(CURDATE(), INTERVAL -2 MONTH), 105, 'address 25', 125, 'A1111111', 0, DATE_ADD(CURDATE(), INTERVAL -2 MONTH)),
(10, 6, 5, 5, DATE_ADD(CURDATE(), INTERVAL -3 MONTH), DATE_ADD(CURDATE(), INTERVAL -3 MONTH), 105, 'address 25', 125, 'A1111111', 0, DATE_ADD(CURDATE(), INTERVAL -3 MONTH)),
(11, 7, 1, 1, CURDATE() , CURDATE() , 101, 'address 21', 121, NULL, 0, CURDATE()),

View File

@ -1,121 +1,73 @@
const app = require('vn-loopback/server/server');
// #1489 updatePrice backTest
xdescribe('sale updatePrice()', () => {
it('should set price to 0 if the params price is undefined', async() => {
let params = {
price: undefined,
id: 13,
ticketFk: 8
};
await app.models.Sale.updatePrice(params);
let saleUpdated = await app.models.Sale.findById(params.id);
describe('sale updatePrice()', () => {
let originalSale;
let originalSalesPersonMana;
let createdSaleComponent;
let saleId = 13;
expect(saleUpdated.price).toEqual(0);
afterAll(async done => {
originalSale.save();
createdSaleComponent.destroy();
originalSalesPersonMana.save();
done();
});
it('should set price as a decimal number', async() => {
let params = {
price: 5.5,
id: 13,
ticketFk: 8
};
await app.models.Sale.updatePrice(params);
let saleUpdated = await app.models.Sale.findById(params.id);
beforeAll(async done => {
originalSale = await app.models.Sale.findById(saleId);
originalSalesPersonMana = await app.models.WorkerMana.findById(18);
expect(saleUpdated.price).toEqual(5.5);
done();
});
it('should now set price as a decimal number in a string', async() => {
let params = {
price: '5.5',
id: 13,
ticketFk: 8
};
await app.models.Sale.updatePrice(params);
let saleUpdated = await app.models.Sale.findById(params.id);
it('should throw an error if the ticket is not editable', async() => {
let immutableSaleId = 1;
let price = 5;
expect(saleUpdated.price).toEqual(5.5);
await app.models.Sale.updatePrice(immutableSaleId, price)
.catch(response => {
expect(response).toEqual(new Error(`The sales of this ticket can't be modified`));
error = response;
});
expect(error).toBeDefined();
});
it('should return 0 if the price is an empty string', async() => {
let params = {
price: '',
id: 13,
ticketFk: 8
};
await app.models.Sale.updatePrice(params);
let saleUpdated = await app.models.Sale.findById(params.id);
let price = '';
await app.models.Sale.updatePrice(saleId, price);
let saleUpdated = await app.models.Sale.findById(saleId);
expect(saleUpdated.price).toEqual(0);
});
it('should now set price as a decimal number in a string', async() => {
let price = '8';
it('should este crea mana', async() => {
let params = {
price: 5.5,
id: 13,
ticketFk: 8
};
await app.models.Sale.updatePrice(params);
let saleUpdated = await app.models.Sale.findById(params.id);
await app.models.Sale.updatePrice(saleId, price);
let saleUpdated = await app.models.Sale.findById(saleId);
expect(saleUpdated.price).toEqual(5.5);
expect(saleUpdated.price).toEqual(8);
});
it('should este no crea el mana', async() => {
let params = {
price: 5.5,
id: 13,
ticketFk: 8
};
let todosLosComponentes = await app.models.SaleComponent.find({where: {saleFk: 13}});
it('should set price as a decimal number and check the sale has the mana component', async() => {
let price = 5.5;
let manaComponentId = 37;
expect(todosLosComponentes[0].value).toEqual('');
expect(todosLosComponentes[1].value).toEqual('');
expect(todosLosComponentes[2].value).toEqual('');
expect(todosLosComponentes[3].value).toEqual('');
expect(todosLosComponentes[4].value).toEqual('');
await app.models.Sale.updatePrice(params);
todosLosComponentes = await app.models.SaleComponent.find({where: {saleFk: 13}});
let saleUpdated = await app.models.Sale.findById(params.id);
await app.models.Sale.updatePrice(saleId, price);
let saleUpdated = await app.models.Sale.findById(saleId);
createdSaleComponent = await app.models.SaleComponent.findOne({where: {saleFk: saleId, componentFk: manaComponentId}});
expect(saleUpdated.price).toEqual(5.5);
expect(todosLosComponentes[0].value).toEqual('');
expect(todosLosComponentes[1].value).toEqual('');
expect(todosLosComponentes[2].value).toEqual('');
expect(todosLosComponentes[3].value).toEqual('');
expect(todosLosComponentes[4].value).toEqual('');
expect(createdSaleComponent.value).toEqual(4.200);
});
// 2 let componentToUse;
// if (usesMana)
// componentToUse = 37;
// 3else
// componentToUse = 34;
// 4 if (saleComponent)
// saleComponent.updateAttributes({value: saleComponent.value + value});
// 5 else {
// await Self.app.models.SaleComponent.create({
// saleFk: params.id,
// componentFk: componentToUse,
// value: value
// });
// }
it('should check that the mana of salesPerson changed', async() => {
let updatedSalesPersonMana = await app.models.WorkerMana.findById(18);
// 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();
// });
expect(updatedSalesPersonMana.amount).not.toEqual(originalSalesPersonMana.amount);
});
});

View File

@ -2,84 +2,94 @@ let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('updatePrice', {
description: 'Changes the discount of a sale',
description: 'Changes the price of a sale',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
accepts: [
{
arg: 'id',
description: 'The sale id',
type: 'number',
required: true,
description: 'sale ID, newPrice, ticketFk',
http: {source: 'body'}
}],
http: {source: 'path'}
}, {
arg: 'newPrice',
description: 'The new price',
type: 'number',
required: true
}
],
returns: {
type: 'string',
root: true
},
http: {
path: `/updatePrice`,
path: `/:id/updatePrice`,
verb: 'post'
}
});
Self.updatePrice = async params => {
if (!params.price) params.price = 0;
Self.updatePrice = async(id, newPrice) => {
let $ = Self.app.models;
let transaction = await Self.beginTransaction({});
let options = {transaction};
let model = Self.app.models;
let manaDiscount;
let buyerDiscount = await model.ComponentRate.findOne({where: {code: 'buyerDiscount'}});
let ticket = await 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}});
let componentId = buyerDiscount.id;
if (usesMana) {
manaDiscount = await model.ComponentRate.findOne({where: {code: 'mana'}});
componentId = manaDiscount.id;
}
let value = (params.price - currentLine.price);
let saleComponent = await Self.app.models.SaleComponent.findOne({
where: {
componentFk: componentId,
saleFk: params.id
}
});
if (saleComponent) {
saleComponent.updateAttributes({value: saleComponent.value + value}).catch(() => {
throw new UserError(`Enter a valid number`);
});
} else {
await Self.app.models.SaleComponent.create({
saleFk: params.id,
componentFk: componentId,
value: value
});
}
await currentLine.updateAttributes({price: params.price});
query = `call vn.manaSpellersRequery(?)`;
await Self.rawSql(query, [ticket[0].client().salesPersonFk]);
};
async function getTicket(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`);
return await model.Ticket.find({
where: {
id: params.ticketFk
},
include: [{
try {
let filter = {
fields: ['id', 'ticketFk', 'price'],
include: {
relation: 'ticket',
scope: {
include: {
relation: 'client',
scope: {
fields: ['salesPersonFk']
}
}],
},
fields: ['id', 'clientFk']
});
}
}
};
let sale = await $.Sale.findById(id, filter, options);
let isEditable = await $.Ticket.isEditable(sale.ticketFk);
if (!isEditable)
throw new UserError(`The sales of this ticket can't be modified`);
let salesPerson = sale.ticket().client().salesPersonFk;
let usesMana = await $.WorkerMana.findOne({where: {workerFk: salesPerson}, fields: 'amount'}, options);
let componentCode = usesMana ? 'mana' : 'buyerDiscount';
let discount = await $.ComponentRate.findOne({where: {code: componentCode}}, options);
let componentId = discount.id;
let componentValue = newPrice - sale.price;
let where = {
componentFk: componentId,
saleFk: id
};
let saleComponent = await $.SaleComponent.findOne({where}, options);
if (saleComponent) {
await $.SaleComponent.updateAll(where, {
value: saleComponent.value + componentValue
}, options);
} else {
await $.SaleComponent.create({
saleFk: id,
componentFk: componentId,
value: componentValue
}, options);
}
await sale.updateAttributes({price: newPrice}, options);
query = `call vn.manaSpellersRequery(?)`;
await Self.rawSql(query, [salesPerson], options);
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}
};
};

View File

@ -4,7 +4,7 @@ describe('ticket getSalesPersonMana()', () => {
it('should get the mana of a salesperson of a given ticket', async() => {
let mana = await app.models.Ticket.getSalesPersonMana(1);
expect(mana).toEqual(221);
expect(mana).toEqual(222);
});
it('should return 0 if the given ticket does not exists', async() => {

View File

@ -11,7 +11,12 @@
"type": "Number"
},
"saleFk": {
"id": true
"type": "Number",
"id": 2
},
"componentFk": {
"type": "Number",
"id": 1
}
},
"relations": {

View File

@ -260,7 +260,7 @@ class Controller {
updatePrice() {
if (this.editedPrice != this.sale.price) {
this.$http.post(`/api/Sales/updatePrice`, {id: this.edit.id, price: this.editedPrice, ticketFk: this.ticket.id}).then(() => {
this.$http.post(`/api/Sales/${this.edit.id}/updatePrice`, {newPrice: this.editedPrice}).then(() => {
this.sale.price = this.edit.price;
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.$scope.model.refresh();

View File

@ -4,7 +4,7 @@ describe('workerMana getCurrentWorkerMana()', () => {
it('should get the mana of the logged worker', async() => {
let mana = await app.models.WorkerMana.getCurrentWorkerMana({req: {accessToken: {userId: 18}}});
expect(mana).toEqual(221);
expect(mana).toEqual(222);
});
it('should return 0 if the user doesnt uses mana', async() => {