Skip to content

Commit 0fc40fe

Browse files
committed
Update Collaboration Service API Call:
- Close room if two users' isForfeit status is true
1 parent cf4f491 commit 0fc40fe

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

services/collaboration/src/controllers/roomController.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export const closeRoomController = async (req: Request, res: Response) => {
106106
};
107107

108108
/**
109-
* Controller function to update user status in a room
109+
* Controller function to update user isForfeit status in a room
110110
* @param req
111111
* @param res
112112
*/
@@ -125,11 +125,25 @@ export const updateUserStatusInRoomController = async (req: Request, res: Respon
125125
return handleHttpNotFound(res, 'Room not found');
126126
}
127127

128-
const updatedRoom = await updateRoomUserStatus(roomId, userId, isForfeit);
128+
const updatedRoom: Room | null = await updateRoomUserStatus(roomId, userId, isForfeit);
129129
if (!updatedRoom) {
130130
return handleHttpNotFound(res, 'User not found in room');
131131
}
132132

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+
133147
return handleHttpSuccess(res, {
134148
message: 'User isForfeit status updated successfully',
135149
room: updatedRoom,

services/collaboration/src/services/mongodbService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export const deleteYjsDocument = async (roomId: string) => {
145145
* Find rooms by user ID and room status
146146
* @param userId
147147
* @param roomStatus
148+
* @param isForfeit
148149
* @returns
149150
*/
150151
export const findRoomsByUserId = async (

0 commit comments

Comments
 (0)