Skip to content

Commit 8759ff0

Browse files
committed
Clean up useless heartbeat and add chat-history endpoint.
1 parent 295cb00 commit 8759ff0

File tree

4 files changed

+31
-37
lines changed

4 files changed

+31
-37
lines changed

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

Lines changed: 17 additions & 18 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
} from "../model/repository.js";
77
import crypto from "crypto";
88

@@ -45,23 +45,6 @@ export async function getRoomByUser(req, res) {
4545
}
4646
}
4747

48-
// Update heartbeat for a room
49-
export async function updateHeartbeat(req, res) {
50-
const { roomId } = req.params;
51-
52-
if (!roomId) {
53-
return res.status(400).json({ error: "Room ID is required" });
54-
}
55-
56-
const updatedRoom = await heartbeat(roomId);
57-
58-
if (updatedRoom) {
59-
res.status(200).json(updatedRoom);
60-
} else {
61-
res.status(404).json({ error: `Room with ID ${roomId} not found` });
62-
}
63-
}
64-
6548
// Get all rooms
6649
export async function getAllRoomsController(req, res) {
6750
const rooms = await getAllRooms();
@@ -72,3 +55,19 @@ export async function getAllRoomsController(req, res) {
7255
res.status(500).json({ error: "Failed to retrieve rooms" });
7356
}
7457
}
58+
59+
export async function getRoomChatHistory(req, res) {
60+
const { roomId } = req.params;
61+
62+
if (!roomId) {
63+
return res.status(400).json({ error: "Room ID is required" });
64+
}
65+
66+
const room = await fetchRoomChatHistory(roomId);
67+
68+
if (room) {
69+
res.status(200).json(room);
70+
} else {
71+
res.status(404).json({ error: `Room not found for ID: ${roomId}` });
72+
}
73+
}

collab-service/app/model/repository.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,6 @@ export async function getRoomId(user) {
3131
}
3232
}
3333

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

4635
export async function getAllRooms() {
4736
try {
@@ -102,3 +91,14 @@ export async function addMessageToChat(roomId, userId, text) {
10291
throw error;
10392
}
10493
}
94+
95+
96+
export async function fetchRoomChatHistory(roomId) {
97+
try {
98+
const room = await UsersSession.findOne({ roomId });
99+
return room.chatHistory;
100+
} catch (error) {
101+
console.error("Error finding room chat history:", error);
102+
return null;
103+
}
104+
}

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
} from "../controller/collab-controller.js";
88

99
const router = express.Router();
@@ -12,8 +12,8 @@ router.post("/create-room", createRoom);
1212

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

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

17+
router.get("/chat-history/:roomId", getRoomChatHistory);
18+
1919
export default router;

0 commit comments

Comments
 (0)