From 23e41d4f84a5041bafb3a1390c8b27e4fe744908 Mon Sep 17 00:00:00 2001 From: Kevin Tjan Date: Sun, 10 Nov 2024 15:26:05 +0800 Subject: [PATCH 1/2] Fix Room Schema Mapping --- backend/matching-service/models/RoomSchema.ts | 2 +- .../matching-service/workers/matchWorker.ts | 11 +++++--- frontend/src/domain/entities/Room.ts | 4 +-- .../src/domain/usecases/HistoryUseCases.ts | 4 +-- .../pages/room/CollaborationRoomPage.tsx | 25 +++++++++++-------- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/backend/matching-service/models/RoomSchema.ts b/backend/matching-service/models/RoomSchema.ts index 9d78e2b817..440a2b43a1 100644 --- a/backend/matching-service/models/RoomSchema.ts +++ b/backend/matching-service/models/RoomSchema.ts @@ -12,7 +12,7 @@ export interface Room extends mongoose.Document { const roomSchema: Schema = new mongoose.Schema({ - participants: [{ userId: String }], + participants: [{ type: String }], category: { type: String, default: 'Any' }, difficulty: { type: String, default: 'Any' }, roomId: { type: String, required: true }, diff --git a/backend/matching-service/workers/matchWorker.ts b/backend/matching-service/workers/matchWorker.ts index 4904c80944..3a38408765 100644 --- a/backend/matching-service/workers/matchWorker.ts +++ b/backend/matching-service/workers/matchWorker.ts @@ -244,7 +244,7 @@ async function handleMatch(user1: any, user2: any) { matchId: savedEvent._id.toString(), }; - await producer.send({ + await producer.send({ topic: MATCH_TOPIC, messages: [ { @@ -395,18 +395,23 @@ export async function emitMatchEvent(matchId: string) { } const roomObject = JSON.parse(data); + console.log(roomObject); if (roomObject.questionId && roomObject.roomId) { const newRoom = new Room({ participants: [ - { userId: roomObject.user1.userId }, - { userId: roomObject.user2.userId }, + JSON.parse(roomObject.user1).userId, + JSON.parse(roomObject.user2).userId, ], category: roomObject.category, difficulty: roomObject.difficulty, roomId: roomObject.roomId, questionId: roomObject.questionId, }); + console.log(newRoom); + console.log( + JSON.parse(roomObject.user1).userId, + JSON.parse(roomObject.user2).userId); await newRoom.save(); diff --git a/frontend/src/domain/entities/Room.ts b/frontend/src/domain/entities/Room.ts index 15cbba4ea3..84bac1cd15 100644 --- a/frontend/src/domain/entities/Room.ts +++ b/frontend/src/domain/entities/Room.ts @@ -1,7 +1,7 @@ export interface Room { roomId: string; attemptStartedAt: string; - userIdOne: { _id: string }; - userIdTwo: { _id: string }; + userIdOne: string; + userIdTwo: string; questionId: string; } diff --git a/frontend/src/domain/usecases/HistoryUseCases.ts b/frontend/src/domain/usecases/HistoryUseCases.ts index 80e2d6d16a..333c3a16b4 100644 --- a/frontend/src/domain/usecases/HistoryUseCases.ts +++ b/frontend/src/domain/usecases/HistoryUseCases.ts @@ -25,7 +25,7 @@ export class HistoryUseCases { || !attemptStartedAt || attemptStartedAt.trim() === "" || !attemptCompletedAt || attemptCompletedAt.trim() === "" || !collaboratorId || collaboratorId.trim() === "") { - throw new Error("Missing Attempt Details"); + throw new Error(`Missing Attempt Details, ${questionId} ${roomId} ${attemptStartedAt} ${attemptCompletedAt} ${collaboratorId} ${attemptCode}`); } await this.historyRepository.createOrUpdateUserHistory(questionId, roomId, attemptStartedAt, attemptCompletedAt, collaboratorId, attemptCode, true); } @@ -42,7 +42,7 @@ export class HistoryUseCases { || !attemptStartedAt || attemptStartedAt.trim() === "" || !attemptCompletedAt || attemptCompletedAt.trim() === "" || !collaboratorId || collaboratorId.trim() === "") { - throw new Error("Missing Attempt Details"); + throw new Error(`Missing Attempt Details, ${questionId} ${roomId} ${attemptStartedAt} ${attemptCompletedAt} ${collaboratorId} ${attemptCode}`); } await this.historyRepository.createOrUpdateUserHistory(questionId, roomId, attemptStartedAt, attemptCompletedAt, collaboratorId, attemptCode, false); } diff --git a/frontend/src/presentation/pages/room/CollaborationRoomPage.tsx b/frontend/src/presentation/pages/room/CollaborationRoomPage.tsx index 12188d33dd..dd8b287533 100644 --- a/frontend/src/presentation/pages/room/CollaborationRoomPage.tsx +++ b/frontend/src/presentation/pages/room/CollaborationRoomPage.tsx @@ -44,16 +44,17 @@ const CollaborationRoomPage: React.FC = () => { setRoom({ roomId: roomId, attemptStartedAt: attemptStartedAt, - userIdOne: { _id: user!._id }, - userIdTwo: { _id: matchUserId }, + userIdOne: user!._id, + userIdTwo: matchUserId, questionId: questionId }); } else { if (!urlRoomId) return; // Fetch room details from API const fetchedRoom = await roomUseCases.getRoomDetails(urlRoomId); + console.log("Fetched Room", fetchedRoom) // Ensure the current user is userIdOne - if (fetchedRoom.userIdOne._id !== user?._id) { + if (fetchedRoom.userIdOne !== user!._id) { const temp = fetchedRoom.userIdOne; fetchedRoom.userIdOne = fetchedRoom.userIdTwo; fetchedRoom.userIdTwo = temp; @@ -132,14 +133,16 @@ const CollaborationRoomPage: React.FC = () => {
-
- -
+ {room.questionId && room.roomId && room.attemptStartedAt && room.userIdTwo && +
+ +
+ }
From 5421e89ff909da0809ba1e23de5f2473c872918e Mon Sep 17 00:00:00 2001 From: Kevin Tjan Date: Sun, 10 Nov 2024 15:29:23 +0800 Subject: [PATCH 2/2] Remove Comments --- backend/matching-service/workers/matchWorker.ts | 8 +------- frontend/src/domain/usecases/HistoryUseCases.ts | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/backend/matching-service/workers/matchWorker.ts b/backend/matching-service/workers/matchWorker.ts index 3a38408765..e20e643016 100644 --- a/backend/matching-service/workers/matchWorker.ts +++ b/backend/matching-service/workers/matchWorker.ts @@ -244,7 +244,7 @@ async function handleMatch(user1: any, user2: any) { matchId: savedEvent._id.toString(), }; - await producer.send({ + await producer.send({ topic: MATCH_TOPIC, messages: [ { @@ -395,7 +395,6 @@ export async function emitMatchEvent(matchId: string) { } const roomObject = JSON.parse(data); - console.log(roomObject); if (roomObject.questionId && roomObject.roomId) { const newRoom = new Room({ @@ -408,11 +407,6 @@ export async function emitMatchEvent(matchId: string) { roomId: roomObject.roomId, questionId: roomObject.questionId, }); - console.log(newRoom); - console.log( - JSON.parse(roomObject.user1).userId, - JSON.parse(roomObject.user2).userId); - await newRoom.save(); await redisClient.xAdd("match_events", "*", { data }); diff --git a/frontend/src/domain/usecases/HistoryUseCases.ts b/frontend/src/domain/usecases/HistoryUseCases.ts index 333c3a16b4..80e2d6d16a 100644 --- a/frontend/src/domain/usecases/HistoryUseCases.ts +++ b/frontend/src/domain/usecases/HistoryUseCases.ts @@ -25,7 +25,7 @@ export class HistoryUseCases { || !attemptStartedAt || attemptStartedAt.trim() === "" || !attemptCompletedAt || attemptCompletedAt.trim() === "" || !collaboratorId || collaboratorId.trim() === "") { - throw new Error(`Missing Attempt Details, ${questionId} ${roomId} ${attemptStartedAt} ${attemptCompletedAt} ${collaboratorId} ${attemptCode}`); + throw new Error("Missing Attempt Details"); } await this.historyRepository.createOrUpdateUserHistory(questionId, roomId, attemptStartedAt, attemptCompletedAt, collaboratorId, attemptCode, true); } @@ -42,7 +42,7 @@ export class HistoryUseCases { || !attemptStartedAt || attemptStartedAt.trim() === "" || !attemptCompletedAt || attemptCompletedAt.trim() === "" || !collaboratorId || collaboratorId.trim() === "") { - throw new Error(`Missing Attempt Details, ${questionId} ${roomId} ${attemptStartedAt} ${attemptCompletedAt} ${collaboratorId} ${attemptCode}`); + throw new Error("Missing Attempt Details"); } await this.historyRepository.createOrUpdateUserHistory(questionId, roomId, attemptStartedAt, attemptCompletedAt, collaboratorId, attemptCode, false); }