diff --git a/client/client/src/basic-data/basic-data.html b/client/client/src/basic-data/basic-data.html index e82ade4f6..b509fa7a4 100644 --- a/client/client/src/basic-data/basic-data.html +++ b/client/client/src/basic-data/basic-data.html @@ -24,7 +24,7 @@ { }); Client.activeSalesPerson = (filter, callback) => { - var filter = filter || {}; + let skip = filter.skip || 0; + let limit = filter.limit || 10; + let where = getCondition(filter.where); - filter.include = [ - { - "relation": "role", - "scope": { - "fields": ["name"] - } - }, - { - "relation": 'user' - }]; - console.log(filter); - Client.app.models.Employee.find(filter, (err, instances) => { - if (err) - callback(err, null); - callback(null, instances); + 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} + ORDER BY em.name ASC + LIMIT ${limit} OFFSET ${skip}`; + + Client.rawSql(query, [], callback) + .then(response => { + callback(null, response); + }) + .catch(reject => { + callback(reject, null); }); }; + + function getCondition(where) { + let out = []; + + if(typeof where === 'object') { + Object.keys(where).forEach((k) => { + let value = where[k]; + if(typeof value !== 'object') { + out.push(`em.${k}='${value}'`); + } else { + let firstProperty = Object.keys(value)[0]; + out.push(`em.${k} ${firstProperty} '${value[firstProperty]}'`); + } + }); + } + + return out.length ? 'AND ' + out.join(' AND ') : ''; + } }; \ No newline at end of file diff --git a/services/client/common/models/client.js b/services/client/common/models/client.js index 96f9a2381..e457b1fef 100644 --- a/services/client/common/models/client.js +++ b/services/client/common/models/client.js @@ -13,6 +13,7 @@ module.exports = function(Client) { require('../methods/client/employee.js')(Client); require('../methods/client/filter.js')(Client); require('../methods/client/roles.js')(Client); + require('../methods/client/salesperson.js')(Client); // Validations