Skip to content

Commit

Permalink
Minor Fix To Handle isForfeit Status (CS3219-AY2425S1#78)
Browse files Browse the repository at this point in the history
* Update Collaboration Service API Call:
- Close room if two users' isForfeit status is true

* Update README.md for Collaboration Service
  • Loading branch information
KhoonSun47 authored Nov 3, 2024
1 parent cf4f491 commit c00b72e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
56 changes: 42 additions & 14 deletions services/collaboration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

Expand All @@ -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.
Expand Down
18 changes: 16 additions & 2 deletions services/collaboration/src/controllers/roomController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions services/collaboration/src/services/mongodbService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit c00b72e

Please sign in to comment.