@@ -62,12 +62,18 @@ export async function GET(request: NextRequest) {
6262 }
6363 }
6464
65- let workflows
65+ const pageParam = url . searchParams . get ( 'page' )
66+ const limitParam = url . searchParams . get ( 'limit' )
67+ const page = pageParam ? parseInt ( pageParam , 10 ) : undefined
68+ const limit = limitParam ? parseInt ( limitParam , 10 ) : undefined
6669
70+ let workflows
6771 const orderByClause = [ asc ( workflow . sortOrder ) , asc ( workflow . createdAt ) , asc ( workflow . id ) ]
6872
73+ let baseQuery : any
74+
6975 if ( workspaceId ) {
70- workflows = await db
76+ baseQuery = db
7177 . select ( )
7278 . from ( workflow )
7379 . where ( eq ( workflow . workspaceId , workspaceId ) )
@@ -77,17 +83,29 @@ export async function GET(request: NextRequest) {
7783 . select ( { workspaceId : permissions . entityId } )
7884 . from ( permissions )
7985 . where ( and ( eq ( permissions . userId , userId ) , eq ( permissions . entityType , 'workspace' ) ) )
86+
8087 const workspaceIds = workspacePermissionRows . map ( ( row ) => row . workspaceId )
88+
8189 if ( workspaceIds . length === 0 ) {
8290 return NextResponse . json ( { data : [ ] } , { status : 200 } )
8391 }
84- workflows = await db
92+
93+ baseQuery = db
8594 . select ( )
8695 . from ( workflow )
8796 . where ( inArray ( workflow . workspaceId , workspaceIds ) )
8897 . orderBy ( ...orderByClause )
8998 }
9099
100+ if ( limit && ! isNaN ( limit ) && limit > 0 ) {
101+ baseQuery = baseQuery . limit ( limit )
102+ if ( page && ! isNaN ( page ) && page > 1 ) {
103+ baseQuery = baseQuery . offset ( ( page - 1 ) * limit )
104+ }
105+ }
106+
107+ workflows = await baseQuery
108+
91109 return NextResponse . json ( { data : workflows } , { status : 200 } )
92110 } catch ( error : any ) {
93111 const elapsed = Date . now ( ) - startTime
0 commit comments