2019-01-21 10:45:53 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('getLeaves', {
|
2019-09-19 13:51:39 +00:00
|
|
|
description: 'Returns the nodes for a zone',
|
|
|
|
accepts: [
|
|
|
|
{
|
2019-09-19 15:49:46 +00:00
|
|
|
arg: 'id',
|
2021-06-18 13:05:03 +00:00
|
|
|
type: 'number',
|
2019-10-08 05:22:38 +00:00
|
|
|
description: 'The zone id',
|
2019-09-19 15:49:46 +00:00
|
|
|
http: {source: 'path'},
|
|
|
|
required: true
|
2021-06-18 13:05:03 +00:00
|
|
|
},
|
|
|
|
{
|
2019-10-08 05:22:38 +00:00
|
|
|
arg: 'parentId',
|
2021-06-18 13:05:03 +00:00
|
|
|
type: 'number',
|
2019-09-19 13:51:39 +00:00
|
|
|
description: 'Get the children of the specified father',
|
2021-06-18 13:05:03 +00:00
|
|
|
},
|
|
|
|
{
|
2019-09-19 13:51:39 +00:00
|
|
|
arg: 'search',
|
2021-06-18 13:05:03 +00:00
|
|
|
type: 'string',
|
2019-09-19 13:51:39 +00:00
|
|
|
description: 'Filter nodes whose name starts with',
|
|
|
|
}
|
|
|
|
],
|
2019-01-21 10:45:53 +00:00
|
|
|
returns: {
|
2021-06-18 13:05:03 +00:00
|
|
|
type: ['object'],
|
2019-01-21 10:45:53 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2019-09-19 15:49:46 +00:00
|
|
|
path: `/:id/getLeaves`,
|
2019-01-21 10:45:53 +00:00
|
|
|
verb: 'GET'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-18 13:05:03 +00:00
|
|
|
Self.getLeaves = async(id, parentId = null, search, options) => {
|
2021-11-18 10:17:30 +00:00
|
|
|
const myOptions = {};
|
2021-06-18 13:05:03 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
const [res] = await Self.rawSql(
|
2019-09-19 15:30:16 +00:00
|
|
|
`CALL zone_getLeaves(?, ?, ?)`,
|
2021-06-18 13:05:03 +00:00
|
|
|
[id, parentId, search],
|
|
|
|
myOptions
|
2019-09-19 13:51:39 +00:00
|
|
|
);
|
|
|
|
|
2021-06-18 13:05:03 +00:00
|
|
|
const map = new Map();
|
2019-09-19 13:51:39 +00:00
|
|
|
for (let node of res) {
|
|
|
|
if (!map.has(node.parentFk))
|
|
|
|
map.set(node.parentFk, []);
|
|
|
|
map.get(node.parentFk).push(node);
|
2019-02-18 07:37:26 +00:00
|
|
|
}
|
2019-01-21 10:45:53 +00:00
|
|
|
|
2019-09-19 13:51:39 +00:00
|
|
|
function setLeaves(nodes) {
|
|
|
|
if (!nodes) return;
|
|
|
|
for (let node of nodes) {
|
|
|
|
node.childs = map.get(node.id);
|
|
|
|
setLeaves(node.childs);
|
|
|
|
}
|
2019-02-18 07:37:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 13:05:03 +00:00
|
|
|
const leaves = map.get(parentId);
|
2022-10-10 08:25:59 +00:00
|
|
|
|
2022-10-10 08:33:08 +00:00
|
|
|
const maxNodes = 250;
|
2022-10-10 08:25:59 +00:00
|
|
|
if (res.length <= maxNodes)
|
|
|
|
setLeaves(leaves);
|
2019-01-21 10:45:53 +00:00
|
|
|
|
2019-09-19 15:30:16 +00:00
|
|
|
return leaves || [];
|
2019-01-21 10:45:53 +00:00
|
|
|
};
|
|
|
|
};
|