Skip to content

Commit 020d6cd

Browse files
committed
formatted
1 parent 4352b5a commit 020d6cd

File tree

11 files changed

+142
-127
lines changed

11 files changed

+142
-127
lines changed
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import axios from 'axios';
2+
23
import { PEERPREP_COLLAB_HOST } from '@/config';
34

5+
export async function createRoom(
6+
userId1: string,
7+
userId2: string,
8+
questionId: string
9+
): Promise<string> {
10+
const response = await axios.get<{ roomName: string }>(`${PEERPREP_COLLAB_HOST}/room`, {
11+
params: {
12+
userid1: userId1,
13+
userid2: userId2,
14+
questionid: questionId,
15+
},
16+
});
417

5-
export async function createRoom(userId1: string, userId2: string, questionId: string): Promise<string> {
6-
const response = await axios.get<{ roomName: string }>(
7-
`${PEERPREP_COLLAB_HOST}/room`,
8-
{
9-
params: {
10-
userid1: userId1,
11-
userid2: userId2,
12-
questionid: questionId,
13-
}
14-
}
15-
);
16-
1718
if (response.status !== 200 || !response.data?.roomName) {
1819
throw new Error('Failed to create room');
1920
}
2021

2122
return response.data.roomName;
22-
}
23+
}

backend/matching/src/services/get-match-items.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { IMatchType, IMatchItemsResponse } from '../types/index';
2-
import { fetchAttemptedQuestions, updateAttemptedQuestions } from './user';
3-
import { getRandomQuestion } from './question';
1+
import { IMatchItemsResponse,IMatchType } from '../types/index';
42
import { createRoom } from './collab';
3+
import { getRandomQuestion } from './question';
4+
import { fetchAttemptedQuestions } from './user';
55

66
export async function getMatchItems(
77
searchIdentifier: IMatchType,
@@ -17,10 +17,9 @@ export async function getMatchItems(
1717

1818
const [attemptedQuestions1, attemptedQuestions2] = await Promise.all([
1919
fetchAttemptedQuestions(userId1),
20-
fetchAttemptedQuestions(userId2)
20+
fetchAttemptedQuestions(userId2),
2121
]);
2222

23-
2423
const allAttemptedQuestions = [...new Set([...attemptedQuestions1, ...attemptedQuestions2])];
2524

2625
const payload = {
@@ -33,10 +32,9 @@ export async function getMatchItems(
3332
// Get a random question
3433
const question = await getRandomQuestion(payload);
3534

36-
3735
const roomName = await createRoom(userId1, userId2, question.id.toString());
3836

39-
console.log("Successfully got match items");
37+
console.log('Successfully got match items');
4038
return {
4139
roomName,
4240
questionId: question.id,
@@ -46,4 +44,4 @@ export async function getMatchItems(
4644
console.error('Error in getMatchItems:', error);
4745
return undefined;
4846
}
49-
}
47+
}

backend/matching/src/services/question.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import axios from 'axios';
2-
import { IServiceResponse, IQuestion, IGetRandomQuestionPayload } from '../types/index';
2+
33
import { PEERPREP_QUESTION_HOST } from '@/config';
44

5+
import { IGetRandomQuestionPayload,IQuestion, IServiceResponse } from '../types/index';
6+
57
export async function getRandomQuestion(payload: IGetRandomQuestionPayload): Promise<IQuestion> {
68
const response = await axios.post<IServiceResponse<{ question: IQuestion }>>(
79
`${PEERPREP_QUESTION_HOST}/questions/random`,
@@ -13,4 +15,4 @@ export async function getRandomQuestion(payload: IGetRandomQuestionPayload): Pro
1315
}
1416

1517
return response.data.data.question;
16-
}
18+
}

backend/matching/src/services/user.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,29 @@ import axios from 'axios';
33
import { PEERPREP_USER_HOST } from '@/config';
44

55
export async function fetchAttemptedQuestions(userId: string): Promise<number[]> {
6-
const response = await axios.post<number[]>(
7-
`${PEERPREP_USER_HOST}/user/attempted-question/get`,
8-
{userId}
9-
);
6+
const response = await axios.post<number[]>(`${PEERPREP_USER_HOST}/user/attempted-question/get`, {
7+
userId,
8+
});
9+
1010
if (response.status !== 200 || !response.data) {
1111
throw new Error(`Failed to fetch attempted questions for user ${userId}`);
1212
}
13+
1314
return response.data || [];
1415
}
1516

16-
export async function updateAttemptedQuestions(userIds: string[], questionId: number): Promise<void> {
17-
const response = await axios.post<unknown>(
18-
`${PEERPREP_USER_HOST}/user/attempted-question/add`,
19-
{ questionId, userIds }
20-
);
17+
export async function updateAttemptedQuestions(
18+
userIds: string[],
19+
questionId: number
20+
): Promise<void> {
21+
const response = await axios.post<unknown>(`${PEERPREP_USER_HOST}/user/attempted-question/add`, {
22+
questionId,
23+
userIds,
24+
});
25+
2126
if (response.status !== 200 || !response.data) {
2227
throw new Error(`Failed to update attempted questions for users ${userIds}`);
2328
}
29+
2430
return;
25-
}
31+
}

backend/matching/src/types/index.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,30 @@ export type IStreamMessage = {
2929
value?: Awaited<ReturnType<(typeof client)['ft']['search']>>['documents'][number]['value'];
3030
};
3131

32-
export type IMatchType = 'difficulty' | 'topic' | 'exact match' | undefined
33-
34-
35-
export interface IServiceResponse<T> {
36-
success: boolean;
37-
data?: T;
38-
error?: { message: string };
39-
}
40-
41-
export interface IQuestion {
42-
id: number;
43-
title: string;
44-
description: string;
45-
difficulty: string;
46-
topic: string[];
47-
}
48-
49-
export interface IGetRandomQuestionPayload {
50-
attemptedQuestions: number[];
51-
difficulty?: string;
52-
topic?: string;
53-
}
54-
55-
export interface IMatchItemsResponse {
56-
roomName: string;
57-
questionId: number;
58-
question: IQuestion;
59-
}
32+
export type IMatchType = 'difficulty' | 'topic' | 'exact match' | undefined;
33+
34+
export interface IServiceResponse<T> {
35+
success: boolean;
36+
data?: T;
37+
error?: { message: string };
38+
}
39+
40+
export interface IQuestion {
41+
id: number;
42+
title: string;
43+
description: string;
44+
difficulty: string;
45+
topic: string[];
46+
}
47+
48+
export interface IGetRandomQuestionPayload {
49+
attemptedQuestions: number[];
50+
difficulty?: string;
51+
topic?: string;
52+
}
53+
54+
export interface IMatchItemsResponse {
55+
roomName: string;
56+
questionId: number;
57+
question: IQuestion;
58+
}

backend/matching/src/workers/matcher.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { client, logQueueStatus } from '@/lib/db';
22
import { POOL_INDEX, STREAM_GROUP, STREAM_NAME, STREAM_WORKER } from '@/lib/db/constants';
33
import { decodePoolTicket, getPoolKey, getStreamId } from '@/lib/utils';
44
import { getMatchItems } from '@/services';
5-
import { MATCH_SVC_EVENT } from '@/ws';
65
import { IMatchType } from '@/types';
6+
import { MATCH_SVC_EVENT } from '@/ws';
77

88
import { connectClient, sendNotif } from './common';
99

@@ -64,7 +64,6 @@ async function processMatch(
6464
const matchedStreamId = getStreamId(timestamp);
6565

6666
logger.info(`Found match: ${JSON.stringify(matched)}`);
67-
6867

6968
await Promise.all([
7069
// Remove other from pool
@@ -74,9 +73,15 @@ async function processMatch(
7473
]);
7574

7675
// Notify both sockets
77-
const { ...matchItems } = await getMatchItems(searchIdentifier, topic, difficulty, requestorUserId, matchedUserId);
76+
const { ...matchItems } = await getMatchItems(
77+
searchIdentifier,
78+
topic,
79+
difficulty,
80+
requestorUserId,
81+
matchedUserId
82+
);
7883
logger.info(`Generated Match - ${JSON.stringify(matchItems)}`);
79-
84+
8085
sendNotif([requestorSocketPort, matchedSocketPort], MATCH_SVC_EVENT.SUCCESS, matchItems);
8186
sendNotif([requestorSocketPort, matchedSocketPort], MATCH_SVC_EVENT.DISCONNECT);
8287

@@ -91,7 +96,7 @@ async function processMatch(
9196

9297
async function match() {
9398
const redisClient = await connectClient(client);
94-
99+
95100
const stream = await redisClient.xReadGroup(
96101
STREAM_GROUP,
97102
STREAM_WORKER,

backend/user/src/controllers/questions/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { StatusCodes } from 'http-status-codes';
2+
3+
import { addAttemptedQuestionService,getAttemptedQuestionsService } from '@/services/questions';
24
import type { IRouteHandler } from '@/types';
3-
import { getAttemptedQuestionsService, addAttemptedQuestionService } from '@/services/questions';
45

56
export const addAttemptedQuestion: IRouteHandler = async (req, res) => {
67
const { questionId, userIds } = req.body; // Assuming the questionId is passed in the request body
@@ -34,4 +35,4 @@ export const getAttemptedQuestions: IRouteHandler = async (req, res) => {
3435
}
3536

3637
return res.status(StatusCodes.OK).json(data);
37-
};
38+
};

backend/user/src/routes/auth.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ router.post('/register', register);
1111
router.post('/username-valid', checkUsernameValid);
1212
router.post('/email-valid', checkEmailValid);
1313

14-
1514
router.use(limiter);
1615

1716
export default router;

backend/user/src/routes/user.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import express from 'express';
22

3-
import { getAttemptedQuestions, addAttemptedQuestion } from '@/controllers/questions';
4-
3+
import { addAttemptedQuestion,getAttemptedQuestions } from '@/controllers/questions';
54

65
const router = express.Router();
76

8-
router.post('/attempted-question/get', getAttemptedQuestions)
9-
router.post('/attempted-question/add', addAttemptedQuestion)
7+
router.post('/attempted-question/get', getAttemptedQuestions);
8+
router.post('/attempted-question/add', addAttemptedQuestion);
109

1110
export default router;

backend/user/src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { dbConfig, UI_HOST } from '@/config';
1212
import { db } from '@/lib/db';
1313
import { logger } from '@/lib/utils';
1414
import authRoutes from '@/routes/auth';
15-
import userRoutes from '@/routes/user';
1615
import authCheckRoutes from '@/routes/auth-check';
16+
import userRoutes from '@/routes/user';
1717

1818
const app = express();
1919
app.use(pino());
@@ -29,7 +29,7 @@ app.use(
2929

3030
app.use('/auth', authRoutes);
3131
app.use('/auth-check', authCheckRoutes);
32-
app.use('/user', userRoutes)
32+
app.use('/user', userRoutes);
3333

3434
// Health Check for Docker
3535
app.get('/health', (_req, res) => res.status(StatusCodes.OK).send('OK'));

0 commit comments

Comments
 (0)