11import paginate from '@/helpers/pagination'
2+ import Flow from '@/models/flow'
23
34import type { QueryResolvers } from '../__generated__/types.generated'
45
@@ -7,31 +8,55 @@ const getFlows: QueryResolvers['getFlows'] = async (
78 params ,
89 context ,
910) => {
10- const flowsQuery = context . currentUser
11- . $relatedQuery ( 'flows' )
12- . joinRelated ( {
13- steps : true ,
11+ const filteredFlowIds = (
12+ await context . currentUser
13+ . $relatedQuery ( 'flows' )
14+ . distinct ( 'id' )
15+ . where ( ( builder ) => {
16+ if ( params . name ) {
17+ builder . where ( 'name' , 'ilike' , `%${ params . name } %` )
18+ }
19+ } )
20+ ) . map ( ( f ) => f . id )
21+
22+ if ( ! filteredFlowIds . length ) {
23+ return {
24+ pageInfo : {
25+ currentPage : 1 ,
26+ totalCount : 0 ,
27+ } ,
28+ edges : [ ] ,
29+ }
30+ }
31+
32+ const flowsQuery = Flow . query ( )
33+ . with ( 'filtered_steps' , ( builder ) => {
34+ builder
35+ . distinct ( 'flow_id' )
36+ . from ( 'steps' )
37+ . where ( ( stepBuilder ) => {
38+ if ( params . connectionId ) {
39+ stepBuilder . where ( 'connection_id' , params . connectionId )
40+ }
41+
42+ if ( params . appKey ) {
43+ stepBuilder . where ( 'app_key' , params . appKey )
44+ }
45+
46+ stepBuilder . withSoftDeleted ( )
47+ } )
48+ . whereNull ( 'deleted_at' )
49+ . whereIn ( 'flow_id' , filteredFlowIds )
50+ . withSoftDeleted ( )
1451 } )
52+ . innerJoin ( 'filtered_steps' , 'id' , 'filtered_steps.flow_id' )
1553 . withGraphFetched ( {
1654 steps : {
1755 connection : true ,
1856 } ,
1957 pendingTransfer : true ,
2058 } )
21- . where ( ( builder ) => {
22- if ( params . connectionId ) {
23- builder . where ( 'steps.connection_id' , params . connectionId )
24- }
25-
26- if ( params . name ) {
27- builder . where ( 'flows.name' , 'ilike' , `%${ params . name } %` )
28- }
29-
30- if ( params . appKey ) {
31- builder . where ( 'steps.app_key' , params . appKey )
32- }
33- } )
34- . groupBy ( 'flows.id' )
59+ . groupBy ( 'id' )
3560 . orderBy ( 'active' , 'desc' )
3661 . orderBy ( 'updated_at' , 'desc' )
3762
0 commit comments