diff --git a/client/core/src/components/icon-menu/icon-menu.html b/client/core/src/components/icon-menu/icon-menu.html index 6e3d7fe64..8ace39c5d 100644 --- a/client/core/src/components/icon-menu/icon-menu.html +++ b/client/core/src/components/icon-menu/icon-menu.html @@ -1,5 +1,6 @@
{ + Self.remoteMethod('activeBuyer', { + description: 'Returns actives workers with Buyer role', + accessType: 'READ', + accepts: [{ + arg: 'filter', + type: 'Object', + required: false, + description: 'Filter defining where and paginated data', + http: {source: 'query'} + }], + returns: { + type: 'Worker', + root: true + }, + http: { + path: `/activeBuyer`, + verb: 'get' + } + }); + + Self.activeBuyer = async filter => { + let sqlLimit = ''; + let sqlOffset = ''; + let params = []; + + if (filter.limit) { + sqlLimit = `LIMIT ?`; + params.push(filter.limit); + } + if (filter.skip) { + sqlOffset = `OFFSET ?`; + params.push(filter.skip); + } + + let query = + `SELECT worker.id, worker.firstName, worker.name + FROM vn.worker + JOIN account.user user ON user.id = worker.userFk + JOIN account.role role ON role.name = 'buyer' + JOIN account.roleRole inheritance ON inheritance.role = user.role AND inheritance.inheritsFrom = role.id + WHERE user.active IS TRUE + ORDER BY worker.firstName ASC + ${sqlLimit} ${sqlOffset}`; + + return await Self.rawSql(query, params); + }; +}; diff --git a/services/loopback/common/methods/client/activeSalesPerson.js b/services/loopback/common/methods/client/activeSalesPerson.js index 31a2c7f7e..8c29f5541 100644 --- a/services/loopback/common/methods/client/activeSalesPerson.js +++ b/services/loopback/common/methods/client/activeSalesPerson.js @@ -20,24 +20,10 @@ module.exports = Self => { }); Self.activeSalesPerson = async filter => { - let where = filter.where; - let sqlWhere = ''; let sqlLimit = ''; let sqlOffset = ''; let params = []; - if (where) { - if (where.firstName) { - sqlWhere = `AND (em.firstName LIKE ? OR em.name LIKE ?)`; - let search = where.firstName.like; - params.push(search); - params.push(search); - } - if (where.id) { - sqlWhere = `AND em.id = ?`; - params.push(where.id); - } - } if (filter.limit) { sqlLimit = `LIMIT ?`; params.push(filter.limit); @@ -48,12 +34,13 @@ module.exports = Self => { } let query = - `SELECT em.id, em.firstName, em.name - FROM worker em - JOIN account.user ac ON em.userFk = ac.id - JOIN account.role r ON r.id = ac.role - WHERE ac.active ${sqlWhere} - ORDER BY em.name ASC + `SELECT worker.id, worker.firstName, worker.name + FROM vn.worker + JOIN account.user user ON user.id = worker.userFk + JOIN account.role role ON role.name = 'salesPerson' + JOIN account.roleRole inheritance ON inheritance.role = user.role AND inheritance.inheritsFrom = role.id + WHERE user.active IS TRUE + ORDER BY worker.firstName ASC ${sqlLimit} ${sqlOffset}`; return await Self.rawSql(query, params); diff --git a/services/loopback/common/methods/client/specs/activeBuyer.spec.js b/services/loopback/common/methods/client/specs/activeBuyer.spec.js new file mode 100644 index 000000000..eadff40ed --- /dev/null +++ b/services/loopback/common/methods/client/specs/activeBuyer.spec.js @@ -0,0 +1,13 @@ +const app = require(`${servicesDir}/client/server/server`); + +describe('Client activeBuyer', () => { + it('should return the buyers as result', async () => { + let filter = {}; + let result = await app.models.Client.activeBuyer(filter); + + let isBuyer = await app.models.Account.hasRole(result[0].id, 'buyer'); + + expect(result.length).toEqual(9); + expect(isBuyer).toBeTruthy(); + }); +}); diff --git a/services/loopback/common/methods/client/specs/activeSalesperson.spec.js b/services/loopback/common/methods/client/specs/activeSalesperson.spec.js index f893ab7a8..45748e6e4 100644 --- a/services/loopback/common/methods/client/specs/activeSalesperson.spec.js +++ b/services/loopback/common/methods/client/specs/activeSalesperson.spec.js @@ -1,12 +1,13 @@ const app = require(`${servicesDir}/client/server/server`); describe('Client activeSalesPerson', () => { - it('should call the activeSalesPerson() method with limit of 1', async() => { - let filter = { - limit: 1 - }; + it('should return the sales people as result', async () => { + let filter = {}; let result = await app.models.Client.activeSalesPerson(filter); - expect(result.length).toEqual(1); + let isSalesPerson = await app.models.Account.hasRole(result[0].id, 'salesPerson'); + + expect(result.length).toEqual(10); + expect(isSalesPerson).toBeTruthy(); }); }); diff --git a/services/loopback/common/models/account.js b/services/loopback/common/models/account.js index 3de1a937c..6426bc3a7 100644 --- a/services/loopback/common/models/account.js +++ b/services/loopback/common/models/account.js @@ -8,9 +8,9 @@ module.exports = Self => { }); Self.observe('before save', (ctx, next) => { - if (ctx.currentInstance && ctx.currentInstance.id && ctx.data && ctx.data.password) { + if (ctx.currentInstance && ctx.currentInstance.id && ctx.data && ctx.data.password) ctx.data.password = md5(ctx.data.password); - } + next(); }); @@ -64,7 +64,7 @@ module.exports = Self => { * @param {Integer} userId The user id * @return {Object} User role list */ - Self.getRoles = async function(userId) { + Self.getRoles = async userId => { let result = await Self.rawSql( `SELECT r.name FROM account.user u @@ -73,9 +73,9 @@ module.exports = Self => { WHERE u.id = ?`, [userId]); let roles = []; - for (role of result) { + for (role of result) roles.push(role.name); - } + return roles; }; diff --git a/services/loopback/common/models/client.js b/services/loopback/common/models/client.js index fb5d6d956..2c381620b 100644 --- a/services/loopback/common/models/client.js +++ b/services/loopback/common/models/client.js @@ -10,6 +10,7 @@ module.exports = Self => { require('../methods/client/hasCustomerRole')(Self); require('../methods/client/isValidClient')(Self); require('../methods/client/activeSalesPerson')(Self); + require('../methods/client/activeBuyer')(Self); require('../methods/client/addressesPropagateRe')(Self); require('../methods/client/getDebt')(Self); require('../methods/client/getMana')(Self);