Skip to content

Commit c2cbdfa

Browse files
limcaaarlKhoonSun47McNaBry
authored
Bug Fix for Submit Button and Forfeit (#81)
* Update Collaboration Service API Call: - Close room if two users' isForfeit status is true * Update README.md for Collaboration Service * Added clarification on roomController methods * Fix bug Fix the bug for submit button, in which the states were not being flipped correctly. * Remove NgOptimizedImage import from home component --------- Co-authored-by: KhoonSun47 <[email protected]> Co-authored-by: McNaBry <[email protected]>
1 parent 3ce9ac7 commit c2cbdfa

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

frontend/src/app/collaboration/editor/editor.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export class EditorComponent implements AfterViewInit, OnInit {
323323
detail: 'Submission failed: Not all participants agreed. Please try again.',
324324
});
325325
}
326-
326+
this.isInitiator = false;
327327
this.isSubmit = false;
328328
}
329329

frontend/src/app/collaboration/submit-dialog/submit-dialog.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,20 @@ export class SubmitDialogComponent implements AfterViewInit {
7676

7777
this.yshow.observe(() => {
7878
const isShow = this.yshow.get('show');
79-
8079
if (isShow) {
8180
this.isVisible = true;
8281
} else {
8382
this.dialogClose.emit(this.numForfeit);
8483
this.isVisible = false;
84+
this.isInitiator = false;
8585
this.ysubmit.clear();
8686
}
8787
});
8888
}
8989

9090
onDialogShow() {
91-
this.yshow.set('show', true);
9291
if (this.isInitiator) {
92+
this.yshow.set('show', true);
9393
if (this.numForfeit == 0 && this.numUniqueUsers == 2) {
9494
this.message = "Waiting for the other user's decision...";
9595
this.ysubmit.set(this.userId!, true);
@@ -139,6 +139,7 @@ export class SubmitDialogComponent implements AfterViewInit {
139139
cancel() {
140140
this.yshow.set('show', false);
141141
this.ysubmit.clear();
142+
this.isInitiator = false;
142143
}
143144

144145
showSubmitDialog() {

services/collaboration/src/controllers/roomController.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export const getRoomByRoomIdController = async (req: Request, res: Response) =>
7373
};
7474

7575
/**
76-
* Controller function to close a room and delete its Yjs document
76+
* Controller function to close a room and delete its Yjs document.
77+
* This function is generally used when a room session ends successfully.
7778
* @param req
7879
* @param res
7980
*/
@@ -87,17 +88,22 @@ export const closeRoomController = async (req: Request, res: Response) => {
8788
return handleHttpNotFound(res, 'Room not found');
8889
}
8990

91+
// Check if the room is already closed
9092
if (!room.room_status) {
9193
console.log(`Room ${roomId} is already closed.`);
9294
return handleHttpSuccess(res, `Room ${roomId} is already closed`);
9395
}
9496

97+
// Close the room by setting its status to false
9598
const result = await closeRoomById(roomId);
9699
if (result.modifiedCount === 0) {
97100
return handleHttpNotFound(res, 'Room not found');
98101
}
99102

103+
// Delete the Yjs document associated with the room
100104
await deleteYjsDocument(roomId);
105+
106+
// Mark history as completed for users who did not forfeit
101107
await Promise.all(
102108
room.users
103109
.filter(user => !user.isForfeit)
@@ -114,7 +120,9 @@ export const closeRoomController = async (req: Request, res: Response) => {
114120
};
115121

116122
/**
117-
* Controller function to update user isForfeit status in a room
123+
* Controller function to update the isForfeit status of a user in a room.
124+
* This function is typically triggered when a user forfeits from an ongoing session.
125+
* If all users in the room forfeit, the room is closed automatically.
118126
* @param req
119127
* @param res
120128
*/
@@ -123,36 +131,47 @@ export const updateUserStatusInRoomController = async (req: Request, res: Respon
123131
const { roomId } = req.params;
124132
const { isForfeit } = req.body;
125133

134+
// Validate that isForfeit is a boolean value
126135
if (typeof isForfeit !== 'boolean') {
127136
return handleHttpBadRequest(res, 'Invalid isForfeit value. Must be true or false.');
128137
}
129138

130139
try {
140+
// Fetch room details to verify access and room status
131141
const room = await findRoomById(roomId, userId);
132142
if (!room) {
133143
return handleHttpNotFound(res, 'Room not found');
134144
}
135145

146+
// Update the user's isForfeit status in the room
136147
const updatedRoom: Room | null = await updateRoomUserStatus(roomId, userId, isForfeit);
137148
if (!updatedRoom) {
138149
return handleHttpNotFound(res, 'User not found in room');
139150
}
140151

152+
// Record the forfeited status in the user's history
141153
await produceUpdateHistory(roomId, userId, HistoryStatus.FORFEITED);
154+
155+
// Check if all users in the room have forfeited
142156
const allUsersForfeited = updatedRoom.users.every(user => user.isForfeit === true);
143157
if (allUsersForfeited) {
158+
// Close the room if both users have forfeited
144159
const result = await closeRoomById(roomId);
145160
if (result.modifiedCount === 0) {
146161
return handleHttpNotFound(res, 'Room not found');
147162
}
163+
164+
// Delete the Yjs document associated with the room
148165
await deleteYjsDocument(roomId);
149166
console.log(`Room ${roomId} closed and Yjs document removed`);
167+
150168
return handleHttpSuccess(res, {
151169
message: 'Both users forfeited. Room has been closed.',
152170
room: updatedRoom,
153171
});
154172
}
155173

174+
// Return success if only the single user's isForfeit status was updated
156175
return handleHttpSuccess(res, {
157176
message: 'User isForfeit status updated successfully',
158177
room: updatedRoom,

0 commit comments

Comments
 (0)