From 82a1d00b0c4e3b9e52d9d19d24c4b7a86f97d205 Mon Sep 17 00:00:00 2001 From: Bernat Date: Wed, 3 Jul 2019 14:06:27 +0200 Subject: [PATCH] =?UTF-8?q?Tarea=20#1516=20order.index=20buscador=20avanza?= =?UTF-8?q?do=20a=C3=B1adir=20mi=20equipo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/order/back/methods/order/filter.js | 29 +++++++++++++++++++ .../back/methods/order/specs/filter.spec.js | 12 ++++---- modules/order/front/search-panel/index.html | 12 ++++++-- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/modules/order/back/methods/order/filter.js b/modules/order/back/methods/order/filter.js index 4a4fd2c85..5dffef195 100644 --- a/modules/order/back/methods/order/filter.js +++ b/modules/order/back/methods/order/filter.js @@ -47,6 +47,10 @@ module.exports = Self => { arg: 'workerFk', type: 'Integer', description: `The salesperson id` + }, { + arg: 'myTeam', + type: 'Boolean', + description: `Whether to show only tickets for the current logged user team (For now it shows only the current user tickets)` }, { arg: 'isConfirmed', type: 'Boolean', @@ -65,7 +69,30 @@ module.exports = Self => { Self.filter = async(ctx, filter) => { let conn = Self.dataSource.connector; + let worker = await Self.app.models.Worker.findOne({ + where: {userFk: ctx.req.accessToken.userId}, + include: [ + {relation: 'collegues'} + ] + }); + let teamIds = []; + if (worker.collegues().length && ctx.args.myTeam) { + worker.collegues().forEach(collegue => { + teamIds.push(collegue.collegueFk); + }); + } + + if (worker.collegues().length === 0 && ctx.args.myTeam) { + worker = await Self.app.models.Worker.findOne({ + fields: ['id'], + where: {userFk: ctx.req.accessToken.userId} + }); + teamIds = [worker && worker.id]; + } + + if (ctx.args && ctx.args.myTeam) + ctx.args.teamIds = teamIds; let where = buildFilter(ctx.args, (param, value) => { switch (param) { case 'search': @@ -86,6 +113,8 @@ module.exports = Self => { return {'ort.ticketFk': value}; case 'isConfirmed': return {'o.confirmed': value ? 1 : 0}; + case 'myTeam': + return {'c.salesPersonFk': {inq: teamIds}}; case 'id': param = `o.${param}`; return {[param]: value}; diff --git a/modules/order/back/methods/order/specs/filter.spec.js b/modules/order/back/methods/order/specs/filter.spec.js index cf9df347f..fc987613a 100644 --- a/modules/order/back/methods/order/specs/filter.spec.js +++ b/modules/order/back/methods/order/specs/filter.spec.js @@ -1,9 +1,13 @@ const app = require('vn-loopback/server/server'); describe('order filter()', () => { - it('should call the filter method with a basic search', async() => { - let ctx = {params: {}}; + let ctx = { + req: {accessToken: {userId: 9}}, + args: {}, + params: {} + }; + it('should call the filter method with a basic search', async() => { let filter = {where: {'o.id': 2}}; let result = await app.models.Order.filter(ctx, filter); let orderId = result[0].id; @@ -12,8 +16,6 @@ describe('order filter()', () => { }); it('should call the filter method with a single advanced search', async() => { - let ctx = {params: {}}; - let filter = {where: {'o.confirmed': false}}; let result = await app.models.Order.filter(ctx, filter); @@ -21,8 +23,6 @@ describe('order filter()', () => { }); it('should call the filter method with a complex advanced search', async() => { - let ctx = {params: {}}; - let filter = {where: {'o.confirmed': false, 'c.salesPersonFk': 19}}; let result = await app.models.Order.filter(ctx, filter); diff --git a/modules/order/front/search-panel/index.html b/modules/order/front/search-panel/index.html index 2a6f33b8c..953524625 100644 --- a/modules/order/front/search-panel/index.html +++ b/modules/order/front/search-panel/index.html @@ -57,8 +57,6 @@ label="Ticket id" model="filter.ticketFk"> - - - + + + + +