Problem: Mentees couldn't see groups they belong to or receive group messages.
Root Causes:
- Frontend was only fetching individual mentor chat for mentees
- No API endpoint for mentees to get their groups
- Missing group auto-join logic in socket connection
Solutions Implemented:
- Added
/api/groups/menteeendpoint inserver/routes/groupRoutes.js- Fetches all groups where mentee is a member
- Proper role-based access control
- Error handling and debugging
- Updated
fetchConversations()in ChatPage to fetch mentee groups - Added debugging logs to track group fetching
- Enhanced conversation list to include both groups and mentor chat for mentees
- Enhanced auto-join logic in
server/socket/socketHandlers.js - Added detailed logging for mentee group joining
- Improved message broadcasting to ensure all group members receive messages
Problem: Close chat should remove individual chats from sidebar and clear messages properly.
Solutions Implemented:
- Individual chats: Removed from sidebar when closed (for mentors)
- Group chats: Remain in sidebar (groups are persistent)
- Default mentor chat: Preserved for mentees (essential communication)
- Proper room leaving when closing chats
- Different handling for individual vs group chats
- Clean message state on chat close
- Enhanced dropdown menu with proper click-outside handling
- Visual feedback for chat closure
- URL state management when closing chats
// GET /api/groups/mentee
// Returns all groups that the authenticated mentee belongs to
router.get('/mentee', auth, roleCheck(['mentee']), async (req, res) => {
const mentee = await Mentee.findOne({ userId: req.user._id });
const groups = await Group.find({
menteeIds: mentee._id,
isArchived: false
}).populate('mentorId', 'fullName department');
res.json(groups);
});// Auto-join mentees to their groups on connection
if (socket.userRole === 'mentee') {
Mentee.findOne({ userId: socket.userId })
.then(mentee => Group.find({ menteeIds: mentee._id }))
.then(groups => {
groups.forEach(group => {
socket.join(group._id.toString());
console.log(`Mentee auto-joined group ${group.name}`);
});
});
}// Only show messages for current conversation
const handleNewMessage = (message) => {
if (selectedConversation) {
let shouldAddMessage = false;
if (selectedConversation.type === 'group') {
shouldAddMessage = message.conversationId === selectedConversation._id;
} else {
const actualId = selectedConversation._id.replace('individual_', '');
shouldAddMessage = message.conversationId === actualId;
}
if (!shouldAddMessage) return;
}
// Add message to chat...
};const handleCloseChat = () => {
if (selectedConversation?.type === 'individual' && user.role === 'mentor') {
// Remove individual chats from mentor's sidebar
setConversations(prev => prev.filter(conv => conv._id !== selectedConversation._id));
}
// Groups remain in sidebar for persistent access
// Clean up socket rooms and UI state
setSelectedConversation(null);
setMessages([]);
setShowChatMenu(false);
};-
As Mentor:
- Login as mentor
- Create a group with multiple mentees
- Send messages in the group
- Verify messages appear in group chat
-
As Mentee:
- Login as mentee
- Check if groups appear in chat sidebar
- Open group chat and verify previous messages load
- Verify real-time message reception
-
Individual Chats:
- Start individual chat with mentee (as mentor)
- Send some messages
- Click "..." menu → "Close Chat"
- Verify chat disappears from sidebar
- Verify messages are cleared
-
Group Chats:
- Open a group chat
- Send messages
- Close the chat
- Verify group remains in sidebar
- Verify messages are cleared from view
- Group fetching for mentees
- Socket room joining/leaving
- Message reception and filtering
- Conversation management
- API request failures
- Socket connection issues
- Missing user profiles
- Invalid group access
server/routes/groupRoutes.js- Added mentee groups endpointserver/socket/socketHandlers.js- Enhanced auto-join logic
client/src/pages/ChatPage.jsx- Major updates to conversation handling- Enhanced
fetchConversations()for mentees - Improved
handleNewMessage()with filtering - Smart
handleCloseChat()functionality - Added comprehensive debugging
- Enhanced
- ✅ See all groups they belong to in chat sidebar
- ✅ Receive real-time group messages
- ✅ Can participate in group discussions
- ✅ Individual mentor chat always available
- ✅ Close chat clears messages but keeps essential chats
- ✅ Group messages reach all mentees instantly
- ✅ Can start/close individual chats with mentees
- ✅ Individual chats removed from sidebar when closed
- ✅ Group chats remain persistent
- ✅ Proper socket room management
- ✅ Real-time message delivery
- ✅ Proper conversation filtering
- ✅ Clean UI state management
- ✅ Robust error handling
- ✅ Comprehensive debugging information
The fixes ensure that group messaging works seamlessly for both mentors and mentees, while providing intuitive chat management with smart close functionality.