From 0b74df5dcb1fc5b612856177a21a62b167d27d44 Mon Sep 17 00:00:00 2001 From: Gerard Date: Tue, 27 Nov 2018 08:50:13 +0100 Subject: [PATCH] #837 crear refactor de activeSalesPerson, activeBuyer en uno solo --- .../common/methods/client/activeBuyer.js | 62 ----------------- .../methods/client/activeSalesPerson.js | 62 ----------------- .../methods/client/activeWorkersWithRole.js | 66 +++++++++++++++++++ services/loopback/common/models/client.js | 3 +- 4 files changed, 67 insertions(+), 126 deletions(-) delete mode 100644 services/loopback/common/methods/client/activeBuyer.js delete mode 100644 services/loopback/common/methods/client/activeSalesPerson.js create mode 100644 services/loopback/common/methods/client/activeWorkersWithRole.js diff --git a/services/loopback/common/methods/client/activeBuyer.js b/services/loopback/common/methods/client/activeBuyer.js deleted file mode 100644 index f29d1f194..000000000 --- a/services/loopback/common/methods/client/activeBuyer.js +++ /dev/null @@ -1,62 +0,0 @@ -module.exports = Self => { - 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 sqlWhere = ''; - let sqlLimit = ''; - let sqlOffset = ''; - let params = []; - - if (filter.where) { - if (filter.where.firstName) { - sqlWhere = `AND (worker.firstName LIKE ? OR worker.name LIKE ?)`; - let search = where.firstName.like; - params.push(search); - params.push(search); - } - if (filter.where.id) { - sqlWhere = `AND worker.id = ?`; - params.push(filter.where.id); - } - } - - 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 ${sqlWhere} - 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 deleted file mode 100644 index bc801314b..000000000 --- a/services/loopback/common/methods/client/activeSalesPerson.js +++ /dev/null @@ -1,62 +0,0 @@ -module.exports = Self => { - Self.remoteMethod('activeSalesPerson', { - description: 'Returns actives workers with salesperson 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: `/activeSalesPerson`, - verb: 'get' - } - }); - - Self.activeSalesPerson = async filter => { - let sqlWhere = ''; - let sqlLimit = ''; - let sqlOffset = ''; - let params = []; - - if (filter.where) { - if (filter.where.firstName) { - sqlWhere = `AND (worker.firstName LIKE ? OR worker.name LIKE ?)`; - let search = where.firstName.like; - params.push(search); - params.push(search); - } - if (filter.where.id) { - sqlWhere = `AND worker.id = ?`; - params.push(filter.where.id); - } - } - - 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 = 'salesPerson' - JOIN account.roleRole inheritance ON inheritance.role = user.role AND inheritance.inheritsFrom = role.id - WHERE user.active IS TRUE ${sqlWhere} - ORDER BY worker.firstName ASC - ${sqlLimit} ${sqlOffset}`; - - return await Self.rawSql(query, params); - }; -}; diff --git a/services/loopback/common/methods/client/activeWorkersWithRole.js b/services/loopback/common/methods/client/activeWorkersWithRole.js new file mode 100644 index 000000000..df934f398 --- /dev/null +++ b/services/loopback/common/methods/client/activeWorkersWithRole.js @@ -0,0 +1,66 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +const buildFilter = require('../../filter.js').buildFilter; +const mergeFilters = require('../../filter.js').mergeFilters; + +module.exports = Self => { + Self.remoteMethod('activeWorkersWithRole', { + description: 'Returns actives workers with salesperson role', + accessType: 'READ', + accepts: [{ + arg: 'filter', + type: 'Object', + description: 'Filter defining where and paginated data', + required: true + }], + returns: { + type: 'Worker', + root: true + }, + http: { + path: `/activeWorkersWithRole`, + verb: 'get' + } + }); + + Self.activeWorkersWithRole = async filter => { + let conn = Self.dataSource.connector; + if (filter.where.and && Array.isArray(filter.where.and)) { + let where = {}; + filter.where.and.forEach(element => { + where[Object.keys(element)[0]] = Object.values(element)[0]; + }); + filter.where = where; + } + let clientFilter = Object.assign({}, filter); + clientFilter.where = buildFilter(filter.where, (param, value) => { + switch (param) { + case 'role': + return {'r.name': value}; + case 'firstName': + return { + 'w.firstName': {like: `%${value}%`}, + 'w.name': {like: `%${value}%`}, + }; + case 'id': + return {'w.id': value}; + } + }); + + let myFilter = { + where: {'u.active': true} + }; + + console.log(filter.where); + myFilter = mergeFilters(myFilter, clientFilter); + + let stmt = new ParameterizedSQL( + `SELECT DISTINCT w.id, w.firstName, w.name + FROM worker w + JOIN account.user u ON u.id = w.userFk + JOIN account.roleRole i ON i.role = u.role + JOIN account.role r ON r.id = i.inheritsFrom` + ); + stmt.merge(conn.makeSuffix(myFilter)); + return await conn.executeStmt(stmt); + }; +}; diff --git a/services/loopback/common/models/client.js b/services/loopback/common/models/client.js index 0341793a1..f117e272d 100644 --- a/services/loopback/common/models/client.js +++ b/services/loopback/common/models/client.js @@ -4,13 +4,12 @@ let isMultiple = require('../helpers').isMultiple; module.exports = Self => { // Methods + require('../methods/client/activeWorkersWithRole')(Self); require('../methods/client/getCard')(Self); require('../methods/client/createWithUser')(Self); require('../methods/client/listWorkers')(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);