@@ -52,22 +52,6 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
5252 // to be able to sort the flows using the entity
5353 const isSortByEntity = orderBy && orderBy . entity !== 'flow' ;
5454 let sortByFlowIDsPromise : Promise < UniqueFlowEntity [ ] > = Promise . resolve ( [ ] ) ;
55- const orderByForFlow = mapFlowOrderBy ( orderBy ) ;
56-
57- // Fetch sorted flow IDs only for the filtered subset instead of the whole table
58- if ( isSortByEntity ) {
59- // Get entity-sorted IDs then intersect with filtered subset
60- sortByFlowIDsPromise = this . flowService . getFlowIDsFromEntity (
61- models ,
62- orderBy
63- ) ;
64- } else {
65- // Let the DB sort only the filtered IDs
66- sortByFlowIDsPromise = this . flowService . getFlows ( {
67- models,
68- orderBy : orderByForFlow ,
69- } ) ;
70- }
7155 // We need to fetch the flowIDs by the nestedFlowFilters
7256 // if there are any
7357 const isFilterByNestedFilters = nestedFlowFilters !== undefined ;
@@ -118,21 +102,6 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
118102 let didFlowsFromObjectFiltersPromiseCreated = false ;
119103 let flowObjectFiltersGrouped : FlowObjectFilterGrouped | null = null ;
120104
121- if ( isFilterByFlowObjects ) {
122- // First step is to map the filters to the FlowObjectFiltersGrouped
123- // To allow doing inclusive filtering between filters of the same type+direction
124- // But exclusive filtering between filters of different type+direction
125- flowObjectFiltersGrouped =
126- mapFlowFiltersToFlowObjectFiltersGrouped ( flowObjectFilters ) ;
127-
128- flowsFromObjectFiltersPromise =
129- this . getFlowIdsFromObjectConditions . search ( {
130- models,
131- flowObjectFilterGrouped : flowObjectFiltersGrouped ,
132- } ) ;
133- didFlowsFromObjectFiltersPromiseCreated = true ;
134- }
135-
136105 // Lastly, we need to check if we need to filter by flow
137106 // And if we didn't did it before when sorting by entity
138107 // if so, we need to obtain the flowIDs from the flowFilters
@@ -163,17 +132,62 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
163132 const [
164133 flowsFromCategoryFilters ,
165134 flowsFromNestedFilters ,
166- flowsFromObjectFilters ,
167135 flowsFromFlowFilters ,
168- sortByFlowIDs ,
169136 ] = await Promise . all ( [
170137 flowsFromCategoryFiltersPromise ,
171138 flowsFromNestedFiltersPromise ,
172- flowsFromObjectFiltersPromise ,
173139 flowsFromFlowFiltersPromise ,
174- sortByFlowIDsPromise ,
175140 ] ) ;
176141
142+ const intersectCandidates = intersectSets (
143+ new Set ( flowsFromCategoryFilters . flows . map ( ( f ) => f . id ) ) ,
144+ new Set ( flowsFromNestedFilters . flows . map ( ( f ) => f . id ) ) ,
145+ new Set ( flowsFromFlowFilters . map ( ( f ) => f . id ) )
146+ ) ;
147+
148+ if ( isFilterByFlowObjects ) {
149+ // First step is to map the filters to the FlowObjectFiltersGrouped
150+ // To allow doing inclusive filtering between filters of the same type+direction
151+ // But exclusive filtering between filters of different type+direction
152+ flowObjectFiltersGrouped =
153+ mapFlowFiltersToFlowObjectFiltersGrouped ( flowObjectFilters ) ;
154+
155+ flowsFromObjectFiltersPromise =
156+ this . getFlowIdsFromObjectConditions . search ( {
157+ models,
158+ flowObjectFilterGrouped : flowObjectFiltersGrouped ,
159+ candidates : intersectCandidates ,
160+ } ) ;
161+ didFlowsFromObjectFiltersPromiseCreated = true ;
162+ }
163+ const flowsFromObjectFilters = await flowsFromObjectFiltersPromise ;
164+
165+ const orderByForFlow = mapFlowOrderBy ( orderBy ) ;
166+
167+ // Fetch sorted flow IDs only for the filtered subset instead of the whole table
168+ if ( isSortByEntity ) {
169+ // Get entity-sorted IDs then intersect with filtered subset
170+ sortByFlowIDsPromise = this . flowService . getFlowIDsFromEntity (
171+ models ,
172+ orderBy
173+ ) ;
174+ } else {
175+ // Let the DB sort only the filtered IDs
176+ sortByFlowIDsPromise = this . flowService . getFlows ( {
177+ models,
178+ conditions : {
179+ id : {
180+ [ models . Op . IN ] : intersectSets (
181+ intersectCandidates ,
182+ new Set ( flowsFromObjectFilters . flows . map ( ( f ) => f . id ) )
183+ ) ,
184+ } ,
185+ } ,
186+ orderBy : orderByForFlow ,
187+ } ) ;
188+ }
189+ const sortByFlowIDs = await sortByFlowIDsPromise ;
190+
177191 // First check if we have created the promises
178192 // and if so, check if the flows are empty
179193 // If they are empty, we can return an empty array
0 commit comments