@@ -463,7 +463,11 @@ 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.id IN (:adminSegments)
470+ )`
467471 replacements . adminSegments = adminSegments
468472 }
469473
@@ -474,51 +478,24 @@ class SegmentRepository extends RepositoryBase<
474478 SELECT
475479 f.id AS foundation_id,
476480 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
481+ COUNT(DISTINCT p.id)::int AS project_count
494482 FROM segments f
495- JOIN segments p
483+ LEFT JOIN segments p
496484 ON p."parentSlug" = f."slug"
497485 AND p."grandparentSlug" IS NULL
498486 AND p."tenantId" = f."tenantId"
499- JOIN segments sp
500- ON sp."parentSlug" = p."slug"
501- AND sp."grandparentSlug" = f.slug
502- AND sp."tenantId" = f."tenantId"
503487 WHERE f."parentSlug" IS NULL
504488 AND f."tenantId" = :tenantId
505489 ${ segmentsSearchQuery }
506- GROUP BY f."id", p. id
490+ GROUP BY f.id
507491 )
508492 SELECT
509493 s.*,
510494 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
495+ f.project_count AS "projectCount"
518496 FROM segments s
519497 JOIN foundations f ON s.id = f.foundation_id
520498 ${ searchQuery }
521- GROUP BY s.id, f.foundation_name
522499 ORDER BY f.foundation_name
523500 ${ this . getPaginationString ( criteria ) } ;
524501 ` ,
0 commit comments