Skip to content

Commit 5bb9fcf

Browse files
authored
Merge pull request #57 from CS3219-AY2425S1/anun/fix1
fix logging and try catch if attemptedquestions fetch fails
2 parents 7225ddb + 1f1bb99 commit 5bb9fcf

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { logger } from '@/lib/utils';
12
import type { IMatchItemsResponse, IMatchType } from '@/types';
23

34
import { createRoom } from './collab';
@@ -16,12 +17,18 @@ export async function getMatchItems(
1617
throw new Error('Both user IDs are required');
1718
}
1819

19-
const [attemptedQuestions1, attemptedQuestions2] = await Promise.all([
20-
fetchAttemptedQuestions(userId1),
21-
fetchAttemptedQuestions(userId2),
22-
]);
20+
let allAttemptedQuestions: number[] = [];
21+
22+
try {
23+
const [attemptedQuestions1, attemptedQuestions2] = await Promise.all([
24+
fetchAttemptedQuestions(userId1),
25+
fetchAttemptedQuestions(userId2),
26+
]);
27+
allAttemptedQuestions = [...new Set([...attemptedQuestions1, ...attemptedQuestions2])];
28+
} catch (error) {
29+
logger.error('Error in getMatchItems: Failed to fetch attempted questions', error);
30+
}
2331

24-
const allAttemptedQuestions = [...new Set([...attemptedQuestions1, ...attemptedQuestions2])];
2532
const topics = topic?.split('|') ?? [];
2633
const payload = {
2734
attemptedQuestions: allAttemptedQuestions,
@@ -35,16 +42,21 @@ export async function getMatchItems(
3542
// Get a random question
3643
const question = await getRandomQuestion(payload);
3744

45+
if (!question) {
46+
logger.info('No matching question found');
47+
return undefined;
48+
}
49+
3850
const roomId = await createRoom(userId1, userId2, question.id.toString());
3951

40-
console.log('Successfully got match items');
52+
logger.info('Successfully got match items');
4153
return {
4254
roomId,
4355
questionId: question.id,
4456
// question,
4557
};
4658
} catch (error) {
47-
console.error('Error in getMatchItems:', error);
59+
logger.error('Error in getMatchItems:', error);
4860
return undefined;
4961
}
5062
}

0 commit comments

Comments
 (0)