-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuperadmin_api.php
More file actions
132 lines (111 loc) · 5.07 KB
/
superadmin_api.php
File metadata and controls
132 lines (111 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
error_reporting(0);
header("Content-Type: application/json; charset=utf-8");
require_once dirname(__FILE__) . "/auth_admin.php";
require_once dirname(__FILE__) . "/funcoes_cliente.php"; // Para obter token do cliente
// Apenas super admins podem usar esta API
if (!verificarSuperAdmin()) {
http_response_code(403); // Forbidden
echo json_encode(["sucesso" => false, "mensagem" => "Acesso negado."]);
exit;
}
// Função para notificar o n8n para apagar o grupo
function notificarN8nParaApagarGrupo($groupId) {
if (empty($groupId)) {
return;
}
$webhookUrl = 'https://n8n-webhook.startingpro.com.br/webhook/16d3b478-8c0b-4133-bce5-f0c428b3d09e';
$payload = json_encode(['groupId' => $groupId]);
$ch = curl_init($webhookUrl);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_TIMEOUT => 5
]);
curl_exec($ch);
curl_close($ch);
}
function responder($sucesso, $mensagem, $dados = null) {
$response = ["sucesso" => $sucesso, "mensagem" => $mensagem];
if ($dados) $response = array_merge($response, $dados);
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? '';
switch ($action) {
case 'listar_todos_eventos':
$eventosFile = dirname(__FILE__) . "/eventos_cadastrados.json";
$clientesFile = dirname(__FILE__) . "/clientes.json";
$todosEventos = file_exists($eventosFile) ? json_decode(file_get_contents($eventosFile), true) : [];
$todosClientes = file_exists($clientesFile) ? json_decode(file_get_contents($clientesFile), true) : [];
// Criar um mapa de email para token para fácil acesso
$clienteTokenMap = [];
foreach ($todosClientes as $cliente) {
$clienteTokenMap[$cliente['email']] = $cliente['token'];
}
if (!is_array($todosEventos)) {
responder(true, "Nenhum evento no sistema.", ["eventos" => []]);
}
$eventosComStats = array_map(function($evento) use ($clienteTokenMap) {
$totalPessoas = 0;
if (($evento['modelo'] ?? '') === 'lista') {
$convidadosFile = dirname(__FILE__) . '/cadastros_lista/' . $evento['id'] . '.json';
if (file_exists($convidadosFile)) {
$convidados = json_decode(file_get_contents($convidadosFile), true) ?? [];
foreach ($convidados as $cadastro) {
$totalPessoas++;
if (!empty($cadastro['familia'])) {
$totalPessoas += count($cadastro['familia']);
}
}
}
}
$evento['stats'] = ['totalPessoas' => $totalPessoas];
$evento['token_cliente_associado'] = $clienteTokenMap[$evento['organizador_email']] ?? '';
$evento['grupo_whatsapp_id'] = $evento['grupo_whatsapp_id'] ?? null;
return $evento;
}, $todosEventos);
responder(true, "Eventos carregados.", ["eventos" => array_values($eventosComStats)]);
break;
case 'apagar_evento_admin':
$eventoId = $input['eventoId'] ?? '';
if (empty($eventoId)) {
responder(false, "ID do evento não fornecido.");
}
$eventosFile = dirname(__FILE__) . "/eventos_cadastrados.json";
$eventos = file_exists($eventosFile) ? json_decode(file_get_contents($eventosFile), true) : [];
$eventoParaApagar = null;
$eventosFiltrados = array_filter($eventos, function($evento) use ($eventoId, &$eventoParaApagar) {
if (($evento['id'] ?? '') === $eventoId) {
$eventoParaApagar = $evento;
return false; // Remove o evento
}
return true;
});
if (!$eventoParaApagar) {
responder(false, "Evento não encontrado.");
}
// CORREÇÃO: Notifica o n8n para apagar o grupo, se existir um ID associado.
if (!empty($eventoParaApagar['grupo_whatsapp_id'])) {
notificarN8nParaApagarGrupo($eventoParaApagar['grupo_whatsapp_id']);
}
// Salvar a lista de eventos atualizada
file_put_contents($eventosFile, json_encode(array_values($eventosFiltrados), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
// Apagar ficheiros associados
if (!empty($eventoParaApagar['arte_topo']) && file_exists(dirname(__FILE__) . '/' . $eventoParaApagar['arte_topo'])) {
unlink(dirname(__FILE__) . '/' . $eventoParaApagar['arte_topo']);
}
$convidadosFile = dirname(__FILE__) . '/cadastros_lista/' . $eventoId . '.json';
if (file_exists($convidadosFile)) {
unlink($convidadosFile);
}
responder(true, "Evento apagado com sucesso.");
break;
default:
responder(false, "Ação não reconhecida.");
break;
}
?>