Skip to content

Commit c00b72e

Browse files
authored
Minor Fix To Handle isForfeit Status (#78)
* Update Collaboration Service API Call: - Close room if two users' isForfeit status is true * Update README.md for Collaboration Service
1 parent cf4f491 commit c00b72e

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

services/collaboration/README.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,34 @@ collaboration service. It enables creating rooms, retrieving room details, and m
8080

8181
---
8282

83-
## Get Room IDs by User (JWT Authentication)
83+
## Get Room Details by Room ID
8484

85-
This endpoint retrieves all active room IDs associated with the authenticated user. Only rooms where `room_status`
86-
is `true` will be retrieved.
85+
This endpoint retrieves detailed information for rooms by its Room ID.
8786

8887
- **HTTP Method**: `GET`
89-
- **Endpoint**: `/api/collaboration/room/user/rooms`
88+
- **Endpoint**: `/api/collaboration/room/{roomId}`
9089

9190
### Authorization
9291

93-
This endpoint requires a valid JWT token in the `Authorization` header. The `userId` is derived from the token and is
94-
not provided directly.
92+
This endpoint requires a valid JWT token in the Authorization header.
93+
94+
### Parameters:
95+
96+
- `roomId` (Required) - The unique identifier of the room to retrieve details for.
9597

9698
### Responses:
9799

98100
| Response Code | Explanation |
99101
|-----------------------------|---------------------------------------------|
100-
| 200 (OK) | Success, room IDs retrieved. |
101-
| 404 (Not Found) | No rooms found for the user. |
102+
| 200 (OK) | Success, room details returned. |
103+
| 404 (Not Found) | Room not found. |
102104
| 500 (Internal Server Error) | Unexpected error in the server or database. |
103105

104106
### Command Line Example:
105107

106108
```bash
107-
curl -X GET http://localhost:8080/api/collaboration/room/user/rooms \
108-
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3MjRlOTZlNDNjMmNjNWQ5ODA5NmM2OSIsInVzZXJuYW1lIjoiVGVzdGluZzEiLCJyb2xlIjoidXNlciIsImlhdCI6MTczMDQ3MjMwMywiZXhwIjoxNzMwNTU4NzAzfQ.x92l-NIgWj_dpM-EC-xOKAGB8zrgGAdKbDpAu3UD5vE" \
109+
curl -X GET "http://localhost:8080/api/collaboration/room/67277d28b6335f6dc76f599a" \
110+
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3MjFhNWZiZWFlNjBjOGViMWU1ZWYzNCIsInVzZXJuYW1lIjoiVGVzdGluZzEiLCJyb2xlIjoidXNlciIsImlhdCI6MTczMDQ3MjY2NCwiZXhwIjoxNzMwNTYxMDY0fQ.DF9CaChoG3-UmeZgZG9SlpjtTknVzeVSBAJDJRdqGk0" \
109111
-H "Content-Type: application/json"
110112
```
111113

@@ -114,15 +116,41 @@ curl -X GET http://localhost:8080/api/collaboration/room/user/rooms \
114116
```json
115117
{
116118
"status": "Success",
117-
"data": [
118-
"6724e9d892fb3e9f04c2e280"
119-
]
119+
"data": {
120+
"room_id": "67277d28b6335f6dc76f599a",
121+
"users": [
122+
{
123+
"id": "67277d044012b8a652616454",
124+
"username": "Testing",
125+
"requestId": "67277d2850d5e18cfc11cd79",
126+
"isForfeit": true
127+
},
128+
{
129+
"id": "67277d104012b8a65261645a",
130+
"username": "Testing1",
131+
"requestId": "67277d2450d5e18cfc11cd74",
132+
"isForfeit": true
133+
}
134+
],
135+
"question": {
136+
"_id": "67277cec70d0a5c9b36304fc",
137+
"id": 3,
138+
"description": "Given a roman numeral, convert it to an integer.",
139+
"difficulty": "Easy",
140+
"title": "Roman to Integer",
141+
"topics": [
142+
"Algorithms"
143+
]
144+
},
145+
"createdAt": "2024-11-03T13:39:52.591Z",
146+
"room_status": false
147+
}
120148
}
121149
```
122150

123151
---
124152

125-
## Get Rooms by User ID, Room Status, and User's isForfeit status
153+
## Get Rooms by Room Status and User Forfeit status
126154

127155
This endpoint retrieves the details of rooms associated with the authenticated user, filtered by the specified room
128156
status and isForfeit status using query parameters.

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)