From 62cc0c3c9e672b895beac083920fdad6c919fd4e Mon Sep 17 00:00:00 2001 From: Dani Herrero Date: Thu, 28 Sep 2017 09:13:42 +0200 Subject: [PATCH] sales person formated --- client/client/src/basic-data/basic-data.html | 3 -- services/client/common/methods/client/card.js | 11 ++++++- .../client/common/methods/client/card.json | 2 +- .../common/methods/client/salesperson.js | 33 ++++++++++++++----- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/client/client/src/basic-data/basic-data.html b/client/client/src/basic-data/basic-data.html index b509fa7a4..07c60750e 100644 --- a/client/client/src/basic-data/basic-data.html +++ b/client/client/src/basic-data/basic-data.html @@ -29,9 +29,6 @@ value-field="id" select-fields="surname" label="Salesperson"> - - {{$parent.$parent.item.name}} {{$parent.$parent.item.surname}} - { Client.remoteMethod('activeSalesPerson', { description: 'returns actives employees with salesperson role', accessType: 'READ', - isStatic: true, accepts: [{ arg: 'filter', type: 'Object', @@ -26,16 +25,17 @@ module.exports = (Client) => { let limit = filter.limit || 10; let where = getCondition(filter.where); - let query = `SELECT em.id, em.name, em.surname FROM Employee em + 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); + callback(null, formatSalesPerson(response)); }) .catch(reject => { callback(reject, null); @@ -44,19 +44,36 @@ module.exports = (Client) => { function getCondition(where) { let out = []; - if(typeof where === 'object') { Object.keys(where).forEach((k) => { let value = where[k]; - if(typeof value !== 'object') { + if (typeof value === 'number') { + out.push(`em.${k}=${value}`); + } else if (typeof value === 'string') { out.push(`em.${k}='${value}'`); - } else { + } 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 ')})` : ''; + } - return out.length ? 'AND ' + out.join(' AND ') : ''; + function formatSalesPerson (response) { + let results = []; + + response.forEach( person => { + results.push({ + id: person.id, + name: `${person.name} ${person.surname}` + }); + }); + + return results; } }; \ No newline at end of file