salix/services/loopback/common/methods/client/activeSalesPerson.js

62 lines
1.8 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethod('activeSalesPerson', {
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: {
type: 'Worker',
2017-09-27 05:22:40 +00:00
root: true
},
http: {
path: `/activeSalesPerson`,
verb: 'get'
}
});
Self.activeSalesPerson = async filter => {
let where = filter.where;
let sqlWhere = '';
let sqlLimit = '';
let sqlOffset = '';
let params = [];
if (where) {
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;
params.push(search);
params.push(search);
}
if (where.id) {
sqlWhere = `AND em.id = ?`;
params.push(where.id);
}
}
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
let query =
`SELECT em.id, em.firstName, em.name
FROM worker em
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
return await Self.rawSql(query, params);
2017-09-27 05:22:40 +00:00
};
2017-11-21 10:59:43 +00:00
};