From 39eb3b96b50b913ccdb97c42517d4d14da21dec9 Mon Sep 17 00:00:00 2001 From: jknapp Date: Tue, 12 Aug 2025 07:21:20 -0700 Subject: [PATCH] testing progress --- admin/class-twp-admin.php | 51 ++++++++++++++++++++++++++++++++++++- includes/class-twp-core.php | 1 + 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/admin/class-twp-admin.php b/admin/class-twp-admin.php index 67682f3..d40e39c 100644 --- a/admin/class-twp-admin.php +++ b/admin/class-twp-admin.php @@ -3729,6 +3729,55 @@ class TWP_Admin { } } + /** + * AJAX handler for accepting next call from a queue + */ + public function ajax_accept_next_queue_call() { + check_ajax_referer('twp_ajax_nonce', 'nonce'); + + $queue_id = intval($_POST['queue_id']); + $user_id = get_current_user_id(); + + global $wpdb; + $calls_table = $wpdb->prefix . 'twp_queued_calls'; + $groups_table = $wpdb->prefix . 'twp_group_members'; + $queues_table = $wpdb->prefix . 'twp_call_queues'; + + // Verify user is a member of this queue's agent group + $is_member = $wpdb->get_var($wpdb->prepare(" + SELECT COUNT(*) + FROM $groups_table gm + JOIN $queues_table q ON gm.group_id = q.agent_group_id + WHERE gm.user_id = %d AND q.id = %d + ", $user_id, $queue_id)); + + if (!$is_member) { + wp_send_json_error('You are not authorized to accept calls from this queue'); + return; + } + + // Get the next waiting call from this queue (lowest position number) + $next_call = $wpdb->get_row($wpdb->prepare(" + SELECT * FROM $calls_table + WHERE queue_id = %d AND status = 'waiting' + ORDER BY position ASC + LIMIT 1 + ", $queue_id)); + + if (!$next_call) { + wp_send_json_error('No calls waiting in this queue'); + return; + } + + $result = TWP_Agent_Manager::accept_queued_call($next_call->id, $user_id); + + if ($result['success']) { + wp_send_json_success($result); + } else { + wp_send_json_error($result['error']); + } + } + /** * AJAX handler for getting waiting calls */ @@ -5685,7 +5734,7 @@ class TWP_Admin { $button.prop('disabled', true).text('Accepting...'); $.post(ajaxurl, { - action: 'twp_accept_call', + action: 'twp_accept_next_queue_call', queue_id: queueId, nonce: '' }, function(response) { diff --git a/includes/class-twp-core.php b/includes/class-twp-core.php index ac410b6..21c09b3 100644 --- a/includes/class-twp-core.php +++ b/includes/class-twp-core.php @@ -122,6 +122,7 @@ class TWP_Core { // Agent queue management AJAX $this->loader->add_action('wp_ajax_twp_accept_call', $plugin_admin, 'ajax_accept_call'); + $this->loader->add_action('wp_ajax_twp_accept_next_queue_call', $plugin_admin, 'ajax_accept_next_queue_call'); $this->loader->add_action('wp_ajax_twp_get_waiting_calls', $plugin_admin, 'ajax_get_waiting_calls'); $this->loader->add_action('wp_ajax_twp_set_agent_status', $plugin_admin, 'ajax_set_agent_status'); $this->loader->add_action('wp_ajax_twp_get_call_details', $plugin_admin, 'ajax_get_call_details');