@@ -73,7 +73,8 @@ export const getRoomByRoomIdController = async (req: Request, res: Response) =>
73
73
} ;
74
74
75
75
/**
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.
77
78
* @param req
78
79
* @param res
79
80
*/
@@ -87,17 +88,22 @@ export const closeRoomController = async (req: Request, res: Response) => {
87
88
return handleHttpNotFound ( res , 'Room not found' ) ;
88
89
}
89
90
91
+ // Check if the room is already closed
90
92
if ( ! room . room_status ) {
91
93
console . log ( `Room ${ roomId } is already closed.` ) ;
92
94
return handleHttpSuccess ( res , `Room ${ roomId } is already closed` ) ;
93
95
}
94
96
97
+ // Close the room by setting its status to false
95
98
const result = await closeRoomById ( roomId ) ;
96
99
if ( result . modifiedCount === 0 ) {
97
100
return handleHttpNotFound ( res , 'Room not found' ) ;
98
101
}
99
102
103
+ // Delete the Yjs document associated with the room
100
104
await deleteYjsDocument ( roomId ) ;
105
+
106
+ // Mark history as completed for users who did not forfeit
101
107
await Promise . all (
102
108
room . users
103
109
. filter ( user => ! user . isForfeit )
@@ -114,7 +120,9 @@ export const closeRoomController = async (req: Request, res: Response) => {
114
120
} ;
115
121
116
122
/**
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.
118
126
* @param req
119
127
* @param res
120
128
*/
@@ -123,36 +131,47 @@ export const updateUserStatusInRoomController = async (req: Request, res: Respon
123
131
const { roomId } = req . params ;
124
132
const { isForfeit } = req . body ;
125
133
134
+ // Validate that isForfeit is a boolean value
126
135
if ( typeof isForfeit !== 'boolean' ) {
127
136
return handleHttpBadRequest ( res , 'Invalid isForfeit value. Must be true or false.' ) ;
128
137
}
129
138
130
139
try {
140
+ // Fetch room details to verify access and room status
131
141
const room = await findRoomById ( roomId , userId ) ;
132
142
if ( ! room ) {
133
143
return handleHttpNotFound ( res , 'Room not found' ) ;
134
144
}
135
145
146
+ // Update the user's isForfeit status in the room
136
147
const updatedRoom : Room | null = await updateRoomUserStatus ( roomId , userId , isForfeit ) ;
137
148
if ( ! updatedRoom ) {
138
149
return handleHttpNotFound ( res , 'User not found in room' ) ;
139
150
}
140
151
152
+ // Record the forfeited status in the user's history
141
153
await produceUpdateHistory ( roomId , userId , HistoryStatus . FORFEITED ) ;
154
+
155
+ // Check if all users in the room have forfeited
142
156
const allUsersForfeited = updatedRoom . users . every ( user => user . isForfeit === true ) ;
143
157
if ( allUsersForfeited ) {
158
+ // Close the room if both users have forfeited
144
159
const result = await closeRoomById ( roomId ) ;
145
160
if ( result . modifiedCount === 0 ) {
146
161
return handleHttpNotFound ( res , 'Room not found' ) ;
147
162
}
163
+
164
+ // Delete the Yjs document associated with the room
148
165
await deleteYjsDocument ( roomId ) ;
149
166
console . log ( `Room ${ roomId } closed and Yjs document removed` ) ;
167
+
150
168
return handleHttpSuccess ( res , {
151
169
message : 'Both users forfeited. Room has been closed.' ,
152
170
room : updatedRoom ,
153
171
} ) ;
154
172
}
155
173
174
+ // Return success if only the single user's isForfeit status was updated
156
175
return handleHttpSuccess ( res , {
157
176
message : 'User isForfeit status updated successfully' ,
158
177
room : updatedRoom ,
0 commit comments