2018-04-03 17:29:01 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('activeSalesPerson', {
|
2018-01-29 11:37:54 +00:00
|
|
|
description: 'Returns actives workers with salesperson role',
|
2017-09-27 05:22:40 +00:00
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'Object',
|
|
|
|
required: false,
|
|
|
|
description: 'Filter defining where and paginated data',
|
|
|
|
http: {source: 'query'}
|
|
|
|
}],
|
|
|
|
returns: {
|
2018-01-29 11:37:54 +00:00
|
|
|
type: 'Worker',
|
2017-09-27 05:22:40 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/activeSalesPerson`,
|
|
|
|
verb: 'get'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-03 17:29:01 +00:00
|
|
|
Self.activeSalesPerson = async filter => {
|
|
|
|
let where = filter.where;
|
|
|
|
let sqlWhere = '';
|
|
|
|
let sqlLimit = '';
|
|
|
|
let sqlOffset = '';
|
|
|
|
let params = [];
|
|
|
|
|
|
|
|
if (where) {
|
2018-04-03 17:37:37 +00:00
|
|
|
if (where.firstName) {
|
2018-06-20 07:53:13 +00:00
|
|
|
sqlWhere = `AND (em.firstName LIKE ? OR em.name LIKE ?)`;
|
|
|
|
let search = where.firstName.like;
|
2018-04-03 17:37:37 +00:00
|
|
|
params.push(search);
|
|
|
|
params.push(search);
|
|
|
|
}
|
|
|
|
if (where.id) {
|
|
|
|
sqlWhere = `AND em.id = ?`;
|
|
|
|
params.push(where.id);
|
|
|
|
}
|
2018-04-03 17:29:01 +00:00
|
|
|
}
|
|
|
|
if (filter.limit) {
|
|
|
|
sqlLimit = `LIMIT ?`;
|
|
|
|
params.push(filter.limit);
|
|
|
|
}
|
|
|
|
if (filter.skip) {
|
|
|
|
sqlOffset = `OFFSET ?`;
|
|
|
|
params.push(filter.skip);
|
|
|
|
}
|
2017-09-27 05:22:40 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
let query =
|
|
|
|
`SELECT em.id, em.firstName, em.name
|
|
|
|
FROM worker em
|
2018-04-03 17:29:01 +00:00
|
|
|
JOIN account.user ac ON em.userFk = ac.id
|
|
|
|
JOIN account.role r ON r.id = ac.role
|
|
|
|
WHERE ac.active ${sqlWhere}
|
|
|
|
ORDER BY em.name ASC
|
|
|
|
${sqlLimit} ${sqlOffset}`;
|
2017-10-18 13:06:38 +00:00
|
|
|
|
2018-04-03 17:29:01 +00:00
|
|
|
return await Self.rawSql(query, params);
|
2017-09-27 05:22:40 +00:00
|
|
|
};
|
2017-11-21 10:59:43 +00:00
|
|
|
};
|