Repository navigation
Hebe/msg persistence - #72
Conversation
✅ Deploy Preview for able-alliance ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Greptile SummaryThis PR adds chat message persistence for socket-based routes: messages are stored in an in-memory
Confidence Score: 4/5Two P1 bugs need fixing before merge: the routeClosed/routeEnded event name mismatch silently breaks client notification, and the hardcoded SuperAdmin ObjectId breaks role-based access for all other super admins. Both P1 findings are present defects on the changed code path — the event name mismatch means clients never get notified when a route ends, and the hardcoded ID means SuperAdmin access control is broken for anyone other than one specific user. Remaining findings are P2 cleanup items that don't block merge. websocket-server/index.mjs — both P1 issues are in this file. Important Files Changed
|
| console.log("Chat log content:" + JSON.stringify(messages)); //TESTING | ||
|
|
||
| chatStore.delete(routeId); | ||
| io.to(room).emit("routeClosed"); |
There was a problem hiding this comment.
Event name mismatch — client never receives route-end notification
The server emits "routeClosed" but the client in socket-test/page.tsx (line 71) listens for "routeEnded". These names don't match, so the client will silently never receive the signal when a route ends.
| io.to(room).emit("routeClosed"); | |
| io.to(room).emit("routeEnded"); |
| const isDriver = route.driver?._id?.toString() === socket.user; | ||
| const isSuperAdmin = socket.user === "69d8392ed589ac8dd451ce65"; | ||
| if (!isDriver && !isSuperAdmin) { |
There was a problem hiding this comment.
Hardcoded SuperAdmin ObjectId is fragile and inconsistent
The auth middleware correctly uses decoded.type === "SuperAdmin" (line 110) to identify super admins, but the endRoute handler hardcodes a specific ObjectId "69d8392ed589ac8dd451ce65". Any other SuperAdmin user will be blocked from ending routes. Store decoded.type on the socket during auth and reuse it here.
// In auth middleware (after line 116):
socket.userType = decoded.type;
// In endRoute handler:
const isSuperAdmin = socket.userType === "SuperAdmin";
| messages, | ||
| }); | ||
| console.log(`Chat log saved for route ${routeId}`); | ||
| console.log("Chat log content:" + JSON.stringify(messages)); //TESTING |
There was a problem hiding this comment.
Debug
console.log with //TESTING comment
This debug line should be removed before merging.
| console.log("Chat log content:" + JSON.stringify(messages)); //TESTING |
Context Used: Fill out this checklist in addition to code commen... (source)
| const routeId = socket.routeId; | ||
| const route = await getRouteForAuth(routeId); | ||
| const senderType = | ||
| route.driver?._id?.toString() === socket.user | ||
| ? "driver" |
There was a problem hiding this comment.
Unnecessary DB call per message to determine sender type
getRouteForAuth hits the database on every sendChatMessage event solely to determine whether the sender is a driver or student. This information is already known at connection time. Store senderType on socket during the auth/connection phase and read it here instead.
…into hebe/msg-persistence
Addresses #35