This commit is contained in:
Lizandro Guarnizo
2026-03-16 18:36:11 -05:00
parent bbfe0417df
commit ded19cbb8d
6 changed files with 416 additions and 5 deletions
+53
View File
@@ -9,6 +9,27 @@ require_once '../config/config.php';
// Verificar autenticación
requireAuthentication();
// Asegurar que la tabla de historial existe
try {
Database::getInstance()->getConnection()->exec("
CREATE TABLE IF NOT EXISTS broadcast_history (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
sent_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
message_type VARCHAR(20) NOT NULL DEFAULT 'text',
selection_type VARCHAR(20) NOT NULL DEFAULT 'filter',
filter_used VARCHAR(50) NULL,
template_name VARCHAR(120) NULL,
message_preview TEXT NULL,
total_users INT UNSIGNED NOT NULL DEFAULT 0,
sent_count INT UNSIGNED NOT NULL DEFAULT 0,
error_count INT UNSIGNED NOT NULL DEFAULT 0,
sent_by INT UNSIGNED NULL,
sent_by_name VARCHAR(120) NULL,
INDEX idx_sent_at (sent_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
");
} catch (Exception $_e) { /* silencioso si ya existe */ }
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
@@ -271,6 +292,38 @@ try {
}
}
// --- Guardar historial de broadcast ---
try {
$adminId = (int)($_SESSION['admin_user']['id'] ?? $_SESSION['user_id'] ?? 0);
$adminName = $_SESSION['admin_user']['name'] ?? $_SESSION['username'] ?? 'Sistema';
$tplName = null;
$preview = '';
if ($messageType === 'template' && isset($template)) {
$tplName = $template['template_name'] ?? $template['name'] ?? null;
$preview = mb_substr($template['body'] ?? $tplName ?? '', 0, 200);
} else {
$preview = mb_substr($input['message'] ?? '', 0, 200);
}
$db->insert('broadcast_history', [
'sent_at' => date('Y-m-d H:i:s'),
'message_type' => $messageType,
'selection_type' => $selectionType,
'filter_used' => ($selectionType === 'filter') ? ($input['filter'] ?? 'all') : null,
'template_name' => $tplName,
'message_preview' => $preview,
'total_users' => count($users),
'sent_count' => $sentCount,
'error_count' => $errorCount,
'sent_by' => $adminId ?: null,
'sent_by_name' => $adminName ?: null,
]);
} catch (Exception $eHist) {
error_log('broadcast_history insert failed: ' . $eHist->getMessage());
}
// --- Fin historial ---
echo json_encode([
'success' => true,
'sent_count' => $sentCount,