Skip to content

Commit 436888a

Browse files
authored
Merge pull request #186 from CS3219-AY2425S1/feat/collab-service/add-chathistory
Clean up useless heartbeat and add chat-history endpoint.
2 parents e500d98 + ced23b8 commit 436888a

File tree

4 files changed

+25
-39
lines changed

4 files changed

+25
-39
lines changed

collab-service/app/controller/collab-controller.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
newRoom,
33
getRoomId,
4-
heartbeat,
54
getAllRooms,
5+
fetchRoomChatHistory,
66
getQuestionIdByRoomId,
77
} from "../model/repository.js";
88
import crypto from "crypto";
@@ -47,23 +47,6 @@ export async function getRoomByUser(req, res) {
4747
}
4848
}
4949

50-
// Update heartbeat for a room
51-
export async function updateHeartbeat(req, res) {
52-
const { roomId } = req.params;
53-
54-
if (!roomId) {
55-
return res.status(400).json({ error: "Room ID is required" });
56-
}
57-
58-
const updatedRoom = await heartbeat(roomId);
59-
60-
if (updatedRoom) {
61-
res.status(200).json(updatedRoom);
62-
} else {
63-
res.status(404).json({ error: `Room with ID ${roomId} not found` });
64-
}
65-
}
66-
6750
// Get all rooms
6851
export async function getAllRoomsController(req, res) {
6952
const rooms = await getAllRooms();
@@ -75,14 +58,24 @@ export async function getAllRoomsController(req, res) {
7558
}
7659
}
7760

78-
// Get QuestionId from the room based on the roomId
79-
export async function getQuestionId(req, res) {
61+
export async function getRoomChatHistory(req, res) {
8062
const { roomId } = req.params;
8163

8264
if (!roomId) {
8365
return res.status(400).json({ error: "Room ID is required" });
8466
}
8567

68+
const room = await fetchRoomChatHistory(roomId);
69+
70+
if (room) {
71+
res.status(200).json(room);
72+
} else {
73+
res.status(404).json({ error: `Room not found for ID: ${roomId}` });
74+
}
75+
}
76+
77+
// Get QuestionId from the room based on the roomId
78+
export async function getQuestionId(req, res) {
8679
const questionId = await getQuestionIdByRoomId(roomId);
8780

8881
if (questionId) {

collab-service/app/model/repository.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ export async function getRoomId(user) {
3535
}
3636
}
3737

38-
export async function heartbeat(roomId) {
39-
try {
40-
const room = await UsersSession.findOne({ roomId: roomId });
41-
room.lastUpdated = new Date();
42-
await room.save();
43-
return room;
44-
} catch (error) {
45-
console.error("Error updating room ${roomId}:", error);
46-
return null;
47-
}
48-
}
4938

5039
export async function getAllRooms() {
5140
try {
@@ -107,6 +96,15 @@ export async function addMessageToChat(roomId, userId, text) {
10796
}
10897
}
10998

99+
export async function fetchRoomChatHistory(roomId) {
100+
try {
101+
const room = await UsersSession.findOne({ roomId });
102+
return room.chatHistory;
103+
} catch (error) {
104+
console.error("Error finding room chat history:", error);
105+
}
106+
}
107+
110108
export async function getQuestionIdByRoomId(roomId) {
111109
try {
112110
const room = await UsersSession.findOne({ roomId });

collab-service/app/model/usersSession-model.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ const messageSchema = new Schema({
1515
type: String,
1616
required: true,
1717
},
18-
// timestamp: {
19-
// type: Date,
20-
// default: Date.now,
21-
// required: true
22-
// }
2318
});
2419

2520
const usersSessionSchema = new Schema({

collab-service/app/routes/collab-routes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import express from "express";
22
import {
33
createRoom,
44
getRoomByUser,
5-
updateHeartbeat,
65
getAllRoomsController,
6+
getRoomChatHistory,
77
getQuestionId
88
} from "../controller/collab-controller.js";
99

@@ -13,10 +13,10 @@ router.post("/create-room", createRoom);
1313

1414
router.get("/user/:user", getRoomByUser);
1515

16-
router.patch("/heartbeat/:roomId", updateHeartbeat);
17-
1816
router.get("/rooms", getAllRoomsController);
1917

18+
router.get("/chat-history/:roomId", getRoomChatHistory);
19+
2020
router.get("/rooms/:roomId/questionId", getQuestionId);
2121

2222
export default router;

0 commit comments

Comments
 (0)