5244-component_workerAutocomplete #1679
|
@ -4,25 +4,35 @@ import Autocomplete from '../autocomplete';
|
||||||
export default class WorkerAutocomplete extends Autocomplete {
|
export default class WorkerAutocomplete extends Autocomplete {
|
||||||
constructor(...args) {
|
constructor(...args) {
|
||||||
super(...args);
|
super(...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
$onInit() {
|
||||||
|
let url = 'Workers/search';
|
||||||
|
if (this.departments) {
|
||||||
|
const parameter = encodeURIComponent(JSON.stringify(this.departments));
|
||||||
|
url = `Workers/search?departmentCodes=${parameter}`;
|
||||||
|
}
|
||||||
Object.assign(this, {
|
Object.assign(this, {
|
||||||
label: 'Worker',
|
label: 'Worker',
|
||||||
url: 'Workers/search',
|
url: url,
|
||||||
fields: ['id', 'name', 'nickname'],
|
|
||||||
searchFunction: function({$search}) {
|
searchFunction: function({$search}) {
|
||||||
return {and: [
|
return {and: [
|
||||||
{active: {neq: false}},
|
{'u.active': {neq: false}},
|
||||||
{or: [
|
{or: [
|
||||||
{name: $search},
|
{'u.name': $search},
|
||||||
{nickname: {like: '%' + $search + '%'}},
|
{'u.nickname': {like: '%' + $search + '%'}},
|
||||||
{code: {like: $search + '%'}}
|
{'w.code': {like: $search + '%'}}
|
||||||
]}
|
]}
|
||||||
]};
|
]};
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ngModule.vnComponent('vnWorkerAutocomplete', {
|
ngModule.vnComponent('vnWorkerAutocomplete', {
|
||||||
slotTemplate: require('./index.html'),
|
slotTemplate: require('./index.html'),
|
||||||
controller: WorkerAutocomplete
|
controller: WorkerAutocomplete,
|
||||||
|
bindings: {
|
||||||
|
departments: '<?'
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
<vn-worker-autocomplete
|
<vn-worker-autocomplete
|
||||||
vn-one
|
vn-one
|
||||||
ng-model="$ctrl.workerFk"
|
ng-model="$ctrl.workerFk"
|
||||||
where="{role: 'employee'}">
|
departments="['VN']">
|
||||||
</vn-worker-autocomplete>
|
</vn-worker-autocomplete>
|
||||||
<vn-autocomplete
|
<vn-autocomplete
|
||||||
vn-one
|
vn-one
|
||||||
|
|
|
@ -1,11 +1,19 @@
|
||||||
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
||||||
|
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('search', {
|
Self.remoteMethodCtx('search', {
|
||||||
description: 'Returns an array of search from an specified worker',
|
description: 'Returns an array of search from an specified worker',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
arg: 'filter',
|
arg: 'filter',
|
||||||
type: 'Object',
|
type: 'object',
|
||||||
description: 'Filter defining where and paginated data',
|
description: 'Filter defining where and paginated data',
|
||||||
required: true
|
required: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arg: 'departmentCodes',
|
||||||
|
type: ['string'],
|
||||||
|
description: 'Department codes to search workers',
|
||||||
}],
|
}],
|
||||||
returns: {
|
returns: {
|
||||||
type: ['object'],
|
type: ['object'],
|
||||||
|
@ -17,24 +25,39 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.search = async filter => {
|
Self.search = async(ctx, filter, departmentCodes) => {
|
||||||
const $ = Self.app.models;
|
const models = Self.app.models;
|
||||||
|
const conn = Self.dataSource.connector;
|
||||||
|
|
||||||
let {where} = filter;
|
let allDepartments = [];
|
||||||
const users = where && await $.VnUser.find({
|
if (departmentCodes) {
|
||||||
fields: ['id'],
|
const departments = await models.Department.find({
|
||||||
where
|
fields: ['id'],
|
||||||
});
|
where: {code: {inq: departmentCodes}}
|
||||||
|
});
|
||||||
|
const departmentIds = departments.map(department => department.id);
|
||||||
|
|
||||||
const workers = await Self.find({
|
let departmentLeaves = [];
|
||||||
fields: ['id'],
|
for (let id of departmentIds) {
|
||||||
where: {
|
const leaves = await models.Department.getLeaves(ctx, id, null);
|
||||||
businessFk: {neq: null},
|
console.log(leaves);
|
||||||
id: users ? {inq: users.map(u => u.id)} : undefined
|
for (let leave of leaves) departmentLeaves.push(leave.id);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
where = {id: {inq: workers.map(w => w.id)}};
|
allDepartments = departmentIds.concat(departmentLeaves);
|
||||||
return await $.VnUser.find(Object.assign({}, filter, {where}));
|
console.log(allDepartments);
|
||||||
|
const where = {'d.id': {inq: allDepartments}};
|
||||||
|
filter = mergeFilters(filter, {where});
|
||||||
|
}
|
||||||
|
|
||||||
|
const stmt = new ParameterizedSQL(
|
||||||
|
`SELECT DISTINCT w.code, u.name, u.nickname
|
||||||
|
FROM worker w
|
||||||
|
JOIN account.user u ON u.id = w.id
|
||||||
|
JOIN business b ON b.workerFk = w.id
|
||||||
|
JOIN department d ON d.id = b.departmentFk`);
|
||||||
|
|
||||||
|
stmt.merge(conn.makeSuffix(filter));
|
||||||
|
return conn.executeStmt(stmt);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue