refs #5244 feat: añadido 'depatments' como binding
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2023-06-21 14:07:36 +02:00
parent dd75302bbb
commit a699e2d874
3 changed files with 59 additions and 26 deletions

View File

@ -4,25 +4,35 @@ import Autocomplete from '../autocomplete';
export default class WorkerAutocomplete extends Autocomplete {
constructor(...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, {
label: 'Worker',
url: 'Workers/search',
fields: ['id', 'name', 'nickname'],
url: url,
searchFunction: function({$search}) {
return {and: [
{active: {neq: false}},
{'u.active': {neq: false}},
{or: [
{name: $search},
{nickname: {like: '%' + $search + '%'}},
{code: {like: $search + '%'}}
{'u.name': $search},
{'u.nickname': {like: '%' + $search + '%'}},
{'w.code': {like: $search + '%'}}
]}
]};
}
},
});
}
}
ngModule.vnComponent('vnWorkerAutocomplete', {
slotTemplate: require('./index.html'),
controller: WorkerAutocomplete
controller: WorkerAutocomplete,
bindings: {
departments: '<?'
},
});

View File

@ -22,7 +22,7 @@
<vn-worker-autocomplete
vn-one
ng-model="$ctrl.workerFk"
where="{role: 'employee'}">
departments="['VN']">
</vn-worker-autocomplete>
<vn-autocomplete
vn-one

View File

@ -1,11 +1,19 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => {
Self.remoteMethod('search', {
Self.remoteMethodCtx('search', {
description: 'Returns an array of search from an specified worker',
accepts: [{
arg: 'filter',
type: 'Object',
type: 'object',
description: 'Filter defining where and paginated data',
required: true
},
{
arg: 'departmentCodes',
type: ['string'],
description: 'Department codes to search workers',
}],
returns: {
type: ['object'],
@ -17,24 +25,39 @@ module.exports = Self => {
}
});
Self.search = async filter => {
const $ = Self.app.models;
Self.search = async(ctx, filter, departmentCodes) => {
const models = Self.app.models;
const conn = Self.dataSource.connector;
let {where} = filter;
const users = where && await $.VnUser.find({
fields: ['id'],
where
});
let allDepartments = [];
if (departmentCodes) {
const departments = await models.Department.find({
fields: ['id'],
where: {code: {inq: departmentCodes}}
});
const departmentIds = departments.map(department => department.id);
const workers = await Self.find({
fields: ['id'],
where: {
businessFk: {neq: null},
id: users ? {inq: users.map(u => u.id)} : undefined
let departmentLeaves = [];
for (let id of departmentIds) {
const leaves = await models.Department.getLeaves(ctx, id, null);
console.log(leaves);
for (let leave of leaves) departmentLeaves.push(leave.id);
}
});
where = {id: {inq: workers.map(w => w.id)}};
return await $.VnUser.find(Object.assign({}, filter, {where}));
allDepartments = departmentIds.concat(departmentLeaves);
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);
};
};