Fix missing enqueued_at column in twp_queued_calls table
Database Schema Fixes: - Added enqueued_at column migration to twp_queued_calls table - Updated personal queue query to use COALESCE for column compatibility - Enhanced requeue function to handle both enqueued_at and joined_at columns - Added dynamic column detection for backwards compatibility SQL Error Resolution: - Fixes "Unknown column 'qc.enqueued_at' in 'ORDER BY'" errors - Maintains compatibility with existing installations - Ensures proper queue ordering functionality The code now gracefully handles both old (joined_at) and new (enqueued_at) column structures, with automatic migration adding the missing column. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
		@@ -7347,13 +7347,26 @@ class TWP_Admin {
 | 
			
		||||
            
 | 
			
		||||
            // Add call to our database queue tracking
 | 
			
		||||
            $calls_table = $wpdb->prefix . 'twp_queued_calls';
 | 
			
		||||
            $wpdb->insert($calls_table, [
 | 
			
		||||
            
 | 
			
		||||
            // Use enqueued_at if available, fallback to joined_at for compatibility
 | 
			
		||||
            $insert_data = [
 | 
			
		||||
                'queue_id' => $queue_id,
 | 
			
		||||
                'call_sid' => $call_sid,
 | 
			
		||||
                'from_number' => $call->from,
 | 
			
		||||
                'enqueued_at' => current_time('mysql'),
 | 
			
		||||
                'to_number' => $call->to ?: '',
 | 
			
		||||
                'position' => 1, // Will be updated by queue manager
 | 
			
		||||
                'status' => 'waiting'
 | 
			
		||||
            ]);
 | 
			
		||||
            ];
 | 
			
		||||
            
 | 
			
		||||
            // Check if enqueued_at column exists
 | 
			
		||||
            $columns = $wpdb->get_col("DESCRIBE $calls_table");
 | 
			
		||||
            if (in_array('enqueued_at', $columns)) {
 | 
			
		||||
                $insert_data['enqueued_at'] = current_time('mysql');
 | 
			
		||||
            } else {
 | 
			
		||||
                $insert_data['joined_at'] = current_time('mysql');
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            $wpdb->insert($calls_table, $insert_data);
 | 
			
		||||
            
 | 
			
		||||
            wp_send_json_success(['message' => 'Call requeued successfully']);
 | 
			
		||||
        } catch (Exception $e) {
 | 
			
		||||
@@ -7893,7 +7906,7 @@ class TWP_Admin {
 | 
			
		||||
            JOIN $queues_table q ON qc.queue_id = q.id
 | 
			
		||||
            WHERE q.queue_name = %s 
 | 
			
		||||
            AND qc.status = 'waiting'
 | 
			
		||||
            ORDER BY qc.enqueued_at ASC
 | 
			
		||||
            ORDER BY COALESCE(qc.enqueued_at, qc.joined_at) ASC
 | 
			
		||||
            LIMIT 1
 | 
			
		||||
        ", $queue_name));
 | 
			
		||||
        
 | 
			
		||||
 
 | 
			
		||||
@@ -402,6 +402,13 @@ class TWP_Activator {
 | 
			
		||||
            $wpdb->query("ALTER TABLE $table_queued_calls ADD COLUMN notified_timeout datetime AFTER agent_call_sid");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Add enqueued_at column (some code uses this instead of joined_at)
 | 
			
		||||
        $enqueued_at_exists = $wpdb->get_results("SHOW COLUMNS FROM $table_queued_calls LIKE 'enqueued_at'");
 | 
			
		||||
        if (empty($enqueued_at_exists)) {
 | 
			
		||||
            $wpdb->query("ALTER TABLE $table_queued_calls ADD COLUMN enqueued_at datetime DEFAULT CURRENT_TIMESTAMP AFTER notified_timeout");
 | 
			
		||||
            error_log('TWP: Added enqueued_at column to twp_queued_calls table');
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Fix phone number field lengths in call recordings table for outbound calls
 | 
			
		||||
        $table_recordings = $wpdb->prefix . 'twp_call_recordings';
 | 
			
		||||
        
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user