102 lines
2.7 KiB
PHP
102 lines
2.7 KiB
PHP
<?php
|
|
/**
|
|
* API - Actualizar mensaje programado
|
|
*/
|
|
|
|
require_once '../config/config.php';
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: PUT, POST');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
requireAuthentication();
|
|
|
|
try {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input) {
|
|
throw new Exception('Datos inválidos');
|
|
}
|
|
|
|
$id = isset($input['id']) ? intval($input['id']) : null;
|
|
|
|
if (!$id) {
|
|
throw new Exception('ID de mensaje programado requerido');
|
|
}
|
|
|
|
$db = Database::getInstance();
|
|
|
|
// Verificar que existe
|
|
$existing = $db->fetch("SELECT * FROM scheduled_messages WHERE id = ?", [$id]);
|
|
|
|
if (!$existing) {
|
|
throw new Exception('Mensaje programado no encontrado');
|
|
}
|
|
|
|
// No permitir editar mensajes ya enviados
|
|
if ($existing['status'] === 'sent') {
|
|
throw new Exception('No se puede editar un mensaje ya enviado');
|
|
}
|
|
|
|
// Preparar campos a actualizar
|
|
$updates = [];
|
|
$params = [];
|
|
|
|
if (isset($input['scheduled_date'])) {
|
|
$updates[] = "scheduled_date = ?";
|
|
$params[] = $input['scheduled_date'];
|
|
}
|
|
|
|
if (isset($input['scheduled_time'])) {
|
|
$updates[] = "scheduled_time = ?";
|
|
$params[] = $input['scheduled_time'];
|
|
}
|
|
|
|
if (isset($input['template_parameters'])) {
|
|
$updates[] = "template_parameters = ?";
|
|
$params[] = is_array($input['template_parameters'])
|
|
? json_encode($input['template_parameters'], JSON_UNESCAPED_UNICODE)
|
|
: $input['template_parameters'];
|
|
}
|
|
|
|
if (isset($input['message_content'])) {
|
|
$updates[] = "message_content = ?";
|
|
$params[] = $input['message_content'];
|
|
}
|
|
|
|
if (isset($input['status'])) {
|
|
$updates[] = "status = ?";
|
|
$params[] = $input['status'];
|
|
}
|
|
|
|
if (empty($updates)) {
|
|
throw new Exception('No hay campos para actualizar');
|
|
}
|
|
|
|
// Agregar updated_at
|
|
$updates[] = "updated_at = NOW()";
|
|
|
|
// Agregar ID al final de params
|
|
$params[] = $id;
|
|
|
|
// Ejecutar actualización
|
|
$sql = "UPDATE scheduled_messages SET " . implode(', ', $updates) . " WHERE id = ?";
|
|
$db->execute($sql, $params);
|
|
|
|
writeLog('INFO', "Mensaje programado actualizado: ID={$id}");
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Mensaje programado actualizado exitosamente'
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Error in update_scheduled_message.php: " . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|