#857 ticket/index buscador avanzado

This commit is contained in:
Gerard 2018-12-14 11:44:04 +01:00
parent 380294a7f6
commit 080c6d5b01
1 changed files with 27 additions and 13 deletions

View File

@ -80,6 +80,7 @@ module.exports = Self => {
}); });
Self.filter = async (ctx, filter) => { Self.filter = async (ctx, filter) => {
console.log(filter);
let conn = Self.dataSource.connector; let conn = Self.dataSource.connector;
let worker = await Self.app.models.Worker.findOne({ let worker = await Self.app.models.Worker.findOne({
@ -90,15 +91,34 @@ module.exports = Self => {
}); });
let teamIds = []; 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) => { let where = buildFilter(ctx.args, (param, value) => {
switch (param) { switch (param) {
case 'search': case 'search':
return /^\d+$/.test(value) return /^\d+$/.test(value)
? {'t.id': value} ? {'t.id': {inq: value}}
: {'t.nickname': {like: `%${value}%`}}; : {'t.nickname': {like: `%${value}%`}};
case 'from': case 'from':
return {'t.shipped': {gte: value}}; return {'t.shipped': {gte: value}};
@ -112,8 +132,6 @@ module.exports = Self => {
return {'a.provinceFk': value}; return {'a.provinceFk': value};
case 'stateFk': case 'stateFk':
return {'ts.stateFk': value}; return {'ts.stateFk': value};
case 'orderFk':
return {'ot.orderFk': value};
case 'myTeam': case 'myTeam':
return {'c.salesPersonFk': {inq: teamIds}}; return {'c.salesPersonFk': {inq: teamIds}};
case 'id': case 'id':
@ -148,8 +166,7 @@ module.exports = Self => {
w.name AS warehouse, w.name AS warehouse,
am.name AS agencyMode, am.name AS agencyMode,
st.name AS state, st.name AS state,
wk.name AS salesPerson, wk.name AS salesPerson
ot.orderFk AS orderFk
FROM ticket t FROM ticket t
LEFT JOIN address a ON a.id = t.addressFk LEFT JOIN address a ON a.id = t.addressFk
LEFT JOIN province p ON p.id = a.provinceFk 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 ticketState ts ON ts.ticketFk = t.id
LEFT JOIN state st ON st.id = ts.stateFk LEFT JOIN state st ON st.id = ts.stateFk
LEFT JOIN client c ON c.id = t.clientFk LEFT JOIN client c ON c.id = t.clientFk
LEFT JOIN worker wk ON wk.id = c.salesPersonFk LEFT JOIN worker wk ON wk.id = c.salesPersonFk`);
LEFT JOIN orderTicket ot ON ot.ticketFk = t.id`); stmt.merge(conn.makeSuffix(filter));
stmt.merge(conn.makeWhere(filter.where));
stmt.merge('GROUP BY t.id');
stmt.merge(conn.makePagination(filter));
stmts.push(stmt); stmts.push(stmt);
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.ticketGetProblems'); stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.ticketGetProblems');