From ccdc35932d052d6019af663c1e981765ffe62d23 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Fri, 3 Jul 2026 14:41:24 -0500 Subject: [PATCH] feat(turnero): add chat plantillas API endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - chat_get_plantillas.php: GET — returns enabled templates from turnero_chat_plantillas JOIN message_templates; auto-creates table - chat_save_plantillas.php: POST {ids:[...]} — replaces all rows in turnero_chat_plantillas using DELETE + INSERT IGNORE in a transaction Co-Authored-By: Claude Sonnet 4.6 --- modules/turnero/api/chat_get_plantillas.php | 30 ++++++++++++++ modules/turnero/api/chat_save_plantillas.php | 42 ++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 modules/turnero/api/chat_get_plantillas.php create mode 100644 modules/turnero/api/chat_save_plantillas.php diff --git a/modules/turnero/api/chat_get_plantillas.php b/modules/turnero/api/chat_get_plantillas.php new file mode 100644 index 0000000..f4a6889 --- /dev/null +++ b/modules/turnero/api/chat_get_plantillas.php @@ -0,0 +1,30 @@ +getConnection(); + + $pdo->exec("CREATE TABLE IF NOT EXISTS turnero_chat_plantillas ( + template_id INT NOT NULL, + PRIMARY KEY (template_id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); + + $rows = $pdo->query( + "SELECT t.id, t.name, t.template_name, t.language_code, t.body_text, + t.header_text, t.header_type, t.footer_text, t.example_parameters, t.status + FROM turnero_chat_plantillas p + JOIN message_templates t ON t.id = p.template_id + ORDER BY t.name ASC" + )->fetchAll(PDO::FETCH_ASSOC); + + echo json_encode(['ok' => true, 'data' => $rows]); +} catch (Throwable $e) { + http_response_code(500); + echo json_encode(['ok' => false, 'error' => $e->getMessage()]); +} diff --git a/modules/turnero/api/chat_save_plantillas.php b/modules/turnero/api/chat_save_plantillas.php new file mode 100644 index 0000000..9bf0bf7 --- /dev/null +++ b/modules/turnero/api/chat_save_plantillas.php @@ -0,0 +1,42 @@ + false, 'error' => 'Método no permitido']); + exit; +} + +try { + $input = json_decode(file_get_contents('php://input'), true) ?: []; + $ids = array_map('intval', $input['ids'] ?? []); + + $pdo = Database::getInstance()->getConnection(); + + $pdo->exec("CREATE TABLE IF NOT EXISTS turnero_chat_plantillas ( + template_id INT NOT NULL, + PRIMARY KEY (template_id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); + + $pdo->beginTransaction(); + $pdo->exec("DELETE FROM turnero_chat_plantillas"); + + if (!empty($ids)) { + $ph = implode(',', array_fill(0, count($ids), '(?)')); + $stmt = $pdo->prepare("INSERT IGNORE INTO turnero_chat_plantillas (template_id) VALUES $ph"); + $stmt->execute($ids); + } + + $pdo->commit(); + echo json_encode(['ok' => true]); +} catch (Throwable $e) { + if (isset($pdo) && $pdo->inTransaction()) $pdo->rollBack(); + http_response_code(500); + echo json_encode(['ok' => false, 'error' => $e->getMessage()]); +}