@@ -463,7 +463,12 @@ class SegmentRepository extends RepositoryBase<
463463 if ( adminSegments . length === 0 ) {
464464 return { count : 0 , rows : [ ] , limit : criteria . limit , offset : criteria . offset }
465465 }
466- segmentsSearchQuery += `AND sp.id IN (:adminSegments)`
466+ segmentsSearchQuery += `AND EXISTS (
467+ SELECT 1 FROM segments sp
468+ WHERE sp."grandparentSlug" = f.slug
469+ AND sp."tenantId" = f."tenantId"
470+ AND sp.id IN (:adminSegments)
471+ )`
467472 replacements . adminSegments = adminSegments
468473 }
469474
@@ -474,23 +479,7 @@ class SegmentRepository extends RepositoryBase<
474479 SELECT
475480 f.id AS foundation_id,
476481 f.name AS foundation_name,
477- f.status,
478- f."createdAt",
479- f."updatedAt",
480- f."sourceId",
481- f."sourceParentId",
482- f.slug,
483- p.name AS project_name,
484- p.id AS project_id,
485- p.status AS project_status,
486- p.slug AS project_slug,
487- COUNT(DISTINCT sp.id) AS subproject_count,
488- JSONB_AGG(JSONB_BUILD_OBJECT(
489- 'id', sp.id,
490- 'name', sp.name,
491- 'status', sp.status,
492- 'slug', sp.slug
493- )) AS subprojects
482+ COUNT(DISTINCT p.id)::int AS project_count
494483 FROM segments f
495484 JOIN segments p
496485 ON p."parentSlug" = f."slug"
@@ -503,22 +492,15 @@ class SegmentRepository extends RepositoryBase<
503492 WHERE f."parentSlug" IS NULL
504493 AND f."tenantId" = :tenantId
505494 ${ segmentsSearchQuery }
506- GROUP BY f."id", p. id
495+ GROUP BY f.id
507496 )
508497 SELECT
509498 s.*,
510499 COUNT(*) OVER () AS "totalCount",
511- JSONB_AGG(JSONB_BUILD_OBJECT(
512- 'id', f.project_id,
513- 'name', f.project_name,
514- 'status', f.project_status,
515- 'slug', f.project_slug,
516- 'subprojects', f.subprojects
517- )) AS projects
500+ f.project_count AS "projectCount"
518501 FROM segments s
519502 JOIN foundations f ON s.id = f.foundation_id
520503 ${ searchQuery }
521- GROUP BY s.id, f.foundation_name
522504 ORDER BY f.foundation_name
523505 ${ this . getPaginationString ( criteria ) } ;
524506 ` ,
0 commit comments