@@ -144,24 +144,28 @@ async function getGraphDataFromDb(graphIds: string[]): Promise<GraphData[]> {
144144 const placeholders = graphIds . map ( ( _ , i ) => `$${ i + 1 } ` ) . join ( ',' ) ;
145145 const result = await query (
146146 `SELECT g.id, g.name,
147- COUNT(a.id) as argument_count,
148- MAX(a.id) as latest_argument_id
147+ COUNT(DISTINCT a.id) as argument_count,
148+ COUNT(r.id) as reaction_count,
149+ GREATEST(MAX(a.id), MAX(r.id)) as latest_activity
149150 FROM graphs g
150151 LEFT JOIN arguments a ON g.id = a.graph_id
152+ LEFT JOIN reactions r ON a.id = r.argument_id
151153 WHERE g.id IN (${ placeholders } )
152- GROUP BY g.id, g.name` ,
154+ GROUP BY g.id, g.name
155+ ORDER BY GREATEST(MAX(a.id), MAX(r.id)) DESC NULLS FIRST` ,
153156 graphIds
154157 ) ;
155158
156159 if ( result . rows . length === 0 ) {
157160 throw new Error ( 'Graphs not found' ) ;
158161 }
159162
160- return result . rows . map ( row => ( {
163+ return result . rows . map ( ( row ) : GraphData => ( {
161164 id : row . id ,
162165 name : row . name ,
163- argumentCount : parseInt ( row . argument_count ) ,
164- lastActivity : row . latest_argument_id ? getTimestamp ( row . latest_argument_id ) : undefined
166+ argumentCount : parseInt ( row . argument_count , 10 ) ,
167+ reactionCount : parseInt ( row . reaction_count , 10 ) ,
168+ lastActivity : row . latest_activity ? getTimestamp ( row . latest_activity ) : undefined
165169 } ) ) ;
166170}
167171
0 commit comments