Fix alert system to stop when queues become empty
- Enhance checkForNewCalls to monitor queue emptiness and auto-stop alerts - Update startAlert to verify waiting calls exist before starting - Add continuous monitoring to stop alerts when no calls remain - Prevent false alerts when calls disconnect before pickup - Add console logging for better alert behavior tracking 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
		@@ -508,22 +508,39 @@
 | 
			
		||||
     * Check for new calls in queues and trigger alerts
 | 
			
		||||
     */
 | 
			
		||||
    function checkForNewCalls(newQueues) {
 | 
			
		||||
        let hasWaitingCalls = false;
 | 
			
		||||
        let newCallDetected = false;
 | 
			
		||||
        
 | 
			
		||||
        newQueues.forEach(function(queue) {
 | 
			
		||||
            const queueId = queue.id;
 | 
			
		||||
            const currentWaiting = parseInt(queue.current_waiting) || 0;
 | 
			
		||||
            const previousWaiting = lastQueueUpdate[queueId] || 0;
 | 
			
		||||
            
 | 
			
		||||
            // Track if any queue has waiting calls
 | 
			
		||||
            if (currentWaiting > 0) {
 | 
			
		||||
                hasWaitingCalls = true;
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // If waiting count increased, we have new calls
 | 
			
		||||
            if (currentWaiting > previousWaiting) {
 | 
			
		||||
                console.log('New call detected in queue:', queue.queue_name);
 | 
			
		||||
                // Trigger alert if enabled
 | 
			
		||||
                if (alertEnabled && !currentCall) {
 | 
			
		||||
                    startAlert();
 | 
			
		||||
                }
 | 
			
		||||
                newCallDetected = true;
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            lastQueueUpdate[queueId] = currentWaiting;
 | 
			
		||||
        });
 | 
			
		||||
        
 | 
			
		||||
        // Manage alerts based on queue state
 | 
			
		||||
        if (alertEnabled && !currentCall) {
 | 
			
		||||
            if (newCallDetected) {
 | 
			
		||||
                // Start alert for new calls
 | 
			
		||||
                startAlert();
 | 
			
		||||
            } else if (!hasWaitingCalls) {
 | 
			
		||||
                // Stop alert if no calls are waiting in any queue
 | 
			
		||||
                console.log('No calls waiting in any queue, stopping alerts');
 | 
			
		||||
                stopAlert();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
@@ -871,13 +888,27 @@
 | 
			
		||||
    function startAlert() {
 | 
			
		||||
        if (!alertEnabled || alertInterval) return;
 | 
			
		||||
        
 | 
			
		||||
        // Check if there are actually waiting calls before starting alert
 | 
			
		||||
        const hasWaitingCalls = userQueues.some(q => parseInt(q.current_waiting) > 0);
 | 
			
		||||
        if (!hasWaitingCalls) {
 | 
			
		||||
            console.log('No waiting calls found, not starting alert');
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Play initial alert
 | 
			
		||||
        playAlertSound();
 | 
			
		||||
        
 | 
			
		||||
        // Repeat every 30 seconds
 | 
			
		||||
        alertInterval = setInterval(function() {
 | 
			
		||||
            if (alertEnabled && !currentCall) {
 | 
			
		||||
                playAlertSound();
 | 
			
		||||
                // Check if there are still waiting calls
 | 
			
		||||
                const stillHasWaitingCalls = userQueues.some(q => parseInt(q.current_waiting) > 0);
 | 
			
		||||
                if (stillHasWaitingCalls) {
 | 
			
		||||
                    playAlertSound();
 | 
			
		||||
                } else {
 | 
			
		||||
                    console.log('No more waiting calls, stopping alert');
 | 
			
		||||
                    stopAlert();
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                stopAlert();
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user