diff --git a/modules/claim/back/methods/claim/filter.js b/modules/claim/back/methods/claim/filter.js new file mode 100644 index 000000000..7a8f3c40c --- /dev/null +++ b/modules/claim/back/methods/claim/filter.js @@ -0,0 +1,118 @@ + +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 client name`, + http: {source: 'query'} + }, { + arg: 'client', + type: 'String', + description: 'The worker name', + http: {source: 'query'} + }, { + arg: 'id', + type: 'Integer', + description: 'The claim id', + http: {source: 'query'} + }, { + arg: 'clientFk', + type: 'Integer', + description: 'The client id', + http: {source: 'query'} + }, { + arg: 'claimStateFk', + type: 'Integer', + description: 'The claim state id', + http: {source: 'query'} + }, { + arg: 'workerFk', + type: 'Integer', + description: 'The worker id', + http: {source: 'query'} + }, { + arg: 'created', + type: 'Date', + description: 'The to date filter', + 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) + ? {'cl.id': value} + : { + or: [ + {'c.name': {like: `%${value}%`}} + ] + }; + case 'client': + return {'c.name': {like: `%${value}%`}}; + case 'id': + return {'cl.id': value}; + case 'clientFk': + return {'c.id': value}; + case 'claimStateFk': + return {'cl.claimStateFk': value}; + case 'workerFk': + return {'cl.workerFk': value}; + case 'created': + return {'cl.created': value}; + } + }); + + filter = mergeFilters(ctx.args.filter, {where}); + + let stmts = []; + let stmt; + + stmt = new ParameterizedSQL( + `SELECT cl.id, c.name, u.nickName, cs.description, cl.created + FROM claim cl + LEFT JOIN client c ON c.id = cl.clientFk + LEFT JOIN worker w ON w.id = cl.workerFk + LEFT JOIN account.user u ON u.id = w.userFk + LEFT JOIN claimState cs ON cs.id = cl.claimStateFk` + ); + + + 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/claim/back/methods/claim/specs/filter.spec.js b/modules/claim/back/methods/claim/specs/filter.spec.js new file mode 100644 index 000000000..187fffaa4 --- /dev/null +++ b/modules/claim/back/methods/claim/specs/filter.spec.js @@ -0,0 +1,27 @@ +const app = require('vn-loopback/server/server'); + +describe('claim filter()', () => { + it('should return 1 result filtering by id', async() => { + let result = await app.models.Claim.filter({args: {filter: {}, search: 1}}); + + expect(result.length).toEqual(1); + expect(result[0].id).toEqual(1); + }); + + it('should return 1 result filtering by string', async() => { + let result = await app.models.Claim.filter({args: {filter: {}, search: 'Tony Stark'}}); + + expect(result.length).toEqual(1); + expect(result[0].id).toEqual(4); + }); + + it('should return 4 results filtering by worker id', async() => { + let result = await app.models.Claim.filter({args: {filter: {}, workerFk: 18}}); + + expect(result.length).toEqual(4); + expect(result[0].id).toEqual(1); + expect(result[1].id).toEqual(2); + expect(result[2].id).toEqual(3); + expect(result[3].id).toEqual(4); + }); +}); diff --git a/modules/claim/back/models/claim.js b/modules/claim/back/models/claim.js index dc07ebea7..415b6f0e9 100644 --- a/modules/claim/back/models/claim.js +++ b/modules/claim/back/models/claim.js @@ -3,4 +3,5 @@ module.exports = Self => { require('../methods/claim/createFromSales')(Self); require('../methods/claim/updateClaim')(Self); require('../methods/claim/regularizeClaim')(Self); + require('../methods/claim/filter')(Self); }; diff --git a/modules/claim/front/index/index.html b/modules/claim/front/index/index.html index 92b798420..f6945e834 100644 --- a/modules/claim/front/index/index.html +++ b/modules/claim/front/index/index.html @@ -1,19 +1,16 @@ + order="claimStateFk ASC, created DESC">
@@ -39,7 +36,7 @@ {{::claim.id}} - {{::claim.client.name}} + {{::claim.name}} {{::claim.created | dateTime:'dd/MM/yyyy'}} @@ -47,12 +44,12 @@ - {{::claim.worker.user.nickname}} + {{::claim.nickName}} - {{::claim.claimState.description}} + {{::claim.description}} diff --git a/modules/claim/front/index/index.js b/modules/claim/front/index/index.js index ec9624845..c8462bc48 100644 --- a/modules/claim/front/index/index.js +++ b/modules/claim/front/index/index.js @@ -4,58 +4,10 @@ export default class Controller { constructor($scope) { this.$ = $scope; this.ticketSelected = null; - - this.filter = { - include: [ - { - relation: 'client', - scope: { - fields: ['name'] - } - }, - { - relation: 'worker', - scope: { - fields: ['userFk'], - include: { - relation: 'user', - scope: { - fields: ['nickname'] - } - } - } - }, - { - relation: 'claimState', - scope: { - fields: ['description'] - } - } - ], - order: 'claimStateFk ASC, created DESC' - }; - } - - exprBuilder(param, value) { - switch (param) { - case 'search': - return /^\d+$/.test(value) - ? {id: value} - : {client: {like: `%${value}%`}}; - case 'client': - return {[param]: {like: `%${value}%`}}; - case 'created': - return {created: {between: [value, value]}}; - case 'id': - case 'clientFk': - case 'workerFk': - case 'claimStateFk': - return {[param]: value}; - } } stateColor(claim) { - switch (claim.claimState.description) { + switch (claim.description) { case 'Pendiente': return 'warning'; case 'Gestionado': @@ -91,6 +43,13 @@ export default class Controller { onDescriptorLoad() { this.$.popover.relocate(); } + + onSearch(params) { + if (params) + this.$.model.applyFilter(null, params); + else + this.$.model.clear(); + } } Controller.$inject = ['$scope']; diff --git a/modules/worker/front/index/index.html b/modules/worker/front/index/index.html index 9c590cc1c..70c3b806f 100644 --- a/modules/worker/front/index/index.html +++ b/modules/worker/front/index/index.html @@ -1,6 +1,6 @@