sales person formated

This commit is contained in:
Dani Herrero 2017-09-28 09:13:42 +02:00
parent 677e8ddd62
commit 62cc0c3c9e
4 changed files with 36 additions and 13 deletions

View File

@ -29,9 +29,6 @@
value-field="id" value-field="id"
select-fields="surname" select-fields="surname"
label="Salesperson"> label="Salesperson">
<tpl-item>
{{$parent.$parent.item.name}} {{$parent.$parent.item.surname}}
</tpl-item>
</vn-autocomplete> </vn-autocomplete>
<vn-autocomplete vn-one <vn-autocomplete vn-one
initial-data="$ctrl.client.contactChannel" initial-data="$ctrl.client.contactChannel"

View File

@ -29,8 +29,17 @@ module.exports = function(Client) {
Client.find(filter, function(err, instances) { Client.find(filter, function(err, instances) {
if (!err) { if (!err) {
cb(null, instances[0]); cb(null, formatCard(instances[0]));
} }
}); });
}; };
function formatCard(card) {
let cardFormated = JSON.parse(JSON.stringify(card));
cardFormated.salesPerson = {
id: card.salesPerson().id,
name: `${card.salesPerson().name} ${card.salesPerson().surname}`
};
return cardFormated;
}
}; };

View File

@ -2,7 +2,7 @@
{ {
"relation": "salesPerson", "relation": "salesPerson",
"scope": { "scope": {
"fields": ["id", "name"] "fields": ["id", "name", "surname"]
} }
}, { }, {
"relation": "contactChannel", "relation": "contactChannel",

View File

@ -2,7 +2,6 @@ module.exports = (Client) => {
Client.remoteMethod('activeSalesPerson', { Client.remoteMethod('activeSalesPerson', {
description: 'returns actives employees with salesperson role', description: 'returns actives employees with salesperson role',
accessType: 'READ', accessType: 'READ',
isStatic: true,
accepts: [{ accepts: [{
arg: 'filter', arg: 'filter',
type: 'Object', type: 'Object',
@ -26,16 +25,17 @@ module.exports = (Client) => {
let limit = filter.limit || 10; let limit = filter.limit || 10;
let where = getCondition(filter.where); 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 Account ac ON em.userFk = ac.id
JOIN Role ON Role.id = ac.roleFK JOIN Role ON Role.id = ac.roleFK
WHERE ac.active AND Role.\`name\`='salesPerson' ${where} WHERE ac.active AND Role.\`name\`='salesPerson' ${where}
ORDER BY em.name ASC ORDER BY em.name ASC
LIMIT ${limit} OFFSET ${skip}`; LIMIT ${limit} OFFSET ${skip}`;
Client.rawSql(query, [], callback) Client.rawSql(query, [], callback)
.then(response => { .then(response => {
callback(null, response); callback(null, formatSalesPerson(response));
}) })
.catch(reject => { .catch(reject => {
callback(reject, null); callback(reject, null);
@ -44,19 +44,36 @@ module.exports = (Client) => {
function getCondition(where) { function getCondition(where) {
let out = []; let out = [];
if(typeof where === 'object') { if(typeof where === 'object') {
Object.keys(where).forEach((k) => { Object.keys(where).forEach((k) => {
let value = where[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}'`); 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]; let firstProperty = Object.keys(value)[0];
out.push(`em.${k} ${firstProperty} '${value[firstProperty]}'`); 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;
} }
}; };