dev #1731
|
@ -0,0 +1,65 @@
|
|||
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.remoteMethod('getItemTypeWorker', {
|
||||
description: 'Returns the workers that appear in itemType',
|
||||
accessType: 'READ',
|
||||
accepts: [{
|
||||
arg: 'filter',
|
||||
type: 'Object',
|
||||
description: 'Filter defining where and paginated data',
|
||||
required: true
|
||||
}],
|
||||
returns: {
|
||||
type: ['object'],
|
||||
root: true
|
||||
},
|
||||
http: {
|
||||
path: `/getItemTypeWorker`,
|
||||
verb: 'GET'
|
||||
}
|
||||
});
|
||||
|
||||
Self.getItemTypeWorker = async(filter, options) => {
|
||||
const myOptions = {};
|
||||
const conn = Self.dataSource.connector;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
const query =
|
||||
`SELECT DISTINCT u.id, w.firstName, w.lastName, u.nickname, u.name
|
||||
FROM itemType it
|
||||
JOIN worker w ON w.id = it.workerFk
|
||||
JOIN account.user u ON u.id = w.id`;
|
||||
|
||||
let stmt = new ParameterizedSQL(query);
|
||||
|
||||
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);
|
||||
stmt.merge(conn.makeSuffix(myFilter));
|
||||
return conn.executeStmt(stmt);
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue