salix/modules/agency/back/methods/zone/getLeaves.js

58 lines
1.5 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('getLeaves', {
description: 'Returns the nodes for a zone',
accepts: [
{
arg: 'id',
type: 'Number',
http: {source: 'path'},
required: true
}, {
arg: 'parentFk',
type: 'Number',
description: 'Get the children of the specified father',
}, {
arg: 'search',
type: 'String',
description: 'Filter nodes whose name starts with',
}
],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/:id/getLeaves`,
verb: 'GET'
}
});
Self.getLeaves = async(id, parentFk = null, search) => {
let [res] = await Self.rawSql(
`CALL zone_getLeaves(?, ?, ?)`,
[id, parentFk, search]
);
let map = new Map();
for (let node of res) {
if (!map.has(node.parentFk))
map.set(node.parentFk, []);
map.get(node.parentFk).push(node);
}
function setLeaves(nodes) {
if (!nodes) return;
for (let node of nodes) {
node.childs = map.get(node.id);
setLeaves(node.childs);
}
}
let leaves = map.get(parentFk);
setLeaves(leaves);
return leaves || [];
};
};