From 59577bcb7f4e19958e70a9fb81585a0f3a2918ce Mon Sep 17 00:00:00 2001 From: Dani Herrero Date: Wed, 18 Oct 2017 15:06:38 +0200 Subject: [PATCH] bug fixed rawSQL --- .../common/methods/client/salesperson.js | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/services/client/common/methods/client/salesperson.js b/services/client/common/methods/client/salesperson.js index 6b493f996..c9e3651f2 100644 --- a/services/client/common/methods/client/salesperson.js +++ b/services/client/common/methods/client/salesperson.js @@ -29,11 +29,11 @@ module.exports = (Client) => { 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} + WHERE ac.active AND Role.\`name\`='salesPerson' ${where.cond} ORDER BY em.name ASC - LIMIT ${limit} OFFSET ${skip}`; - - Client.rawSql(query, [], callback) + LIMIT ? OFFSET ?`; + + Client.rawSql(query, [where.value, limit, skip], callback) .then(response => { callback(null, formatSalesPerson(response)); }) @@ -43,31 +43,16 @@ module.exports = (Client) => { }; function getCondition(where) { - let out = []; - if(typeof where === 'object') { - Object.keys(where).forEach((k) => { - let value = where[k]; - if (typeof value === 'number') { - out.push(`em.${k}=${value}`); - } else if (typeof value === 'string') { - out.push(`em.${k}='${value}'`); - } else if (typeof value === 'boolean' || value === null) { - out.push(`em.${k} IS ${String(value).toUpperCase()}`); - } else if (Object.keys(value).length) { - let firstProperty = Object.keys(value)[0]; - out.push(`em.${k} ${firstProperty} '${value[firstProperty]}'`); - } else { - throw new Error ('Error: unexpected type'); - } - }); - } - return out.length ? `AND (${out.join(' AND ')})` : ''; + 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 : ''; + return out; } - function formatSalesPerson (response) { + function formatSalesPerson(response) { let results = []; - response.forEach( person => { + response.forEach(person => { results.push({ id: person.id, name: `${person.name} ${person.surname}`