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:
2025-08-15 10:28:39 -07:00
parent 872182a393
commit 9832f899c3

View File

@@ -508,22 +508,39 @@
* Check for new calls in queues and trigger alerts * Check for new calls in queues and trigger alerts
*/ */
function checkForNewCalls(newQueues) { function checkForNewCalls(newQueues) {
let hasWaitingCalls = false;
let newCallDetected = false;
newQueues.forEach(function(queue) { newQueues.forEach(function(queue) {
const queueId = queue.id; const queueId = queue.id;
const currentWaiting = parseInt(queue.current_waiting) || 0; const currentWaiting = parseInt(queue.current_waiting) || 0;
const previousWaiting = lastQueueUpdate[queueId] || 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 waiting count increased, we have new calls
if (currentWaiting > previousWaiting) { if (currentWaiting > previousWaiting) {
console.log('New call detected in queue:', queue.queue_name); console.log('New call detected in queue:', queue.queue_name);
// Trigger alert if enabled newCallDetected = true;
if (alertEnabled && !currentCall) {
startAlert();
}
} }
lastQueueUpdate[queueId] = currentWaiting; 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() { function startAlert() {
if (!alertEnabled || alertInterval) return; 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 // Play initial alert
playAlertSound(); playAlertSound();
// Repeat every 30 seconds // Repeat every 30 seconds
alertInterval = setInterval(function() { alertInterval = setInterval(function() {
if (alertEnabled && !currentCall) { if (alertEnabled && !currentCall) {
// Check if there are still waiting calls
const stillHasWaitingCalls = userQueues.some(q => parseInt(q.current_waiting) > 0);
if (stillHasWaitingCalls) {
playAlertSound(); playAlertSound();
} else {
console.log('No more waiting calls, stopping alert');
stopAlert();
}
} else { } else {
stopAlert(); stopAlert();
} }