From 62fe9d760c6d47c730e186b14698f3fc782d5954 Mon Sep 17 00:00:00 2001 From: Bernat Date: Mon, 3 Jun 2019 08:01:24 +0200 Subject: [PATCH] #1488 #1489 ticket sale updatePrice and backTest --- db/dump/fixtures.sql | 2 +- .../methods/sale/specs/updatePrice.spec.js | 138 ++++++----------- .../ticket/back/methods/sale/updatePrice.js | 146 ++++++++++-------- .../ticket/specs/getSalespersonMana.spec.js | 2 +- .../ticket/back/models/sale-component.json | 7 +- modules/ticket/front/sale/index.js | 2 +- .../specs/getCurrentWorkerMana.spec.js | 2 +- 7 files changed, 133 insertions(+), 166 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index b1cc35f63..38318f578 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -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()), diff --git a/modules/ticket/back/methods/sale/specs/updatePrice.spec.js b/modules/ticket/back/methods/sale/specs/updatePrice.spec.js index f86cb17eb..7c31ab46b 100644 --- a/modules/ticket/back/methods/sale/specs/updatePrice.spec.js +++ b/modules/ticket/back/methods/sale/specs/updatePrice.spec.js @@ -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); + }); }); diff --git a/modules/ticket/back/methods/sale/updatePrice.js b/modules/ticket/back/methods/sale/updatePrice.js index 246a9a4e5..dd6d64c59 100644 --- a/modules/ticket/back/methods/sale/updatePrice.js +++ b/modules/ticket/back/methods/sale/updatePrice.js @@ -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', - required: true, - description: 'sale ID, newPrice, ticketFk', - http: {source: 'body'} - }], + accepts: [ + { + arg: 'id', + description: 'The sale id', + type: 'number', + required: true, + 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: [{ - relation: 'client', - scope: { - fields: ['salesPersonFk'] + try { + let filter = { + fields: ['id', 'ticketFk', 'price'], + include: { + relation: 'ticket', + scope: { + include: { + relation: 'client', + scope: { + fields: ['salesPersonFk'] + } + }, + fields: ['id', 'clientFk'] + } } - }], - 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; + } + }; }; diff --git a/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js b/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js index 3d7e084be..b08220d15 100644 --- a/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js +++ b/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js @@ -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() => { diff --git a/modules/ticket/back/models/sale-component.json b/modules/ticket/back/models/sale-component.json index e2649e09b..49edab2ae 100644 --- a/modules/ticket/back/models/sale-component.json +++ b/modules/ticket/back/models/sale-component.json @@ -11,7 +11,12 @@ "type": "Number" }, "saleFk": { - "id": true + "type": "Number", + "id": 2 + }, + "componentFk": { + "type": "Number", + "id": 1 } }, "relations": { diff --git a/modules/ticket/front/sale/index.js b/modules/ticket/front/sale/index.js index 535241796..0293f12d0 100644 --- a/modules/ticket/front/sale/index.js +++ b/modules/ticket/front/sale/index.js @@ -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(); diff --git a/modules/worker/back/methods/worker-mana/specs/getCurrentWorkerMana.spec.js b/modules/worker/back/methods/worker-mana/specs/getCurrentWorkerMana.spec.js index 1d48eef58..fe0b119f3 100644 --- a/modules/worker/back/methods/worker-mana/specs/getCurrentWorkerMana.spec.js +++ b/modules/worker/back/methods/worker-mana/specs/getCurrentWorkerMana.spec.js @@ -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() => {