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