1+ import { logger } from '@/lib/utils' ;
12import type { IMatchItemsResponse , IMatchType } from '@/types' ;
23
34import { createRoom } from './collab' ;
@@ -16,12 +17,18 @@ export async function getMatchItems(
1617 throw new Error ( 'Both user IDs are required' ) ;
1718 }
1819
19- const [ attemptedQuestions1 , attemptedQuestions2 ] = await Promise . all ( [
20- fetchAttemptedQuestions ( userId1 ) ,
21- fetchAttemptedQuestions ( userId2 ) ,
22- ] ) ;
20+ let allAttemptedQuestions : number [ ] = [ ] ;
21+
22+ try {
23+ const [ attemptedQuestions1 , attemptedQuestions2 ] = await Promise . all ( [
24+ fetchAttemptedQuestions ( userId1 ) ,
25+ fetchAttemptedQuestions ( userId2 ) ,
26+ ] ) ;
27+ allAttemptedQuestions = [ ...new Set ( [ ...attemptedQuestions1 , ...attemptedQuestions2 ] ) ] ;
28+ } catch ( error ) {
29+ logger . error ( 'Error in getMatchItems: Failed to fetch attempted questions' , error ) ;
30+ }
2331
24- const allAttemptedQuestions = [ ...new Set ( [ ...attemptedQuestions1 , ...attemptedQuestions2 ] ) ] ;
2532 const topics = topic ?. split ( '|' ) ?? [ ] ;
2633 const payload = {
2734 attemptedQuestions : allAttemptedQuestions ,
@@ -35,16 +42,21 @@ export async function getMatchItems(
3542 // Get a random question
3643 const question = await getRandomQuestion ( payload ) ;
3744
45+ if ( ! question ) {
46+ logger . info ( 'No matching question found' ) ;
47+ return undefined ;
48+ }
49+
3850 const roomId = await createRoom ( userId1 , userId2 , question . id . toString ( ) ) ;
3951
40- console . log ( 'Successfully got match items' ) ;
52+ logger . info ( 'Successfully got match items' ) ;
4153 return {
4254 roomId,
4355 questionId : question . id ,
4456 // question,
4557 } ;
4658 } catch ( error ) {
47- console . error ( 'Error in getMatchItems:' , error ) ;
59+ logger . error ( 'Error in getMatchItems:' , error ) ;
4860 return undefined ;
4961 }
5062}
0 commit comments