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

63 lines
1.9 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('activeSalesPerson', {
description: 'Returns actives workers with salesperson role',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'Object',
required: false,
description: 'Filter defining where and paginated data',
http: {source: 'query'}
}],
returns: {
type: 'Worker',
root: true
},
http: {
path: `/activeSalesPerson`,
verb: 'get'
}
});
Self.activeSalesPerson = async filter => {
let sqlWhere = '';
let sqlLimit = '';
let sqlOffset = '';
let params = [];
if (filter.where) {
if (filter.where.firstName) {
sqlWhere = `AND (worker.firstName LIKE ? OR worker.name LIKE ?)`;
let search = where.firstName.like;
params.push(search);
params.push(search);
}
if (filter.where.id) {
sqlWhere = `AND worker.id = ?`;
params.push(filter.where.id);
}
}
if (filter.limit) {
sqlLimit = `LIMIT ?`;
params.push(filter.limit);
}
if (filter.skip) {
sqlOffset = `OFFSET ?`;
params.push(filter.skip);
}
let query =
`SELECT worker.id, worker.firstName, worker.name
FROM vn.worker
JOIN account.user user ON user.id = worker.userFk
JOIN account.role role ON role.name = 'salesPerson'
JOIN account.roleRole inheritance ON inheritance.role = user.role AND inheritance.inheritsFrom = role.id
WHERE user.active IS TRUE ${sqlWhere}
ORDER BY worker.firstName ASC
${sqlLimit} ${sqlOffset}`;
return await Self.rawSql(query, params);
};
};