@@ -106,7 +106,7 @@ export const closeRoomController = async (req: Request, res: Response) => {
106
106
} ;
107
107
108
108
/**
109
- * Controller function to update user status in a room
109
+ * Controller function to update user isForfeit status in a room
110
110
* @param req
111
111
* @param res
112
112
*/
@@ -125,11 +125,25 @@ export const updateUserStatusInRoomController = async (req: Request, res: Respon
125
125
return handleHttpNotFound ( res , 'Room not found' ) ;
126
126
}
127
127
128
- const updatedRoom = await updateRoomUserStatus ( roomId , userId , isForfeit ) ;
128
+ const updatedRoom : Room | null = await updateRoomUserStatus ( roomId , userId , isForfeit ) ;
129
129
if ( ! updatedRoom ) {
130
130
return handleHttpNotFound ( res , 'User not found in room' ) ;
131
131
}
132
132
133
+ const allUsersForfeited = updatedRoom . users . every ( user => user . isForfeit === true ) ;
134
+ if ( allUsersForfeited ) {
135
+ const result = await closeRoomById ( roomId ) ;
136
+ if ( result . modifiedCount === 0 ) {
137
+ return handleHttpNotFound ( res , 'Room not found' ) ;
138
+ }
139
+ await deleteYjsDocument ( roomId ) ;
140
+ console . log ( `Room ${ roomId } closed and Yjs document removed` ) ;
141
+ return handleHttpSuccess ( res , {
142
+ message : 'Both users forfeited. Room has been closed.' ,
143
+ room : updatedRoom ,
144
+ } ) ;
145
+ }
146
+
133
147
return handleHttpSuccess ( res , {
134
148
message : 'User isForfeit status updated successfully' ,
135
149
room : updatedRoom ,
0 commit comments