Skip to content

Latest commit

 

History

History
200 lines (162 loc) · 6.35 KB

File metadata and controls

200 lines (162 loc) · 6.35 KB

Group Messaging and Close Chat Fixes

🔧 Issues Fixed

1. Group Messages Not Showing for Mentees

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:

Backend Changes:

  • Added /api/groups/mentee endpoint in server/routes/groupRoutes.js
    • Fetches all groups where mentee is a member
    • Proper role-based access control
    • Error handling and debugging

Frontend Changes:

  • 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

Socket Improvements:

  • 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

2. Close Chat Functionality Enhancement

Problem: Close chat should remove individual chats from sidebar and clear messages properly.

Solutions Implemented:

Smart Chat Removal:

  • 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)

Socket Room Management:

  • Proper room leaving when closing chats
  • Different handling for individual vs group chats
  • Clean message state on chat close

UI Improvements:

  • Enhanced dropdown menu with proper click-outside handling
  • Visual feedback for chat closure
  • URL state management when closing chats

📋 Technical Implementation Details

New API Endpoint

// 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);
});

Enhanced Socket Auto-Join

// 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}`);
            });
        });
}

Smart Message Filtering

// 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...
};

Intelligent Close Chat Logic

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);
};

🧪 Testing Instructions

Test Group Messaging:

  1. As Mentor:

    • Login as mentor
    • Create a group with multiple mentees
    • Send messages in the group
    • Verify messages appear in group chat
  2. 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

Test Close Chat:

  1. 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
  2. Group Chats:

    • Open a group chat
    • Send messages
    • Close the chat
    • Verify group remains in sidebar
    • Verify messages are cleared from view

🔍 Debugging Features Added

Console Logging:

  • Group fetching for mentees
  • Socket room joining/leaving
  • Message reception and filtering
  • Conversation management

Error Handling:

  • API request failures
  • Socket connection issues
  • Missing user profiles
  • Invalid group access

📁 Files Modified

Backend:

  • server/routes/groupRoutes.js - Added mentee groups endpoint
  • server/socket/socketHandlers.js - Enhanced auto-join logic

Frontend:

  • client/src/pages/ChatPage.jsx - Major updates to conversation handling
    • Enhanced fetchConversations() for mentees
    • Improved handleNewMessage() with filtering
    • Smart handleCloseChat() functionality
    • Added comprehensive debugging

✅ Expected Behavior After Fixes

For Mentees:

  • ✅ 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

For Mentors:

  • ✅ 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

System-wide:

  • ✅ 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.