diff --git a/modules/ticket/back/methods/ticket/filter.js b/modules/ticket/back/methods/ticket/filter.js index 51979cf11..28de7b8ee 100644 --- a/modules/ticket/back/methods/ticket/filter.js +++ b/modules/ticket/back/methods/ticket/filter.js @@ -100,34 +100,27 @@ module.exports = Self => { }); Self.filter = async(ctx, filter) => { + const userId = ctx.req.accessToken.userId; const conn = Self.dataSource.connector; + const models = Self.app.models; const args = ctx.args; - let worker = await Self.app.models.Worker.findOne({ - where: {userFk: ctx.req.accessToken.userId}, - include: [ - {relation: 'collegues'} - ] - }); - - let teamIds = []; - - if (worker.collegues().length && args.myTeam) { - worker.collegues().forEach(collegue => { - teamIds.push(collegue.collegueFk); + // Apply filter by team + const teamMembersId = []; + if (args.myTeam != null) { + const worker = await models.Worker.findById(userId, { + include: { + relation: 'collegues' + } }); - } - - if (args.mine || (worker.collegues().length === 0 && args.myTeam)) { - worker = await Self.app.models.Worker.findOne({ - fields: ['id'], - where: {userFk: ctx.req.accessToken.userId} + const collegues = worker.collegues() || []; + collegues.forEach(collegue => { + teamMembersId.push(collegue.collegueFk); }); - teamIds = [worker && worker.id]; - } - if (ctx.args && (args.mine || args.myTeam)) - args.teamIds = teamIds; + if (teamMembersId.length == 0) + teamMembersId.push(userId); + } if (ctx.args && args.to) { const dateTo = args.to; @@ -156,7 +149,11 @@ module.exports = Self => { return {'ts.stateFk': value}; case 'mine': case 'myTeam': - return {'c.salesPersonFk': {inq: teamIds}}; + if (value) + return {'c.salesPersonFk': {inq: teamMembersId}}; + else + return {'c.salesPersonFk': {nin: teamMembersId}}; + case 'alertLevel': return {'ts.alertLevel': value}; case 'pending': diff --git a/modules/ticket/back/methods/ticket/specs/filter.spec.js b/modules/ticket/back/methods/ticket/specs/filter.spec.js index faaeb82c0..56d351f2e 100644 --- a/modules/ticket/back/methods/ticket/specs/filter.spec.js +++ b/modules/ticket/back/methods/ticket/specs/filter.spec.js @@ -71,4 +71,20 @@ describe('ticket filter()', () => { expect(secondRow.state).toEqual('Entregado'); expect(thirdRow.state).toEqual('Entregado'); }); + + it('should return the tickets from the worker team', async() => { + const ctx = {req: {accessToken: {userId: 9}}, args: {myTeam: true}}; + const filter = {}; + const result = await app.models.Ticket.filter(ctx, filter); + + expect(result.length).toEqual(17); + }); + + it('should return the tickets that are not from the worker team', async() => { + const ctx = {req: {accessToken: {userId: 9}}, args: {myTeam: false}}; + const filter = {}; + const result = await app.models.Ticket.filter(ctx, filter); + + expect(result.length).toEqual(7); + }); });