Skip to content

Commit 870032a

Browse files
authored
Update auditor.html
1 parent a3dfb2d commit 870032a

File tree

1 file changed

+129
-16
lines changed

1 file changed

+129
-16
lines changed

auditor.html

Lines changed: 129 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,27 @@
253253
border-radius: 10px;
254254
margin-top: 12px;
255255
}
256+
257+
.user-info {
258+
position: absolute;
259+
top: 20px;
260+
right: 20px;
261+
background: rgba(212,175,55,0.1);
262+
border: 1px solid rgba(212,175,55,0.3);
263+
border-radius: 10px;
264+
padding: 10px 15px;
265+
font-size: 0.9rem;
266+
color: #d4af37;
267+
}
256268
</style>
257269
</head>
258270
<body>
259271

272+
<!-- Informação do usuário logado -->
273+
<div class="user-info" id="userInfo">
274+
<i class="fas fa-user"></i> Carregando...
275+
</div>
276+
260277
<h1>🎛️ Painel Completo — Terra Dourada</h1>
261278
<div class="subtitle">Sincronização, Auditoria e Controle Total da Urna</div>
262279

@@ -358,6 +375,8 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
358375
// ==============================
359376
const dbVotos = new PouchDB('terra_dourada_votos_local');
360377
const dbAutorizacoes = new PouchDB('terra_dourada_autorizacoes');
378+
const dbEleicoes = new PouchDB('terra_dourada_eleicoes');
379+
const dbConfig = new PouchDB('terra_dourada_config');
361380
const IPFS_GATEWAY = "https://ipfs.io/ipfs/";
362381
let LOGS = [];
363382

@@ -368,6 +387,84 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
368387
branco: "hash_voto_branco_123456789"
369388
};
370389

390+
// ⭐⭐ NOVO: Identificar usuário logado
391+
async function identificarUsuarioLogado() {
392+
try {
393+
console.log('🔍 Identificando usuário logado...');
394+
395+
// Do localStorage
396+
const usuarioData = JSON.parse(localStorage.getItem("usuario_data") || '{}');
397+
if (usuarioData.email) {
398+
console.log('✅ Usuário do localStorage:', usuarioData.email);
399+
return usuarioData;
400+
}
401+
402+
// Do PouchDB
403+
const conta = await dbConfig.get('dono_da_sala');
404+
console.log('✅ Usuário do PouchDB:', conta.email);
405+
return conta;
406+
407+
} catch (error) {
408+
console.error('❌ Erro ao identificar usuário:', error);
409+
return null;
410+
}
411+
}
412+
413+
// ⭐⭐ NOVO: Atualizar info do usuário na tela
414+
function atualizarInfoUsuario(usuario) {
415+
const userInfo = document.getElementById('userInfo');
416+
if (usuario && userInfo) {
417+
userInfo.innerHTML = `<i class="fas fa-user"></i> ${usuario.name || usuario.email} (${usuario.provider})`;
418+
}
419+
}
420+
421+
// ⭐⭐ NOVO: Buscar eleições do usuário logado
422+
async function buscarEleicoesUsuario() {
423+
try {
424+
const usuarioLogado = await identificarUsuarioLogado();
425+
if (!usuarioLogado) return [];
426+
427+
const todasEleicoes = await dbEleicoes.allDocs({ include_docs: true });
428+
const eleicoesDoUsuario = todasEleicoes.rows.filter(row =>
429+
row.doc.ownerId === usuarioLogado.email || row.doc.ownerEmail === usuarioLogado.email
430+
);
431+
432+
console.log(`🏛️ Eleições do usuário: ${eleicoesDoUsuario.length}`);
433+
return eleicoesDoUsuario.map(row => row.doc._id);
434+
435+
} catch (error) {
436+
console.error('Erro ao buscar eleições do usuário:', error);
437+
return [];
438+
}
439+
}
440+
441+
// ⭐⭐ NOVO: Filtrar votos apenas das eleições do usuário
442+
async function filtrarVotosUsuario() {
443+
try {
444+
const idsEleicoesUsuario = await buscarEleicoesUsuario();
445+
446+
// Se não tem eleições, retorna array vazio
447+
if (idsEleicoesUsuario.length === 0) {
448+
return [];
449+
}
450+
451+
const todosVotos = await dbVotos.allDocs({ include_docs: true });
452+
453+
// ✅ FILTRAR: Apenas votos das eleições do usuário
454+
const votosDoUsuario = todosVotos.rows.filter(row => {
455+
const voto = row.doc;
456+
return voto.eleicao_id && idsEleicoesUsuario.includes(voto.eleicao_id);
457+
});
458+
459+
console.log(`🗳️ Votos do usuário: ${votosDoUsuario.length} de ${todosVotos.rows.length}`);
460+
return votosDoUsuario;
461+
462+
} catch (error) {
463+
console.error('Erro ao filtrar votos:', error);
464+
return [];
465+
}
466+
}
467+
371468
// ==============================
372469
// FUNÇÕES AUXILIARES - CORRIGIDAS
373470
// ==============================
@@ -451,11 +548,12 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
451548
// ==============================
452549
async function atualizarDashboard() {
453550
try {
454-
const allVotos = await dbVotos.allDocs({ include_docs: true });
551+
// ✅ CORREÇÃO: Usar apenas votos do usuário
552+
const votosDoUsuario = await filtrarVotosUsuario();
455553

456554
// Processa votos e busca nomes reais dos eleitores
457555
const votosComInfo = await Promise.all(
458-
allVotos.rows.map(async (row) => {
556+
votosDoUsuario.map(async (row) => {
459557
const voto = row.doc;
460558
const eleitorNome = await getEleitorInfo(voto);
461559
return {
@@ -532,8 +630,9 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
532630
// ==============================
533631
async function reenviarPendentes() {
534632
try {
535-
const all = await dbVotos.allDocs({ include_docs: true });
536-
const pendentes = all.rows.map(r => r.doc).filter(doc => doc.erro === "sem_conexao_backend");
633+
// ✅ CORREÇÃO: Usar apenas votos do usuário
634+
const votosDoUsuario = await filtrarVotosUsuario();
635+
const pendentes = votosDoUsuario.map(r => r.doc).filter(doc => doc.erro === "sem_conexao_backend");
537636

538637
if (pendentes.length === 0) {
539638
log('Nenhum voto pendente para sincronizar', 'warning');
@@ -602,10 +701,11 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
602701
// EXPORTAR DADOS - CORRIGIDO
603702
// ==============================
604703
async function exportarDados() {
605-
const all = await dbVotos.allDocs({ include_docs: true });
704+
// ✅ CORREÇÃO: Usar apenas votos do usuário
705+
const votosDoUsuario = await filtrarVotosUsuario();
606706

607707
const dadosExportar = await Promise.all(
608-
all.rows.map(async (row) => {
708+
votosDoUsuario.map(async (row) => {
609709
const doc = row.doc;
610710
const eleitor = await getEleitorInfo(doc);
611711
const candidato = getCandidatoInfo(doc);
@@ -618,7 +718,8 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
618718
tipo: "voto_presidencial",
619719
timestamp: getTimestampInfo(doc),
620720
status: doc.erro ? "pendente" : "enviado",
621-
cid: getCIDInfo(doc)
721+
cid: getCIDInfo(doc),
722+
eleicao_id: doc.eleicao_id || "N/A"
622723
};
623724
})
624725
);
@@ -637,13 +738,14 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
637738
}
638739

639740
// ==============================
640-
// LIMPAR ENVIADOS
741+
// LIMPAR ENVIADOS - CORRIGIDO
641742
// ==============================
642743
async function limparEnviados() {
643744
if (!confirm('Tem certeza que deseja limpar os votos já enviados?')) return;
644745

645-
const all = await dbVotos.allDocs({ include_docs: true });
646-
const enviados = all.rows.map(r => r.doc).filter(doc => doc.cid_pinata && !doc.erro);
746+
// ✅ CORREÇÃO: Usar apenas votos do usuário
747+
const votosDoUsuario = await filtrarVotosUsuario();
748+
const enviados = votosDoUsuario.map(r => r.doc).filter(doc => doc.cid_pinata && !doc.erro);
647749

648750
for (let doc of enviados) {
649751
await dbVotos.remove(doc);
@@ -676,13 +778,14 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
676778
let totalVotos = 0;
677779
let validos = 0;
678780

679-
const result = await dbVotos.allDocs({ include_docs: true });
680-
const docs = result.rows;
781+
// ✅ CORREÇÃO: Usar apenas votos do usuário
782+
const votosDoUsuario = await filtrarVotosUsuario();
783+
const docs = votosDoUsuario.map(row => row.doc);
681784

682785
const total = docs.length;
683786

684787
for (let i = 0; i < total; i++) {
685-
const row = docs[i].doc;
788+
const row = docs[i];
686789

687790
progress.style.width = ((i / total) * 100).toFixed(1) + "%";
688791

@@ -754,8 +857,9 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
754857

755858
logIPFS("🔍 Carregando votos locais do PouchDB...");
756859

757-
const all = await dbVotos.allDocs({ include_docs: true });
758-
const docs = all.rows.slice(0, qtd).map(r => r.doc);
860+
// ✅ CORREÇÃO: Usar apenas votos do usuário
861+
const votosDoUsuario = await filtrarVotosUsuario();
862+
const docs = votosDoUsuario.slice(0, qtd).map(row => row.doc);
759863

760864
logIPFS(`📦 Total carregado: ${docs.length} votos.`);
761865

@@ -894,7 +998,16 @@ <h2>🌐 Auditoria Sovereign - Verificação IPFS</h2>
894998
// ==============================
895999
// INICIALIZAÇÃO
8961000
// ==============================
897-
atualizarDashboard();
1001+
document.addEventListener("DOMContentLoaded", async function() {
1002+
// Identificar e mostrar usuário
1003+
const usuarioLogado = await identificarUsuarioLogado();
1004+
if (usuarioLogado) {
1005+
atualizarInfoUsuario(usuarioLogado);
1006+
}
1007+
1008+
// Inicializar dashboard
1009+
atualizarDashboard();
1010+
});
8981011
</script>
8991012

9001013
</body>

0 commit comments

Comments
 (0)