@@ -79,26 +79,95 @@ export const createStream = async (req: Request, res: Response) => {
7979} ;
8080
8181/**
82- * List streams by sender or recipient
82+ * List streams by sender, recipient, status, token with sorting and pagination
8383 */
8484export const listStreams = async ( req : Request , res : Response ) => {
8585 try {
86- const { sender, recipient } = req . query ;
86+ const {
87+ sender,
88+ recipient,
89+ status,
90+ token,
91+ sort = 'createdAt' ,
92+ order = 'desc' ,
93+ limit = '20' ,
94+ offset = '0'
95+ } = req . query ;
8796
8897 const where : any = { } ;
8998 if ( typeof sender === 'string' ) where . sender = sender ;
9099 if ( typeof recipient === 'string' ) where . recipient = recipient ;
100+ if ( typeof token === 'string' ) where . tokenAddress = token ;
91101
92- const streams = await prisma . stream . findMany ( {
93- where,
94- orderBy : { createdAt : 'desc' } ,
95- include : {
96- senderUser : true ,
97- recipientUser : true
102+ // Handle status filtering
103+ if ( typeof status === 'string' ) {
104+ const validStatuses = [ 'active' , 'cancelled' , 'completed' , 'paused' ] ;
105+ if ( ! validStatuses . includes ( status ) ) {
106+ return res . status ( 400 ) . json ( {
107+ error : 'Invalid status parameter' ,
108+ message : `status must be one of: ${ validStatuses . join ( ', ' ) } `
109+ } ) ;
98110 }
99- } ) ;
100111
101- return res . status ( 200 ) . json ( streams ) ;
112+ // Map status to database conditions
113+ switch ( status ) {
114+ case 'active' :
115+ where . isActive = true ;
116+ break ;
117+ case 'cancelled' :
118+ where . isActive = false ;
119+ // Additional check for cancelled events could be added here
120+ break ;
121+ case 'completed' :
122+ where . isActive = false ;
123+ // Additional check for completed events could be added here
124+ break ;
125+ case 'paused' :
126+ where . isActive = false ;
127+ // Additional check for paused events could be added here
128+ break ;
129+ }
130+ }
131+
132+ // Validate and parse pagination parameters
133+ const parsedLimit = Math . min (
134+ typeof limit === 'string' ? ( Number . parseInt ( limit , 10 ) || 20 ) : 20 ,
135+ 100
136+ ) ;
137+ const parsedOffset = typeof offset === 'string' ? ( Number . parseInt ( offset , 10 ) || 0 ) : 0 ;
138+
139+ // Validate sort field
140+ const validSortFields = [ 'createdAt' , 'startTime' , 'lastUpdateTime' , 'depositedAmount' ] ;
141+ const sortField = validSortFields . includes ( typeof sort === 'string' ? sort : 'createdAt' )
142+ ? ( sort as 'createdAt' | 'startTime' | 'lastUpdateTime' | 'depositedAmount' )
143+ : 'createdAt' ;
144+
145+ // Validate order
146+ const sortOrder = order === 'asc' ? 'asc' : 'desc' ;
147+
148+ const [ streams , total ] = await Promise . all ( [
149+ prisma . stream . findMany ( {
150+ where,
151+ orderBy : { [ sortField ] : sortOrder } ,
152+ take : parsedLimit ,
153+ skip : parsedOffset ,
154+ include : {
155+ senderUser : true ,
156+ recipientUser : true
157+ }
158+ } ) ,
159+ prisma . stream . count ( { where } )
160+ ] ) ;
161+
162+ const hasMore = parsedOffset + streams . length < total ;
163+
164+ return res . status ( 200 ) . json ( {
165+ data : streams ,
166+ total,
167+ hasMore,
168+ limit : parsedLimit ,
169+ offset : parsedOffset
170+ } ) ;
102171 } catch ( error ) {
103172 logger . error ( 'Error listing streams:' , error ) ;
104173 return res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
@@ -302,7 +371,7 @@ export const getStreamClaimableAmount = async (req: Request, res: Response) => {
302371 */
303372export const getUserStreamSummary = async ( req : Request , res : Response ) => {
304373 try {
305- const address = ( req . params . address ?? '' ) . trim ( ) ;
374+ const address = Array . isArray ( req . params . address ) ? req . params . address [ 0 ] : ( req . params . address ?? '' ) . trim ( ) ;
306375 if ( ! address ) {
307376 return res . status ( 400 ) . json ( { error : 'Address is required' } ) ;
308377 }
@@ -339,10 +408,10 @@ export const getUserStreamSummary = async (req: Request, res: Response) => {
339408 ] ) ;
340409
341410 const totalStreamsCreated = outgoingStreams . length ;
342- const totalStreamedOut = sumStringI128 ( outgoingStreams . map ( ( stream ) => stream . withdrawnAmount ) ) ;
343- const totalStreamedIn = sumStringI128 ( incomingStreams . map ( ( stream ) => stream . withdrawnAmount ) ) ;
344- const activeOutgoingCount = outgoingStreams . filter ( ( stream ) => stream . isActive ) . length ;
345- const activeIncomingCount = incomingStreams . filter ( ( stream ) => stream . isActive ) . length ;
411+ const totalStreamedOut = sumStringI128 ( outgoingStreams . map ( ( stream : any ) => stream . withdrawnAmount ) ) ;
412+ const totalStreamedIn = sumStringI128 ( incomingStreams . map ( ( stream : any ) => stream . withdrawnAmount ) ) ;
413+ const activeOutgoingCount = outgoingStreams . filter ( ( stream : any ) => stream . isActive ) . length ;
414+ const activeIncomingCount = incomingStreams . filter ( ( stream : any ) => stream . isActive ) . length ;
346415
347416 const calculatedAt = Math . floor ( nowMs / 1000 ) ;
348417 let claimableTotal = 0n ;
0 commit comments