diff --git a/services/loopback/common/methods/ticket/filter.js b/services/loopback/common/methods/ticket/filter.js index aaba6ea93..2095aa307 100644 --- a/services/loopback/common/methods/ticket/filter.js +++ b/services/loopback/common/methods/ticket/filter.js @@ -80,6 +80,7 @@ module.exports = Self => { }); Self.filter = async (ctx, filter) => { + console.log(filter); let conn = Self.dataSource.connector; let worker = await Self.app.models.Worker.findOne({ @@ -90,15 +91,34 @@ module.exports = Self => { }); let teamIds = []; - worker.collegues().forEach(collegue => { - teamIds.push(collegue.collegueFk); - }); + + if (worker.collegues().length) { + worker.collegues().forEach(collegue => { + teamIds.push(collegue.collegueFk); + }); + } else { + worker = await Self.app.models.Worker.findOne({ + fields: ['id'], + where: {userFk: ctx.req.accessToken.userId} + }); + teamIds = [worker && worker.id]; + console.log(worker); + } + + let orderTickets = []; + if (ctx.args.orderFk) { + let ticketsToSearch = await Self.app.models.OrderTickets.find({where: {orderFk: filter.orderFk}}); + ticketsToSearch.forEach(ticket => { + orderTickets.push(ticket.ticketFk); + }); + ctx.args.search = orderTickets; + } let where = buildFilter(ctx.args, (param, value) => { switch (param) { case 'search': return /^\d+$/.test(value) - ? {'t.id': value} + ? {'t.id': {inq: value}} : {'t.nickname': {like: `%${value}%`}}; case 'from': return {'t.shipped': {gte: value}}; @@ -112,8 +132,6 @@ module.exports = Self => { return {'a.provinceFk': value}; case 'stateFk': return {'ts.stateFk': value}; - case 'orderFk': - return {'ot.orderFk': value}; case 'myTeam': return {'c.salesPersonFk': {inq: teamIds}}; case 'id': @@ -148,8 +166,7 @@ module.exports = Self => { w.name AS warehouse, am.name AS agencyMode, st.name AS state, - wk.name AS salesPerson, - ot.orderFk AS orderFk + wk.name AS salesPerson FROM ticket t LEFT JOIN address a ON a.id = t.addressFk LEFT JOIN province p ON p.id = a.provinceFk @@ -158,11 +175,8 @@ module.exports = Self => { LEFT JOIN ticketState ts ON ts.ticketFk = t.id LEFT JOIN state st ON st.id = ts.stateFk LEFT JOIN client c ON c.id = t.clientFk - LEFT JOIN worker wk ON wk.id = c.salesPersonFk - LEFT JOIN orderTicket ot ON ot.ticketFk = t.id`); - stmt.merge(conn.makeWhere(filter.where)); - stmt.merge('GROUP BY t.id'); - stmt.merge(conn.makePagination(filter)); + LEFT JOIN worker wk ON wk.id = c.salesPersonFk`); + stmt.merge(conn.makeSuffix(filter)); stmts.push(stmt); stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.ticketGetProblems');