From f6eb8315a3adb9489dfe2cd78b5673fd12e0acef Mon Sep 17 00:00:00 2001 From: Gerard Date: Mon, 11 Mar 2019 08:00:49 +0100 Subject: [PATCH] #999 worker.index buscador --- modules/worker/back/methods/worker/filter.js | 126 +++++++++++++++++++ modules/worker/back/models/worker.js | 3 + modules/worker/front/index/index.html | 18 ++- modules/worker/front/index/index.js | 29 +---- modules/worker/front/locale/es.yml | 6 +- modules/worker/front/search-panel/index.html | 38 +++++- 6 files changed, 181 insertions(+), 39 deletions(-) create mode 100644 modules/worker/back/methods/worker/filter.js create mode 100644 modules/worker/back/models/worker.js diff --git a/modules/worker/back/methods/worker/filter.js b/modules/worker/back/methods/worker/filter.js new file mode 100644 index 000000000..cd644730a --- /dev/null +++ b/modules/worker/back/methods/worker/filter.js @@ -0,0 +1,126 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +const buildFilter = require('vn-loopback/util/filter').buildFilter; +const mergeFilters = require('vn-loopback/util/filter').mergeFilters; + +module.exports = Self => { + Self.remoteMethodCtx('filter', { + description: 'Find all instances of the model matched by filter from the data source.', + accessType: 'READ', + accepts: [ + { + arg: 'filter', + type: 'Object', + description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', + http: {source: 'query'} + }, { + arg: 'tags', + type: ['Object'], + description: 'List of tags to filter with', + http: {source: 'query'} + }, { + arg: 'search', + type: 'String', + description: `If it's and integer searchs by id, otherwise it searchs by name`, + http: {source: 'query'} + }, { + arg: 'id', + type: 'Integer', + description: 'The worker id', + http: {source: 'query'} + }, { + arg: 'fi', + type: 'String', + description: 'The worker fi', + http: {source: 'query'} + }, { + arg: 'departmentFk', + type: 'Integer', + description: 'The worker department id', + http: {source: 'query'} + }, { + arg: 'extension', + type: 'Integer', + description: 'The worker extension id', + http: {source: 'query'} + }, { + arg: 'firstName', + type: 'String', + description: 'The worker firstName', + http: {source: 'query'} + }, { + arg: 'name', + type: 'String', + description: 'The worker name', + http: {source: 'query'} + }, { + arg: 'nickname', + type: 'String', + description: 'The worker name', + http: {source: 'query'} + } + ], + returns: { + type: ['Object'], + root: true + }, + http: { + path: `/filter`, + verb: 'GET' + } + }); + + Self.filter = async(ctx, filter) => { + let conn = Self.dataSource.connector; + + let where = buildFilter(ctx.args, (param, value) => { + switch (param) { + case 'search': + return /^\d+$/.test(value) + ? {'w.id': value} + : {or: [ + {'w.firstName': {like: `%${value}%`}}, + {'w.name': {like: `%${value}%`}} + ]}; + case 'id': + return {'w.id': value}; + case 'name': + return {'w.name': {like: `%${value}%`}}; + case 'firstName': + return {'w.firstName': {like: `%${value}%`}}; + case 'extension': + return {'p.extension': value}; + case 'fi': + return {'c.fi': value}; + case 'departmentFk': + return {'d.id': value}; + case 'userName': + return {'u.name': {like: `%${value}%`}}; + } + }); + + filter = mergeFilters(ctx.args.filter, {where}); + + let stmts = []; + let stmt; + + stmt = new ParameterizedSQL( + `SELECT w.id, u.email, p.extension, u.name as userName, + d.name AS department, w.name, u.nickname + FROM worker w + LEFT JOIN workerDepartment wd ON wd.workerFk = w.id + LEFT JOIN department d ON d.id = wd.departmentFk + LEFT JOIN client c ON c.id = w.userFk + LEFT JOIN account.user u ON u.id = w.userFk + LEFT JOIN pbx.sip p ON p.user_id = u.id` + ); + + + stmt.merge(conn.makeSuffix(filter)); + let itemsIndex = stmts.push(stmt) - 1; + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await conn.executeStmt(sql); + return itemsIndex === 0 ? result : result[itemsIndex]; + }; +}; diff --git a/modules/worker/back/models/worker.js b/modules/worker/back/models/worker.js new file mode 100644 index 000000000..91939c66f --- /dev/null +++ b/modules/worker/back/models/worker.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/worker/filter')(Self); +}; diff --git a/modules/worker/front/index/index.html b/modules/worker/front/index/index.html index 7debfc728..a6fdc511e 100644 --- a/modules/worker/front/index/index.html +++ b/modules/worker/front/index/index.html @@ -1,8 +1,8 @@
@@ -10,8 +10,7 @@ @@ -22,21 +21,18 @@ class="vn-list-item"> -
{{::worker.user.nickname}}
+
{{::worker.nickname}}
+ value="{{::worker.userName}}"> - - + value="{{::worker.userName}}@verdnatura.es"> + value="{{::worker.department}}">
diff --git a/modules/worker/front/index/index.js b/modules/worker/front/index/index.js index 53a601ca7..333edcd4d 100644 --- a/modules/worker/front/index/index.js +++ b/modules/worker/front/index/index.js @@ -5,33 +5,14 @@ export default class Controller { Object.assign(this, { $, selectedWorker: null, - include: [ - { - relation: 'user', - scope: {fields: ['nickname', 'email']} - }, { - relation: 'client', - scope: {fields: ['fi']} - } - ] }); } - exprBuilder(param, value) { - switch (param) { - case 'search': - return /^\d+$/.test(value) - ? {id: value} - : {or: [ - {firstName: {like: `%${value}%`}}, - {name: {like: `%${value}%`}} - ]}; - case 'name': - case 'firstName': - return {[param]: {like: `%${value}%`}}; - case 'id': - return {[param]: value}; - } + onSearch(params) { + if (params) + this.$.model.applyFilter(null, params); + else + this.$.model.clear(); } preview(event, worker) { diff --git a/modules/worker/front/locale/es.yml b/modules/worker/front/locale/es.yml index 7caf36e0b..fec7b0995 100644 --- a/modules/worker/front/locale/es.yml +++ b/modules/worker/front/locale/es.yml @@ -7,4 +7,8 @@ Department: Departamento User id: Id de usuario Role: Rol Extension: Extensión -Go to client: Ir al cliente \ No newline at end of file +Go to client: Ir al cliente +View worker: Ver trabajador +Worker id: Id trabajador +Fiscal Identifier: NIF +User name: Usuario diff --git a/modules/worker/front/search-panel/index.html b/modules/worker/front/search-panel/index.html index 36945f33d..67fd0ffcc 100644 --- a/modules/worker/front/search-panel/index.html +++ b/modules/worker/front/search-panel/index.html @@ -11,22 +11,54 @@ + label="User id" + model="filter.id"> + + + + + + + + + + + + + +