[finish] active salesPerson service

This commit is contained in:
Dani Herrero 2017-09-27 09:25:33 +02:00
parent 4eeb5df74f
commit fbd5fea126
3 changed files with 36 additions and 17 deletions

View File

@ -24,7 +24,7 @@
<vn-autocomplete vn-one
initial-data="$ctrl.client.salesPerson"
field="$ctrl.client.salesPersonFk"
url="/client/api/Employees"
url="/client/api/Clients/activeSalesPerson"
show-field="name"
value-field="id"
select-fields="surname"

View File

@ -22,23 +22,41 @@ module.exports = (Client) => {
});
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 ') : '';
}
};

View File

@ -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