1
- export const getMatchItems = ( ) => {
2
- // TODO: Get Match Items
3
- return {
4
- roomId : '' ,
5
- questionId : '' ,
6
- } ;
1
+ import { IMatchType } from "@/types" ;
2
+ import axios from 'axios' ;
3
+
4
+ interface IGetRandomQuestionPayload {
5
+ attemptedQuestions : number [ ] ;
6
+ difficulty ?: string ;
7
+ topic ?: string ;
8
+ }
9
+
10
+ interface IQuestion {
11
+ id : number ;
12
+ title : string ;
13
+ description : string ;
14
+ difficulty : string ;
15
+ topic : string [ ] ;
16
+ }
17
+
18
+ interface IServiceResponse < T > {
19
+ success : boolean ;
20
+ data ?: T ;
21
+ error ?: { message : string } ;
22
+ }
23
+
24
+ interface IMatchItemsResponse {
25
+ roomId : string ;
26
+ questionId : number ;
27
+ question : IQuestion ;
28
+ }
29
+
30
+ export const getMatchItems = async (
31
+ searchIdentifier : IMatchType ,
32
+ topic ?: string ,
33
+ difficulty ?: string ,
34
+ userId1 ?: string ,
35
+ userId2 ?: string
36
+ ) : Promise < IServiceResponse < IMatchItemsResponse > > => {
37
+ const userEndpoint = `${ process . env . USER_SERVER_ENDPOINT } ` ;
38
+ const questionEndpoint = `${ process . env . QUESTION_SERVER_ENDPOINT } ` ;
39
+ const collabServerEndpoint = `${ process . env . COLLAB_SERVER_ENDPOINT } ` ;
40
+
41
+ try {
42
+ if ( ! userId1 || ! userId2 ) {
43
+ throw new Error ( 'Both user IDs are required' ) ;
44
+ }
45
+
46
+ // Fetch attempted questions for both users
47
+ const [ attemptedQuestions1 , attemptedQuestions2 ] = await Promise . all ( [
48
+ fetchAttemptedQuestions ( userEndpoint , userId1 ) ,
49
+ fetchAttemptedQuestions ( userEndpoint , userId2 )
50
+ ] ) ;
51
+
52
+ // Combine attempted questions from both users
53
+ const allAttemptedQuestions = [ ...new Set ( [ ...attemptedQuestions1 , ...attemptedQuestions2 ] ) ] ;
54
+
55
+ // Prepare payload for the /random endpoint
56
+ const payload : IGetRandomQuestionPayload = {
57
+ attemptedQuestions : allAttemptedQuestions ,
58
+ } ;
59
+
60
+ if ( searchIdentifier === 'difficulty' && difficulty ) {
61
+ payload . difficulty = difficulty ;
62
+ } else if ( searchIdentifier === 'topic' && topic ) {
63
+ payload . topic = topic ;
64
+ } else if ( searchIdentifier === 'exact match' && topic && difficulty ) {
65
+ payload . topic = topic ;
66
+ payload . difficulty = difficulty ;
67
+ }
68
+
69
+ // Query the question endpoint using the /random endpoint
70
+ const questionResponse = await axios . post < IServiceResponse < { question : IQuestion } > > (
71
+ `${ questionEndpoint } /random` ,
72
+ payload
73
+ ) ;
74
+
75
+ if ( ! questionResponse . data . success || ! questionResponse . data . data ?. question ) {
76
+ throw new Error ( questionResponse . data . error ?. message || 'Failed to get a random question' ) ;
77
+ }
78
+
79
+ const questionId = questionResponse . data . data . question . id ;
80
+
81
+ // Update attempted questions for both users
82
+ await Promise . all ( [
83
+ updateAttemptedQuestions ( userEndpoint , userId1 , questionId ) ,
84
+ updateAttemptedQuestions ( userEndpoint , userId2 , questionId )
85
+ ] ) ;
86
+
87
+ // Query the collab server for the room ID
88
+ const roomResponse = await axios . get < IServiceResponse < { roomId : string } > > (
89
+ `${ collabServerEndpoint } /rooms` ,
90
+ {
91
+ params : {
92
+ userId1,
93
+ userId2,
94
+ questionId : questionId . toString ( ) ,
95
+ }
96
+ }
97
+ ) ;
98
+
99
+ if ( ! roomResponse . data . success || ! roomResponse . data . data ?. roomId ) {
100
+ throw new Error ( roomResponse . data . error ?. message || 'Failed to create room' ) ;
101
+ }
102
+
103
+ return {
104
+ success : true ,
105
+ data : {
106
+ roomId : roomResponse . data . data . roomId ,
107
+ questionId : questionId ,
108
+ question : questionResponse . data . data . question ,
109
+ }
110
+ } ;
111
+ } catch ( error ) {
112
+ console . error ( 'Error in getMatchItems:' , error ) ;
113
+ return {
114
+ success : false ,
115
+ error : {
116
+ message : error instanceof Error ? error . message : 'An unknown error occurred' ,
117
+ }
118
+ } ;
119
+ }
7
120
} ;
121
+
122
+ async function fetchAttemptedQuestions ( userEndpoint : string , userId : string ) : Promise < number [ ] > {
123
+ const response = await axios . get < IServiceResponse < { attemptedQuestions : number [ ] } > > (
124
+ `${ userEndpoint } /user/${ userId } /attempted-questions`
125
+ ) ;
126
+ if ( ! response . data . success || ! response . data . data ?. attemptedQuestions ) {
127
+ throw new Error ( `Failed to fetch attempted questions for user ${ userId } ` ) ;
128
+ }
129
+ return response . data . data . attemptedQuestions ;
130
+ }
131
+
132
+ async function updateAttemptedQuestions ( userEndpoint : string , userId : string , questionId : number ) : Promise < void > {
133
+ const response = await axios . post < IServiceResponse < { message : string } > > (
134
+ `${ userEndpoint } /user/${ userId } /attempted-question` ,
135
+ { questionId }
136
+ ) ;
137
+ if ( ! response . data . success ) {
138
+ throw new Error ( `Failed to update attempted questions for user ${ userId } ` ) ;
139
+ }
140
+ }
0 commit comments