diff --git a/services/client/common/methods/client/salesperson.js b/services/client/common/methods/client/salesperson.js index dde54fe9f..23ad812b1 100644 --- a/services/client/common/methods/client/salesperson.js +++ b/services/client/common/methods/client/salesperson.js @@ -23,17 +23,17 @@ module.exports = (Client) => { Client.activeSalesPerson = (filter, callback) => { let skip = filter.skip || 0; let limit = filter.limit || 10; - let where = getCondition(filter.where); + let where = getCondition(filter.where, limit, skip); let query = `SELECT em.id, em.name, em.surname FROM Employee em JOIN Account ac ON em.userFk = ac.id JOIN Role ON Role.id = ac.roleFK - WHERE ac.active AND Role.\`name\`='salesPerson' ${where.cond} + WHERE ac.active AND Role.\`name\`='salesPerson' ${where.sql} ORDER BY em.name ASC LIMIT ? OFFSET ?`; - Client.rawSql(query, [where.value, parseInt(limit, 10), parseInt(skip, 10)], callback) + Client.rawSql(query, where.params, callback) .then(response => { callback(null, formatSalesPerson(response)); }) @@ -42,10 +42,21 @@ module.exports = (Client) => { }); }; - function getCondition(where) { - let out = {}; - out.cond = (typeof where === 'object' && where.name && where.name.regexp) ? `AND em.name regexp ?` : '?'; - out.value = (typeof where === 'object' && where.name && where.name.regexp) ? where.name.regexp : ''; + function getCondition(where, limit, skip) { + let out = { + sql: '', + params: [] + }; + if (where && where.or) { + out.sql = `AND (em.name regexp ? OR em.surname regexp ?)`; + where.or.forEach(val => { + Object.keys(val).forEach(key => { + out.params.push(val[key].regexp); + }); + }); + } + out.params.push(parseInt(limit, 10)); + out.params.push(parseInt(skip, 10)); return out; }