Primer commit del sistema separado falta mejorar mucho
This commit is contained in:
85
telegram/api/recipients/edit.php
Executable file
85
telegram/api/recipients/edit.php
Executable file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/**
|
||||
* API - Editar Destinatario Telegram
|
||||
*/
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Habilitar errores para debug (quitar en producción estricta)
|
||||
ini_set('display_errors', 0);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
require_once __DIR__ . '/../../../shared/utils/helpers.php';
|
||||
require_once __DIR__ . '/../../../shared/auth/jwt.php';
|
||||
require_once __DIR__ . '/../../../shared/database/connection.php';
|
||||
|
||||
// Verificar autenticación
|
||||
$userData = JWTAuth::authenticate();
|
||||
if (!$userData) {
|
||||
jsonResponse(['success' => false, 'error' => 'No autenticado'], 401);
|
||||
}
|
||||
|
||||
// Verificar permiso
|
||||
if (!hasPermission('manage_recipients', 'telegram')) {
|
||||
jsonResponse(['success' => false, 'error' => 'No tienes permiso para editar destinatarios de Telegram.'], 403);
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
jsonResponse(['success' => false, 'error' => 'Método no permitido'], 405);
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = $input['id'] ?? null;
|
||||
$nombre = trim($input['nombre'] ?? '');
|
||||
$telegram_id = trim($input['telegram_id'] ?? '');
|
||||
$tipo = trim($input['tipo'] ?? '');
|
||||
|
||||
// Validaciones
|
||||
if (empty($id) || !is_numeric($id)) {
|
||||
jsonResponse(['success' => false, 'error' => 'ID de destinatario inválido'], 400);
|
||||
}
|
||||
if (empty($nombre) || empty($telegram_id) || empty($tipo)) {
|
||||
jsonResponse(['success' => false, 'error' => 'Faltan datos requeridos (nombre, ID de Telegram, tipo)'], 400);
|
||||
}
|
||||
if (!in_array($tipo, ['canal', 'usuario', 'grupo'])) { // Permitir 'grupo' aunque no esté en UI aún
|
||||
jsonResponse(['success' => false, 'error' => 'Tipo de destinatario inválido'], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$db = getDB();
|
||||
|
||||
// 1. Verificar si el destinatario existe y si el usuario tiene permisos
|
||||
$stmt = $db->prepare("SELECT usuario_id FROM destinatarios_telegram WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
$recipient = $stmt->fetch();
|
||||
|
||||
if (!$recipient) {
|
||||
jsonResponse(['success' => false, 'error' => 'Destinatario no encontrado'], 404);
|
||||
}
|
||||
|
||||
// Opcional: Si se implementara la propiedad del destinatario, se verificaría aquí
|
||||
// if ($userData->rol !== 'Admin' && $recipient['usuario_id'] != $userData->userId) {
|
||||
// jsonResponse(['success' => false, 'error' => 'No tiene permisos para editar este destinatario'], 403);
|
||||
// }
|
||||
|
||||
// 2. Verificar duplicidad de telegram_id (excluyendo el propio destinatario)
|
||||
$stmt = $db->prepare("SELECT id FROM destinatarios_telegram WHERE telegram_id = ? AND id != ?");
|
||||
$stmt->execute([$telegram_id, $id]);
|
||||
if ($stmt->fetch()) {
|
||||
jsonResponse(['success' => false, 'error' => 'Ya existe un destinatario con este ID de Telegram'], 409);
|
||||
}
|
||||
|
||||
// 3. Actualizar el destinatario
|
||||
$stmt = $db->prepare("
|
||||
UPDATE destinatarios_telegram
|
||||
SET nombre = ?, telegram_id = ?, tipo = ?
|
||||
WHERE id = ?
|
||||
");
|
||||
$stmt->execute([$nombre, $telegram_id, $tipo, $id]);
|
||||
|
||||
logToFile('telegram/recipients.log', "Destinatario editado: ID={$id}, Nombre={$nombre}, Usuario={$userData->username}");
|
||||
jsonResponse(['success' => true, 'message' => 'Destinatario actualizado correctamente']);
|
||||
|
||||
} catch (Exception $e) {
|
||||
logToFile('telegram/errors.log', "Error editando destinatario: " . $e->getMessage(), 'ERROR');
|
||||
jsonResponse(['success' => false, 'error' => 'Ocurrió un error en el servidor.'], 500);
|
||||
}
|
||||
Reference in New Issue
Block a user