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':