@@ -84,16 +84,18 @@ const parseOrderBy = (
8484 orderBy : string | undefined ,
8585 fallbackDirection : OrderDirection ,
8686) : { field ?: string ; direction : OrderDirection } => {
87+ const defaultDirection : OrderDirection = fallbackDirection || 'DESC'
88+
8789 if ( ! orderBy || ! orderBy . trim ( ) ) {
88- return { field : undefined , direction : fallbackDirection }
90+ return { field : undefined , direction : defaultDirection }
8991 }
9092
9193 const [ rawField , rawDir ] = orderBy . trim ( ) . split ( '_' )
9294 const field = rawField ?. trim ( ) || undefined
9395
9496 const dir = ( rawDir || '' ) . toUpperCase ( )
9597 const direction : OrderDirection =
96- dir === 'ASC' || dir === 'DESC' ? ( dir as OrderDirection ) : fallbackDirection
98+ dir === 'ASC' || dir === 'DESC' ? ( dir as OrderDirection ) : defaultDirection
9799
98100 return { field, direction }
99101}
@@ -368,14 +370,17 @@ export const buildQuery = ({
368370 limit = 20 ,
369371 offset = 0 ,
370372} : BuildQueryArgs ) : string => {
371- const fallbackDir : OrderDirection = orderDirection || 'DESC'
372- const { field : sortField , direction } = parseOrderBy ( orderBy , fallbackDir )
373+ const { field : sortField , direction } = parseOrderBy ( orderBy , orderDirection )
373374
374375 // Detect alias usage in filters (to decide joins/CTEs needed outside)
375376 const filterHasMo = filterString . includes ( 'mo.' )
376377 const filterHasMe = filterString . includes ( 'me.' )
377378 const filterHasNonIdMemberFields = hasNonIdMemberFieldReferences ( filterString )
378379
380+ log . info (
381+ `filterHasMo=${ filterHasMo } , filterHasMe=${ filterHasMe } , filterHasNonIdMemberFields=${ filterHasNonIdMemberFields } ` ,
382+ )
383+
379384 const needsMemberOrgs = includeMemberOrgs || filterHasMo
380385
381386 // If filters pin m.id to a single value or a small IN-list, skip top-N entirely.
@@ -405,9 +410,7 @@ export const buildQuery = ({
405410 withAggregates,
406411 } )
407412
408- log . info (
409- `useActivityCountOptimized=${ useActivityCountOptimized } , filterHasMe=${ filterHasMe } , filterHasMo=${ filterHasMo } , filterHasNonIdMemberFields=${ filterHasNonIdMemberFields } ` ,
410- )
413+ log . info ( `useActivityCountOptimized=${ useActivityCountOptimized } ` )
411414
412415 if ( useActivityCountOptimized ) {
413416 return buildActivityCountOptimizedQuery ( {
@@ -421,6 +424,7 @@ export const buildQuery = ({
421424 } )
422425 }
423426
427+ log . info ( 'Using fallback query path' )
424428 // Fallback path (other sorts / non-aggregate / filtered queries)
425429 const baseCtes = [ needsMemberOrgs ? buildMemberOrgsCTE ( true ) : '' , searchConfig . cte ] . filter (
426430 Boolean ,
0 commit comments