| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Store info about messages/events from chat |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Agent\Controllers; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
use Extendify\Shared\Services\Sanitizer; |
| 12 |
|
| 13 |
/** |
| 14 |
* The controller |
| 15 |
*/ |
| 16 |
|
| 17 |
class WorkflowHistoryController |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Initialize the controller and set up the database table. |
| 21 |
* |
| 22 |
* @return \WP_REST_Response |
| 23 |
*/ |
| 24 |
public static function init() |
| 25 |
{ |
| 26 |
self::setupWorkflowsTable(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get the last 5 workflows |
| 31 |
* |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public static function getWorkflowHistory($user_id = null) |
| 35 |
{ |
| 36 |
global $wpdb; |
| 37 |
$table = $wpdb->prefix . 'extendify_agent_workflows'; |
| 38 |
$user_id = $user_id ?: get_current_user_id(); |
| 39 |
|
| 40 |
$results = $wpdb->get_results( |
| 41 |
$wpdb->prepare( |
| 42 |
"SELECT * FROM $table WHERE user_id = %d AND status = %s ORDER BY created_at DESC LIMIT 5", |
| 43 |
$user_id, |
| 44 |
'completed' |
| 45 |
), |
| 46 |
ARRAY_A |
| 47 |
); |
| 48 |
return array_map(function ($item) { |
| 49 |
return [ |
| 50 |
'workflowId' => $item['workflow_id'], |
| 51 |
'answerId' => $item['answer_id'], |
| 52 |
'summary' => $item['summary'], |
| 53 |
'status' => $item['status'], |
| 54 |
'errorMsg' => $item['error_msg'], |
| 55 |
'agentName' => $item['agent_name'], |
| 56 |
'createdAt' => $item['created_at'], |
| 57 |
]; |
| 58 |
}, $results); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Persist the data |
| 63 |
* |
| 64 |
* @param \WP_REST_Request $request - The request. |
| 65 |
* @return \WP_REST_Response |
| 66 |
*/ |
| 67 |
public static function add($request) |
| 68 |
{ |
| 69 |
global $wpdb; |
| 70 |
$table = $wpdb->prefix . 'extendify_agent_workflows'; |
| 71 |
$user_id = get_current_user_id(); |
| 72 |
|
| 73 |
$wpdb->insert($table, [ |
| 74 |
'workflow_id' => Sanitizer::sanitizeText($request->get_param('workflowId')), |
| 75 |
'answer_id' => Sanitizer::sanitizeText($request->get_param('answerId')), |
| 76 |
'summary' => Sanitizer::sanitizeTextarea($request->get_param('summary')), |
| 77 |
'status' => Sanitizer::sanitizeText($request->get_param('status')), |
| 78 |
'error_msg' => Sanitizer::sanitizeText($request->get_param('errorMsg')), |
| 79 |
'agent_name' => Sanitizer::sanitizeText($request->get_param('agentName')), |
| 80 |
'user_id' => $user_id, |
| 81 |
'created_at' => current_time('mysql'), |
| 82 |
]); |
| 83 |
|
| 84 |
return new \WP_REST_Response(null, 204); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Ensures the custom table exists and is up to date for MySQL. |
| 90 |
* Creates the table if missing, adds missing columns, and ensures an index on user_id. |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
private static function setupWorkflowsTable() |
| 95 |
{ |
| 96 |
global $wpdb; |
| 97 |
$table = $wpdb->prefix . 'extendify_agent_workflows'; |
| 98 |
$columns = [ |
| 99 |
'id' => "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY", |
| 100 |
'workflow_id' => "VARCHAR(64) NOT NULL", |
| 101 |
'answer_id' => "VARCHAR(64) NOT NULL", |
| 102 |
'summary' => "TEXT", |
| 103 |
'error_msg' => "TEXT", |
| 104 |
'agent_name' => "VARCHAR(64) NOT NULL", |
| 105 |
'status' => "VARCHAR(20) NOT NULL", |
| 106 |
'user_id' => "BIGINT UNSIGNED NOT NULL", |
| 107 |
'created_at' => "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP" |
| 108 |
]; |
| 109 |
|
| 110 |
$exists = $wpdb->get_var("SHOW TABLES LIKE '$table'"); |
| 111 |
if (!$exists) { |
| 112 |
$cols = []; |
| 113 |
foreach ($columns as $name => $type) { |
| 114 |
$cols[] = "$name $type"; |
| 115 |
} |
| 116 |
$sql = "CREATE TABLE $table (" . implode(',', $cols) . ", INDEX(user_id)) "; |
| 117 |
$sql .= $wpdb->get_charset_collate() . ";"; |
| 118 |
$wpdb->query($sql); |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$existingCols = array_column($wpdb->get_results("SHOW COLUMNS FROM $table", ARRAY_A), 'Field'); |
| 123 |
foreach ($columns as $name => $type) { |
| 124 |
if (!in_array($name, $existingCols, true)) { |
| 125 |
$wpdb->query("ALTER TABLE $table ADD COLUMN $name $type"); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// Indexes |
| 130 |
$userIndex = $wpdb->get_results("SHOW INDEX FROM $table WHERE Key_name = 'user_id'", ARRAY_A); |
| 131 |
if (empty($userIndex)) { |
| 132 |
$wpdb->query("CREATE INDEX user_id ON $table(user_id)"); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|