1
+ import { logger } from '@/lib/utils' ;
1
2
import type { IMatchItemsResponse , IMatchType } from '@/types' ;
2
3
3
4
import { createRoom } from './collab' ;
@@ -16,12 +17,18 @@ export async function getMatchItems(
16
17
throw new Error ( 'Both user IDs are required' ) ;
17
18
}
18
19
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
+ }
23
31
24
- const allAttemptedQuestions = [ ...new Set ( [ ...attemptedQuestions1 , ...attemptedQuestions2 ] ) ] ;
25
32
const topics = topic ?. split ( '|' ) ?? [ ] ;
26
33
const payload = {
27
34
attemptedQuestions : allAttemptedQuestions ,
@@ -35,16 +42,21 @@ export async function getMatchItems(
35
42
// Get a random question
36
43
const question = await getRandomQuestion ( payload ) ;
37
44
45
+ if ( ! question ) {
46
+ logger . info ( 'No matching question found' ) ;
47
+ return undefined ;
48
+ }
49
+
38
50
const roomId = await createRoom ( userId1 , userId2 , question . id . toString ( ) ) ;
39
51
40
- console . log ( 'Successfully got match items' ) ;
52
+ logger . info ( 'Successfully got match items' ) ;
41
53
return {
42
54
roomId,
43
55
questionId : question . id ,
44
56
// question,
45
57
} ;
46
58
} catch ( error ) {
47
- console . error ( 'Error in getMatchItems:' , error ) ;
59
+ logger . error ( 'Error in getMatchItems:' , error ) ;
48
60
return undefined ;
49
61
}
50
62
}
0 commit comments