2017-09-27 05:22:40 +00:00
|
|
|
module.exports = (Client) => {
|
|
|
|
Client.remoteMethod('activeSalesPerson', {
|
|
|
|
description: 'returns actives employees with salesperson role',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'Object',
|
|
|
|
required: false,
|
|
|
|
description: 'Filter defining where and paginated data',
|
|
|
|
http: {source: 'query'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
arg: 'data',
|
|
|
|
type: 'Employee',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/activeSalesPerson`,
|
|
|
|
verb: 'get'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Client.activeSalesPerson = (filter, callback) => {
|
2017-09-27 07:25:33 +00:00
|
|
|
let skip = filter.skip || 0;
|
|
|
|
let limit = filter.limit || 10;
|
2017-11-21 10:50:37 +00:00
|
|
|
let where = getCondition(filter.where, limit, skip);
|
2017-09-27 05:22:40 +00:00
|
|
|
|
2017-09-28 07:13:42 +00:00
|
|
|
let query = `SELECT em.id, em.name, em.surname
|
|
|
|
FROM Employee em
|
2017-09-27 07:25:33 +00:00
|
|
|
JOIN Account ac ON em.userFk = ac.id
|
|
|
|
JOIN Role ON Role.id = ac.roleFK
|
2017-11-21 10:50:37 +00:00
|
|
|
WHERE ac.active AND Role.\`name\`='salesPerson' ${where.sql}
|
2017-09-27 07:25:33 +00:00
|
|
|
ORDER BY em.name ASC
|
2017-10-18 13:06:38 +00:00
|
|
|
LIMIT ? OFFSET ?`;
|
|
|
|
|
2017-11-21 10:50:37 +00:00
|
|
|
Client.rawSql(query, where.params, callback)
|
2017-09-27 07:25:33 +00:00
|
|
|
.then(response => {
|
2017-09-28 07:13:42 +00:00
|
|
|
callback(null, formatSalesPerson(response));
|
2017-09-27 07:25:33 +00:00
|
|
|
})
|
|
|
|
.catch(reject => {
|
|
|
|
callback(reject, null);
|
2017-09-27 05:22:40 +00:00
|
|
|
});
|
|
|
|
};
|
2017-09-27 07:25:33 +00:00
|
|
|
|
2017-11-21 10:50:37 +00:00
|
|
|
function getCondition(where, limit, skip) {
|
|
|
|
let out = {
|
|
|
|
sql: '',
|
|
|
|
params: []
|
|
|
|
};
|
|
|
|
if (where && where.or) {
|
|
|
|
out.sql = `AND (em.name regexp ? OR em.surname regexp ?)`;
|
|
|
|
where.or.forEach(val => {
|
|
|
|
Object.keys(val).forEach(key => {
|
|
|
|
out.params.push(val[key].regexp);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
out.params.push(parseInt(limit, 10));
|
|
|
|
out.params.push(parseInt(skip, 10));
|
2017-10-18 13:06:38 +00:00
|
|
|
return out;
|
2017-09-28 07:13:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 13:06:38 +00:00
|
|
|
function formatSalesPerson(response) {
|
2017-09-28 07:13:42 +00:00
|
|
|
let results = [];
|
|
|
|
|
2017-10-18 13:06:38 +00:00
|
|
|
response.forEach(person => {
|
2017-09-28 07:13:42 +00:00
|
|
|
results.push({
|
|
|
|
id: person.id,
|
|
|
|
name: `${person.name} ${person.surname}`
|
|
|
|
});
|
|
|
|
});
|
2017-09-27 07:25:33 +00:00
|
|
|
|
2017-09-28 07:13:42 +00:00
|
|
|
return results;
|
2017-09-27 07:25:33 +00:00
|
|
|
}
|
2017-09-27 05:22:40 +00:00
|
|
|
};
|