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

63 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-11-12 10:17:47 +00:00
module.exports = Self => {
Self.remoteMethod('activeBuyer', {
description: 'Returns actives workers with Buyer 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: `/activeBuyer`,
verb: 'get'
}
});
Self.activeBuyer = async filter => {
2018-11-13 10:26:36 +00:00
let sqlWhere = '';
2018-11-12 10:17:47 +00:00
let sqlLimit = '';
let sqlOffset = '';
let params = [];
2018-11-13 10:26:36 +00:00
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);
}
}
2018-11-12 10:17:47 +00:00
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
2018-11-13 10:26:36 +00:00
JOIN account.role role ON role.name = 'buyer'
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
2018-11-12 10:17:47 +00:00
${sqlLimit} ${sqlOffset}`;
return await Self.rawSql(query, params);
};
};