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"
select-fields="surname"
label="Salesperson">
<tpl-item>
{{$parent.$parent.item.name}} {{$parent.$parent.item.surname}}
</tpl-item>
</vn-autocomplete>
<vn-autocomplete vn-one
initial-data="$ctrl.client.contactChannel"

View File

@ -29,8 +29,17 @@ module.exports = function(Client) {
Client.find(filter, function(err, instances) {
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",
"scope": {
"fields": ["id", "name"]
"fields": ["id", "name", "surname"]
}
}, {
"relation": "contactChannel",

View File

@ -2,7 +2,6 @@ module.exports = (Client) => {
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;
}
};