diff --git a/services/collaboration/README.md b/services/collaboration/README.md index 75a5c911e4..686c608caf 100644 --- a/services/collaboration/README.md +++ b/services/collaboration/README.md @@ -80,32 +80,34 @@ collaboration service. It enables creating rooms, retrieving room details, and m --- -## Get Room IDs by User (JWT Authentication) +## Get Room Details by Room ID -This endpoint retrieves all active room IDs associated with the authenticated user. Only rooms where `room_status` -is `true` will be retrieved. +This endpoint retrieves detailed information for rooms by its Room ID. - **HTTP Method**: `GET` -- **Endpoint**: `/api/collaboration/room/user/rooms` +- **Endpoint**: `/api/collaboration/room/{roomId}` ### Authorization -This endpoint requires a valid JWT token in the `Authorization` header. The `userId` is derived from the token and is -not provided directly. +This endpoint requires a valid JWT token in the Authorization header. + +### Parameters: + +- `roomId` (Required) - The unique identifier of the room to retrieve details for. ### Responses: | Response Code | Explanation | |-----------------------------|---------------------------------------------| -| 200 (OK) | Success, room IDs retrieved. | -| 404 (Not Found) | No rooms found for the user. | +| 200 (OK) | Success, room details returned. | +| 404 (Not Found) | Room not found. | | 500 (Internal Server Error) | Unexpected error in the server or database. | ### Command Line Example: ```bash -curl -X GET http://localhost:8080/api/collaboration/room/user/rooms \ - -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3MjRlOTZlNDNjMmNjNWQ5ODA5NmM2OSIsInVzZXJuYW1lIjoiVGVzdGluZzEiLCJyb2xlIjoidXNlciIsImlhdCI6MTczMDQ3MjMwMywiZXhwIjoxNzMwNTU4NzAzfQ.x92l-NIgWj_dpM-EC-xOKAGB8zrgGAdKbDpAu3UD5vE" \ +curl -X GET "http://localhost:8080/api/collaboration/room/67277d28b6335f6dc76f599a" \ + -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3MjFhNWZiZWFlNjBjOGViMWU1ZWYzNCIsInVzZXJuYW1lIjoiVGVzdGluZzEiLCJyb2xlIjoidXNlciIsImlhdCI6MTczMDQ3MjY2NCwiZXhwIjoxNzMwNTYxMDY0fQ.DF9CaChoG3-UmeZgZG9SlpjtTknVzeVSBAJDJRdqGk0" \ -H "Content-Type: application/json" ``` @@ -114,15 +116,41 @@ curl -X GET http://localhost:8080/api/collaboration/room/user/rooms \ ```json { "status": "Success", - "data": [ - "6724e9d892fb3e9f04c2e280" - ] + "data": { + "room_id": "67277d28b6335f6dc76f599a", + "users": [ + { + "id": "67277d044012b8a652616454", + "username": "Testing", + "requestId": "67277d2850d5e18cfc11cd79", + "isForfeit": true + }, + { + "id": "67277d104012b8a65261645a", + "username": "Testing1", + "requestId": "67277d2450d5e18cfc11cd74", + "isForfeit": true + } + ], + "question": { + "_id": "67277cec70d0a5c9b36304fc", + "id": 3, + "description": "Given a roman numeral, convert it to an integer.", + "difficulty": "Easy", + "title": "Roman to Integer", + "topics": [ + "Algorithms" + ] + }, + "createdAt": "2024-11-03T13:39:52.591Z", + "room_status": false + } } ``` --- -## Get Rooms by User ID, Room Status, and User's isForfeit status +## Get Rooms by Room Status and User Forfeit status This endpoint retrieves the details of rooms associated with the authenticated user, filtered by the specified room status and isForfeit status using query parameters. diff --git a/services/collaboration/src/controllers/roomController.ts b/services/collaboration/src/controllers/roomController.ts index 9e1a7a0a55..777a495fd3 100644 --- a/services/collaboration/src/controllers/roomController.ts +++ b/services/collaboration/src/controllers/roomController.ts @@ -106,7 +106,7 @@ export const closeRoomController = async (req: Request, res: Response) => { }; /** - * Controller function to update user status in a room + * Controller function to update user isForfeit status in a room * @param req * @param res */ @@ -125,11 +125,25 @@ export const updateUserStatusInRoomController = async (req: Request, res: Respon return handleHttpNotFound(res, 'Room not found'); } - const updatedRoom = await updateRoomUserStatus(roomId, userId, isForfeit); + const updatedRoom: Room | null = await updateRoomUserStatus(roomId, userId, isForfeit); if (!updatedRoom) { return handleHttpNotFound(res, 'User not found in room'); } + const allUsersForfeited = updatedRoom.users.every(user => user.isForfeit === true); + if (allUsersForfeited) { + const result = await closeRoomById(roomId); + if (result.modifiedCount === 0) { + return handleHttpNotFound(res, 'Room not found'); + } + await deleteYjsDocument(roomId); + console.log(`Room ${roomId} closed and Yjs document removed`); + return handleHttpSuccess(res, { + message: 'Both users forfeited. Room has been closed.', + room: updatedRoom, + }); + } + return handleHttpSuccess(res, { message: 'User isForfeit status updated successfully', room: updatedRoom, diff --git a/services/collaboration/src/services/mongodbService.ts b/services/collaboration/src/services/mongodbService.ts index 480c02806d..ec287f4f31 100644 --- a/services/collaboration/src/services/mongodbService.ts +++ b/services/collaboration/src/services/mongodbService.ts @@ -145,6 +145,7 @@ export const deleteYjsDocument = async (roomId: string) => { * Find rooms by user ID and room status * @param userId * @param roomStatus + * @param isForfeit * @returns */ export const findRoomsByUserId = async (