Files
whatsapp/modules/turnero/api/chat_save_plantillas.php
T
Lizandro GuarnizoandClaude Sonnet 4.6 ccdc35932d feat(turnero): add chat plantillas API endpoints
- 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 <noreply@anthropic.com>
2026-07-03 14:41:24 -05:00

43 lines
1.3 KiB
PHP

<?php
/**
* POST — Guarda la lista de plantillas habilitadas para el chat turnero.
* Body: { ids: [1, 2, 3] }
*/
require_once __DIR__ . '/../../../config/config.php';
requireAuthentication();
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['ok' => 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()]);
}