@@ -90,14 +90,33 @@ export const getUserContest = async (req, res) => {
9090export const getUserContestHistory = async ( req , res ) => {
9191 try {
9292 const username = validateUsername ( req . params . username ) ;
93+ const attendedOnly = req . params . attendedOnly === 'true' || req . query . attendedOnly === 'true' ;
9394
9495 const data = await client . graphqlQuery ( GRAPHQL_QUERIES . USER_CONTEST_INFO , { username } ) ;
9596
9697 if ( ! data . userContestRankingHistory ) {
9798 return handleError ( res , new Error ( 'Contest history not found for user' ) , 404 ) ;
9899 }
99100
100- handleResponse ( res , data . userContestRankingHistory , 'User contest history retrieved successfully' ) ;
101+ let contestHistory = data . userContestRankingHistory ;
102+
103+ // Filter to only attended contests if requested
104+ if ( attendedOnly ) {
105+ contestHistory = contestHistory . filter ( contest => contest . attended === true ) ;
106+ }
107+
108+ // Sort by most recent first (descending order)
109+ const sortedContestHistory = contestHistory . sort ( ( a , b ) => {
110+ const timeA = new Date ( a . contest . startTime * 1000 ) . getTime ( ) ;
111+ const timeB = new Date ( b . contest . startTime * 1000 ) . getTime ( ) ;
112+ return timeB - timeA ; // Descending order (most recent first)
113+ } ) ;
114+
115+ const message = attendedOnly
116+ ? 'User attended contest history retrieved successfully'
117+ : 'User contest history retrieved successfully' ;
118+
119+ handleResponse ( res , sortedContestHistory , message ) ;
101120 } catch ( error ) {
102121 handleError ( res , error ) ;
103122 }
@@ -276,6 +295,7 @@ export const getUserContestRanking = async (req, res) => {
276295export const getAllUserData = async ( req , res ) => {
277296 try {
278297 const username = validateUsername ( req . params . username ) ;
298+ const attendedOnly = req . params . attendedOnly === 'true' || req . query . attendedOnly === 'true' ;
279299
280300 // Fetch all user data in parallel for better performance
281301 const [
@@ -331,7 +351,21 @@ export const getAllUserData = async (req, res) => {
331351 } ,
332352 contestInfo : {
333353 ranking : contestData . status === 'fulfilled' ? contestData . value ?. userContestRanking : null ,
334- history : contestData . status === 'fulfilled' ? contestData . value ?. userContestRankingHistory || [ ] : [ ]
354+ history : contestData . status === 'fulfilled' && contestData . value ?. userContestRankingHistory
355+ ? ( ( ) => {
356+ let history = contestData . value . userContestRankingHistory ;
357+ // Filter to only attended contests if requested
358+ if ( attendedOnly ) {
359+ history = history . filter ( contest => contest . attended === true ) ;
360+ }
361+ // Sort by most recent first
362+ return history . sort ( ( a , b ) => {
363+ const timeA = new Date ( a . contest . startTime * 1000 ) . getTime ( ) ;
364+ const timeB = new Date ( b . contest . startTime * 1000 ) . getTime ( ) ;
365+ return timeB - timeA ; // Descending order (most recent first)
366+ } ) ;
367+ } ) ( )
368+ : [ ]
335369 } ,
336370 recentSubmissions : recentSubmissions . status === 'fulfilled' ? recentSubmissions . value ?. recentSubmissionList || [ ] : [ ] ,
337371 calendar : calendarData . status === 'fulfilled' ? calendarData . value ?. matchedUser ?. userCalendar : null ,
0 commit comments