salix/modules/worker/back/methods/department/getLeaves.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-03-12 14:04:09 +00:00
module.exports = Self => {
2023-06-01 06:32:06 +00:00
Self.remoteMethodCtx, ('getLeaves', {
2019-10-01 13:09:55 +00:00
description: 'Returns the nodes for a department',
2019-03-12 14:04:09 +00:00
accepts: [{
2019-10-08 10:48:12 +00:00
arg: 'parentId',
2019-03-12 14:04:09 +00:00
type: 'Number',
2019-10-01 13:09:55 +00:00
description: 'Get the children of the specified father',
}, {
arg: 'search',
type: 'String',
description: 'Filter nodes whose name starts with',
2019-03-12 14:04:09 +00:00
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/getLeaves`,
verb: 'GET'
}
});
2023-06-01 06:32:06 +00:00
Self.getLeaves = async(ctx, parentId = null, search) => {
2019-10-01 13:09:55 +00:00
let [res] = await Self.rawSql(
`CALL department_getLeaves(?, ?)`,
2023-06-01 06:32:06 +00:00
[parentId, search],
{userId: ctx.req.accessToken.userId}
2019-10-01 13:09:55 +00:00
);
2023-06-01 14:03:55 +00:00
2019-10-01 13:09:55 +00:00
let map = new Map();
for (let node of res) {
if (!map.has(node.parentFk))
map.set(node.parentFk, []);
map.get(node.parentFk).push(node);
2019-03-12 14:04:09 +00:00
}
2019-10-01 13:09:55 +00:00
function setLeaves(nodes) {
if (!nodes) return;
for (let node of nodes) {
node.childs = map.get(node.id);
setLeaves(node.childs);
}
2019-03-12 14:04:09 +00:00
}
2019-10-08 10:48:12 +00:00
let leaves = map.get(parentId);
2023-06-01 12:19:50 +00:00
2019-10-01 13:09:55 +00:00
setLeaves(leaves);
return leaves || [];
2019-03-12 14:04:09 +00:00
};
};