From e6a8b34fe461cabfbf7d3623dd983f6abf75facf Mon Sep 17 00:00:00 2001 From: Daniel Herrero Date: Wed, 14 Mar 2018 13:59:01 +0100 Subject: [PATCH] filter tickets --- client/ticket/src/list/ticket-list.html | 2 +- .../loopback/common/methods/ticket/filter.js | 42 +++++++++++++++++++ services/loopback/common/models/ticket.js | 1 + 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 services/loopback/common/methods/ticket/filter.js diff --git a/client/ticket/src/list/ticket-list.html b/client/ticket/src/list/ticket-list.html index 9952267f0..51750d20a 100644 --- a/client/ticket/src/list/ticket-list.html +++ b/client/ticket/src/list/ticket-list.html @@ -1,4 +1,4 @@ - +
diff --git a/services/loopback/common/methods/ticket/filter.js b/services/loopback/common/methods/ticket/filter.js new file mode 100644 index 000000000..aaf46ec97 --- /dev/null +++ b/services/loopback/common/methods/ticket/filter.js @@ -0,0 +1,42 @@ +module.exports = Self => { + Self.installMethod('filter', filterParams); + + function filterParams(params) { + let filters = { + where: {}, + skip: (params.page - 1) * params.size, + limit: params.size, + order: params.order || 'created DESC' + }; + + delete params.page; + delete params.size; + delete params.order; + + if (params.search) { + filters.where.and = [ + { + or: [ + {id: params.search}, + {name: {regexp: params.search}} + ] + } + ]; + delete params.search; + } + + Object.keys(params).forEach( + key => { + if (filters.where.and) { + let filter = {}; + filter[key] = (key === 'nickname') ? {regexp: params[key]} : params[key]; + filters.where.and.push(filter); + } else { + filters.where[key] = (key === 'nickname') ? {regexp: params[key]} : params[key]; + } + } + ); + + return filters; + } +}; diff --git a/services/loopback/common/models/ticket.js b/services/loopback/common/models/ticket.js index f390c559f..108ebc872 100644 --- a/services/loopback/common/models/ticket.js +++ b/services/loopback/common/models/ticket.js @@ -2,4 +2,5 @@ module.exports = function(Self) { require('../methods/ticket/change-time.js')(Self); require('../methods/ticket/change-worker.js')(Self); + require('../methods/ticket/filter.js')(Self); };