@@ -7,6 +7,13 @@ interface IGetRandomQuestionPayload {
7
7
topic ?: string ;
8
8
}
9
9
10
+
11
+ interface IServiceResponse < T > {
12
+ success : boolean ;
13
+ data ?: T ;
14
+ error ?: { message : string } ;
15
+ }
16
+
10
17
interface IQuestion {
11
18
id : number ;
12
19
title : string ;
@@ -15,25 +22,19 @@ interface IQuestion {
15
22
topic : string [ ] ;
16
23
}
17
24
18
- interface IServiceResponse < T > {
19
- success : boolean ;
20
- data ?: T ;
21
- error ?: { message : string } ;
22
- }
23
-
24
25
interface IMatchItemsResponse {
25
- roomId : string ;
26
- questionId : number ;
27
- question : IQuestion ;
26
+ roomName : string ;
27
+ questionId : number ;
28
+ question : IQuestion ;
28
29
}
29
30
30
31
export const getMatchItems = async (
31
- searchIdentifier : IMatchType ,
32
- topic ?: string ,
33
- difficulty ?: string ,
34
- userId1 ?: string ,
35
- userId2 ?: string
36
- ) : Promise < IServiceResponse < IMatchItemsResponse > > => {
32
+ searchIdentifier : IMatchType ,
33
+ topic ?: string ,
34
+ difficulty ?: string ,
35
+ userId1 ?: string ,
36
+ userId2 ?: string
37
+ ) : Promise < IMatchItemsResponse | undefined > => {
37
38
const userEndpoint = `${ process . env . USER_SERVER_ENDPOINT } ` ;
38
39
const questionEndpoint = `${ process . env . QUESTION_SERVER_ENDPOINT } ` ;
39
40
const collabServerEndpoint = `${ process . env . COLLAB_SERVER_ENDPOINT } ` ;
@@ -68,11 +69,11 @@ export const getMatchItems = async (
68
69
69
70
// Query the question endpoint using the /random endpoint
70
71
const questionResponse = await axios . post < IServiceResponse < { question : IQuestion } > > (
71
- `${ questionEndpoint } /random` ,
72
+ `${ questionEndpoint } /questions/ random` ,
72
73
payload
73
74
) ;
74
75
75
- if ( ! questionResponse . data . success || ! questionResponse . data . data ?. question ) {
76
+ if ( questionResponse . status !== 200 || ! questionResponse . data . data ) {
76
77
throw new Error ( questionResponse . data . error ?. message || 'Failed to get a random question' ) ;
77
78
}
78
79
@@ -85,56 +86,48 @@ export const getMatchItems = async (
85
86
] ) ;
86
87
87
88
// Query the collab server for the room ID
88
- const roomResponse = await axios . get < IServiceResponse < { roomId : string } > > (
89
- `${ collabServerEndpoint } /rooms ` ,
89
+ const roomResponse = await axios . get < { roomName : string } > (
90
+ `${ collabServerEndpoint } /room ` ,
90
91
{
91
92
params : {
92
- userId1,
93
- userId2,
94
- questionId : questionId . toString ( ) ,
93
+ userid1 : userId1 ,
94
+ userid2 : userId2 ,
95
+ questionid : questionId . toString ( ) ,
95
96
}
96
97
}
97
- ) ;
98
-
99
- if ( ! roomResponse . data . success || ! roomResponse . data . data ?. roomId ) {
100
- throw new Error ( roomResponse . data . error ?. message || 'Failed to create room' ) ;
98
+ ) ;
99
+ if ( roomResponse . status !== 200 || ! roomResponse . data ?. roomName ) {
100
+ throw new Error ( 'Failed to create room' ) ;
101
101
}
102
102
103
+ console . log ( "Succesfully got match items" ) ;
103
104
return {
104
- success : true ,
105
- data : {
106
- roomId : roomResponse . data . data . roomId ,
105
+ roomName : roomResponse . data . roomName ,
107
106
questionId : questionId ,
108
107
question : questionResponse . data . data . question ,
109
108
}
110
- } ;
111
109
} catch ( error ) {
112
110
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
- }
120
111
} ;
112
+ }
121
113
122
114
async function fetchAttemptedQuestions ( userEndpoint : string , userId : string ) : Promise < number [ ] > {
123
- const response = await axios . get < IServiceResponse < { attemptedQuestions : number [ ] } > > (
115
+ const response = await axios . get < number [ ] > (
124
116
`${ userEndpoint } /user/${ userId } /attempted-questions`
125
117
) ;
126
- if ( ! response . data . success || ! response . data . data ?. attemptedQuestions ) {
118
+ if ( response . status !== 200 || ! response . data ) {
127
119
throw new Error ( `Failed to fetch attempted questions for user ${ userId } ` ) ;
128
120
}
129
- return response . data . data . attemptedQuestions ;
121
+ return response . data || [ ]
130
122
}
131
123
132
124
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` ,
125
+ const response = await axios . post < unknown > (
126
+ `${ userEndpoint } /user/${ userId } /attempt -question` ,
135
127
{ questionId }
136
128
) ;
137
- if ( ! response . data . success ) {
138
- throw new Error ( `Failed to update attempted questions for user ${ userId } ` ) ;
129
+ if ( response . status !== 200 || ! response . data ) {
130
+ throw new Error ( `Failed to fetch attempted questions for user ${ userId } ` ) ;
139
131
}
132
+ return ;
140
133
}
0 commit comments