43 lines
1.5 KiB
JavaScript
43 lines
1.5 KiB
JavaScript
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
const buildFilter = require('vn-loopback/util/filter').buildFilter;
|
|
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
|
|
|
|
module.exports = Self => {
|
|
Self.activeWorkers = async(query, filter) => {
|
|
let conn = Self.dataSource.connector;
|
|
if (filter.where && filter.where.and && Array.isArray(filter.where.and)) {
|
|
let where = {};
|
|
filter.where.and.forEach(element => {
|
|
where[Object.keys(element)[0]] = Object.values(element)[0];
|
|
});
|
|
filter.where = where;
|
|
}
|
|
let clientFilter = Object.assign({}, filter);
|
|
clientFilter.where = buildFilter(filter.where, (param, value) => {
|
|
switch (param) {
|
|
case 'role':
|
|
return {'r.name': value};
|
|
case 'firstName':
|
|
return {or: [
|
|
{'w.firstName': {like: `%${value}%`}},
|
|
{'w.lastName': {like: `%${value}%`}},
|
|
{'u.name': {like: `%${value}%`}},
|
|
{'u.nickname': {like: `%${value}%`}}
|
|
]};
|
|
case 'id':
|
|
return {'w.id': value};
|
|
}
|
|
});
|
|
|
|
let myFilter = {
|
|
where: {'u.active': true}
|
|
};
|
|
|
|
myFilter = mergeFilters(myFilter, clientFilter);
|
|
|
|
let stmt = new ParameterizedSQL(query);
|
|
stmt.merge(conn.makeSuffix(myFilter));
|
|
return conn.executeStmt(stmt);
|
|
};
|
|
};
|