Skip to content

Commit 732c979

Browse files
authored
Merge pull request #90 from rjkoh/random-question
fix user-service nodemailer import, get random question, fix matching…
2 parents 5c5ba3d + e559115 commit 732c979

File tree

4 files changed

+44
-43
lines changed

4 files changed

+44
-43
lines changed

backend/matching-service/controllers/matchingController.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ export function setupSocketListeners() {
9494
}
9595

9696
if (category === "") {
97-
category = "Any";
97+
category = "all";
9898
}
9999

100100
if (difficulty === "") {
101-
category = "Any";
101+
difficulty = "all";
102102
}
103103

104104
const socketId = socket.id;
@@ -304,8 +304,8 @@ async function handleMatchEvent(
304304

305305
socket1.emit(SOCKET_EVENTS.MATCH_FOUND, {
306306
message: `You have been matched with User ID: ${user2.userId}`,
307-
category: user1.category || user2.category || "Any",
308-
difficulty: user1.difficulty || user2.difficulty || "Any",
307+
category: user1.category || user2.category || "all",
308+
difficulty: user1.difficulty || user2.difficulty || "all",
309309
attemptStartedAt: Date.now(),
310310
matchId: matchId,
311311
roomId: roomId,
@@ -315,8 +315,8 @@ async function handleMatchEvent(
315315

316316
socket2.emit(SOCKET_EVENTS.MATCH_FOUND, {
317317
message: `You have been matched with User ID: ${user1.userId}`,
318-
category: user2.category || user1.category || "Any",
319-
difficulty: user2.difficulty || user1.difficulty || "Any",
318+
category: user2.category || user1.category || "all",
319+
difficulty: user2.difficulty || user1.difficulty || "all",
320320
attemptStartedAt: Date.now(),
321321
matchId: matchId,
322322
roomId: roomId,

backend/matching-service/workers/matchWorker.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ function getRelaxedCriteria(
174174
difficulty: userData.difficulty,
175175
};
176176
case 1:
177-
return { category: userData.category, difficulty: "Any" };
177+
return { category: userData.category, difficulty: "all" };
178178
case 2:
179-
return { category: "Any", difficulty: userData.difficulty };
179+
return { category: "all", difficulty: userData.difficulty };
180180
default:
181181
return {
182182
category: userData.category,
@@ -196,10 +196,10 @@ function isCriteriaSatisfied(
196196
relaxedCriteria: { category: string; difficulty: string }
197197
): boolean {
198198
const categoryMatch =
199-
relaxedCriteria.category === "Any" ||
199+
relaxedCriteria.category === "all" ||
200200
user.category === relaxedCriteria.category;
201201
const difficultyMatch =
202-
relaxedCriteria.difficulty === "Any" ||
202+
relaxedCriteria.difficulty === "all" ||
203203
user.difficulty === relaxedCriteria.difficulty;
204204
return categoryMatch && difficultyMatch;
205205
}
@@ -227,8 +227,8 @@ async function handleMatch(user1: any, user2: any) {
227227
);
228228
console.log(messagesBefore);
229229

230-
const category = user1.category || user2.category || "Any";
231-
const difficulty = user1.difficulty || user2.difficulty || "Any";
230+
const category = user1.category == 'all' ? user2.category : user1.category;
231+
const difficulty = user1.difficulty == 'all' ? user2.difficulty : user1.difficulty;
232232

233233
const matchingEvent = new MatchingEventModel({
234234
category: category,

backend/question-service/controllers/questionsController.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import categoryModel from "../models/Category";
33
import { Request, Response, NextFunction } from "express";
44
import { EachMessagePayload } from "kafkajs";
55
import { producer, QUESTION_TOPIC } from "../utils/kafkaClient";
6+
import { Types } from "mongoose";
67

78
const DIFFICULTIES = ["easy", "medium", "hard"];
89

@@ -238,46 +239,51 @@ async function findSuitableQuestion(categoryName: string, difficulty: string) {
238239
}
239240
}
240241

241-
// If difficulty is "any", return any question from the category
242-
if (difficulty.toLowerCase() === "all") {
243-
return await questionModel.findOne({
244-
...(category ? { categories: category._id } : {}),
245-
});
242+
const query: { categories?: Types.ObjectId; difficulty?: RegExp } = {
243+
...(category ? { categories: category._id } : {}),
244+
};
245+
246+
if (difficulty.toLowerCase() !== "all") {
247+
query.difficulty = new RegExp(`^${difficulty}$`, "i");
246248
}
249+
console.log(query);
247250

248-
let question = await questionModel.findOne({
249-
...(category ? { categories: category._id } : {}),
250-
difficulty: new RegExp(`^${difficulty}$`, "i"),
251-
});
251+
// Attempt to find a question with the exact specified difficulty
252+
let questions = await questionModel.aggregate([
253+
{ $match: query },
254+
{ $sample: { size: 1 } },
255+
]);
252256

253-
if (question) {
254-
return question;
257+
if (questions.length > 0) {
258+
return questions[0];
255259
}
256260

257261
const difficultyIndex = DIFFICULTIES.indexOf(difficulty.toLowerCase());
258262
if (difficultyIndex === -1) {
259263
throw new Error(`Invalid difficulty level: ${difficulty}`);
260264
}
261265

262-
// Find lower difficulty question if exact match is not found
266+
// Adjust the search to find the nearest difficulty level, starting with lower levels
263267
for (let i = difficultyIndex - 1; i >= 0; i--) {
264-
question = await questionModel.findOne({
265-
...(category ? { categories: category._id } : {}),
266-
difficulty: new RegExp(`^${DIFFICULTIES[i]}$`, "i"),
267-
});
268-
if (question) {
269-
return question;
268+
query.difficulty = new RegExp(`^${DIFFICULTIES[i]}$`, "i"); // Case-insensitive exact match for lower difficulty
269+
questions = await questionModel.aggregate([
270+
{ $match: query },
271+
{ $sample: { size: 1 } },
272+
]);
273+
if (questions.length > 0) {
274+
return questions[0];
270275
}
271276
}
272277

273-
// Find higher difficulty question if lower difficulty not found
278+
// Check higher difficulty levels if lower levels are unavailable
274279
for (let i = difficultyIndex + 1; i < DIFFICULTIES.length; i++) {
275-
question = await questionModel.findOne({
276-
...(category ? { categories: category._id } : {}),
277-
difficulty: new RegExp(`^${DIFFICULTIES[i]}$`, "i"),
278-
});
279-
if (question) {
280-
return question;
280+
query.difficulty = new RegExp(`^${DIFFICULTIES[i]}$`, "i"); // Case-insensitive exact match for higher difficulty
281+
questions = await questionModel.aggregate([
282+
{ $match: query },
283+
{ $sample: { size: 1 } },
284+
]);
285+
if (questions.length > 0) {
286+
return questions[0];
281287
}
282288
}
283289

backend/user-service/package-lock.json

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)