diff --git a/modules/agency/back/methods/zone-geo/getLeaves.js b/modules/agency/back/methods/zone-geo/getLeaves.js index dc1c1e2f8..bfcef08ae 100644 --- a/modules/agency/back/methods/zone-geo/getLeaves.js +++ b/modules/agency/back/methods/zone-geo/getLeaves.js @@ -31,7 +31,7 @@ module.exports = Self => { } }); - Self.getLeaves = async(zoneFk, parentFk, filter) => { + Self.getLeaves = async(zoneFk, parentFk = null, filter) => { let conn = Self.dataSource.connector; let stmts = []; @@ -78,20 +78,19 @@ module.exports = Self => { ON zg.lft <= ch.lft AND zg.rgt >= ch.rgt UNION ALL SELECT - child.id, - child.name, - child.lft, - child.rgt, - child.depth, - child.sons, + zg.id, + zg.name, + zg.lft, + zg.rgt, + zg.depth, + zg.sons, zi.isIncluded AS selected - FROM zoneGeo parent - JOIN zoneGeo child ON child.lft > parent.lft - AND child.rgt < parent.rgt - AND child.depth = parent.depth + 1 - LEFT JOIN zoneIncluded zi ON zi.geoFk = child.id + FROM zoneGeo zg + LEFT JOIN zoneIncluded zi ON zi.geoFk = zg.id AND zi.zoneFk = ? - WHERE parent.id = ?) AS nst`, [zoneFk, parentFk]); + WHERE (? IS NULL AND zg.parentFk IS NULL) + OR (? IS NOT NULL AND zg.parentFk = ?)) AS nst`, + [zoneFk, parentFk, parentFk, parentFk]); // Get nodes from depth greather than Origin stmt.merge(conn.makeSuffix(filter));