diff --git a/admin/class-twp-admin.php b/admin/class-twp-admin.php index df78a7f..fd53f3d 100644 --- a/admin/class-twp-admin.php +++ b/admin/class-twp-admin.php @@ -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)); diff --git a/includes/class-twp-activator.php b/includes/class-twp-activator.php index 8178868..b4787d6 100644 --- a/includes/class-twp-activator.php +++ b/includes/class-twp-activator.php @@ -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';