|
1 | 1 | import { v4 as uuidv4 } from "uuid"; |
| 2 | +import axios from "axios"; |
2 | 3 | import db from "../db.js"; |
| 4 | +import config from "../config.js"; |
3 | 5 | import Room from "../models/roomModel.js"; |
4 | 6 | import roomManager from "../websocket/roomManager.js"; |
5 | 7 |
|
@@ -75,6 +77,85 @@ class RoomController { |
75 | 77 | } |
76 | 78 | } |
77 | 79 |
|
| 80 | + /** |
| 81 | + * Fetch question details from question service |
| 82 | + * @param {string} questionId - Question ID |
| 83 | + * @returns {Promise<Object|null>} - Question details (title, difficulty, topic) or null if not found |
| 84 | + */ |
| 85 | + async fetchQuestionDetails(questionId) { |
| 86 | + try { |
| 87 | + const response = await axios.get( |
| 88 | + `${config.QUESTION_SERVICE_URL}/v1/questions/${questionId}`, |
| 89 | + { timeout: 5000 } |
| 90 | + ); |
| 91 | + return { |
| 92 | + _id: response.data._id, |
| 93 | + title: response.data.title, |
| 94 | + difficulty: response.data.difficulty, |
| 95 | + topic: response.data.topic, |
| 96 | + }; |
| 97 | + } catch (error) { |
| 98 | + if (error.response?.status === 404) { |
| 99 | + console.warn(`Question ${questionId} not found`); |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + if (error.response) { |
| 104 | + console.error( |
| 105 | + `Failed to fetch question ${questionId}:`, |
| 106 | + `Status ${error.response.status}`, |
| 107 | + error.response.data?.error || error.response.statusText |
| 108 | + ); |
| 109 | + } else if (error.request) { |
| 110 | + console.error( |
| 111 | + `Failed to fetch question ${questionId}:`, |
| 112 | + `No response from question service at ${config.QUESTION_SERVICE_URL}.`, |
| 113 | + `Error: ${error.message || 'Network error'}` |
| 114 | + ); |
| 115 | + } else { |
| 116 | + console.error( |
| 117 | + `Failed to fetch question ${questionId}:`, |
| 118 | + error.message || 'Unknown error' |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + return null; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Get all rooms for a user with question details |
| 128 | + * @param {string} userId - User ID |
| 129 | + * @returns {Promise<Array<Object>>} - Array of room objects with question details |
| 130 | + */ |
| 131 | + async getRoomsByUserId(userId) { |
| 132 | + try { |
| 133 | + const rooms = await Room.findRoomsByUserId(userId); |
| 134 | + const roomsArray = rooms.map((room) => room.toObject()); |
| 135 | + |
| 136 | + const roomsWithQuestions = await Promise.all( |
| 137 | + roomsArray.map(async (room) => { |
| 138 | + if (room.questionId) { |
| 139 | + const questionDetails = await this.fetchQuestionDetails(room.questionId); |
| 140 | + return { |
| 141 | + ...room, |
| 142 | + question: questionDetails, |
| 143 | + }; |
| 144 | + } |
| 145 | + return { |
| 146 | + ...room, |
| 147 | + question: null, |
| 148 | + }; |
| 149 | + }) |
| 150 | + ); |
| 151 | + |
| 152 | + return roomsWithQuestions; |
| 153 | + } catch (error) { |
| 154 | + console.error(`Failed to get rooms for user ${userId}:`, error); |
| 155 | + throw error; |
| 156 | + } |
| 157 | + } |
| 158 | + |
78 | 159 | /** |
79 | 160 | * Get document content from Yjs persistence |
80 | 161 | * @param {string} roomId - Room ID |
|
0 commit comments