diff --git a/modules/client/back/methods/client/specs/updatePortfolio.spec.js b/modules/client/back/methods/client/specs/updatePortfolio.spec.js index f56555c08..bf681eb2e 100644 --- a/modules/client/back/methods/client/specs/updatePortfolio.spec.js +++ b/modules/client/back/methods/client/specs/updatePortfolio.spec.js @@ -1,21 +1,39 @@ const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); describe('Client updatePortfolio', () => { - const clientId = 1108; + const activeCtx = { + accessToken: {userId: 9}, + http: { + req: { + headers: {origin: 'http://localhost'}, + [`__`]: value => { + return value; + } + } + } + }; + + beforeAll(() => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + }); + it('should update the portfolioWeight when the salesPerson of a client changes', async() => { + const clientId = 1108; const salesPersonId = 18; const tx = await models.Client.beginTransaction({}); try { const options = {transaction: tx}; - const expectedResult = 841.63; - const clientQuery = `UPDATE vn.client SET salesPersonFk = ${salesPersonId} WHERE id = ${clientId}; `; - await models.Client.rawSql(clientQuery); + const client = await models.Client.findById(clientId, null, options); + await client.updateAttribute('salesPersonFk', salesPersonId, options); - await models.Client.updatePortfolio(); + await models.Client.updatePortfolio(options); const portfolioQuery = `SELECT portfolioWeight FROM bs.salesPerson WHERE workerFk = ${salesPersonId}; `; const [salesPerson] = await models.Client.rawSql(portfolioQuery, null, options); @@ -30,21 +48,21 @@ describe('Client updatePortfolio', () => { }); it('should keep the same portfolioWeight when a salesperson is unassigned of a client', async() => { - pending('task 3817'); + const clientId = 1107; const salesPersonId = 19; const tx = await models.Client.beginTransaction({}); try { const options = {transaction: tx}; - const expectedResult = 34.40; - await models.Client.rawSql(`UPDATE vn.client SET salesPersonFk = NULL WHERE id = ${clientId}; `); + const client = await models.Client.findById(clientId, null, options); + await client.updateAttribute('salesPersonFk', null, options); await models.Client.updatePortfolio(); const portfolioQuery = `SELECT portfolioWeight FROM bs.salesPerson WHERE workerFk = ${salesPersonId}; `; - const [salesPerson] = await models.Client.rawSql(portfolioQuery, null, options); + const [salesPerson] = await models.Client.rawSql(portfolioQuery); expect(salesPerson.portfolioWeight).toEqual(expectedResult); diff --git a/modules/client/back/methods/client/updatePortfolio.js b/modules/client/back/methods/client/updatePortfolio.js index 3d522f6c8..809a84636 100644 --- a/modules/client/back/methods/client/updatePortfolio.js +++ b/modules/client/back/methods/client/updatePortfolio.js @@ -13,8 +13,13 @@ module.exports = function(Self) { } }); - Self.updatePortfolio = async() => { + Self.updatePortfolio = async options => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + query = `CALL bs.salesPerson_updatePortfolio()`; - return await Self.rawSql(query); + return Self.rawSql(query, null, myOptions); }; }; diff --git a/modules/client/back/models/client.json b/modules/client/back/models/client.json index b9951e8bb..1426152a4 100644 --- a/modules/client/back/models/client.json +++ b/modules/client/back/models/client.json @@ -136,6 +136,9 @@ "mysql": { "columnName": "businessTypeFk" } + }, + "salesPersonFk": { + "type": "number" } }, "relations": {