photo_server_json_con_aves22/public/js/admin/db.js
2026-04-18 20:14:42 +02:00

137 lines
No EOL
2.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ===============================
// JWT PARSER
// ===============================
function parseJwt(t) {
try { return JSON.parse(atob(t.split('.')[1])); }
catch { return {}; }
}
// ===============================
// LETTURA DB UTENTE CORRENTE
// ===============================
async function readDBUser() {
const photos = await apiGet(`/photos`);
out(photos); // mostra attive + eliminate
}
// ===============================
// LETTURA DB ADMIN
// ===============================
async function readDB() {
Admin.db = await apiGet(`/photos?user=Admin`);
out(Admin.db);
}
// ===============================
// RESET COMPLETO DB (solo Admin)
// ===============================
async function resetDB() {
await apiPost(`/initDB`);
await readDB(); // aggiorna UI
}
// ===============================
// RESET DB PER UTENTE
// ===============================
async function resetDBuser() {
const payload = parseJwt(Admin.token);
let userToReset = payload.name;
// Se Admin → usa dropdown
if (payload.name === "Admin") {
const sel = document.getElementById("userSelect");
if (!sel || !sel.value) {
alert("Seleziona un utente");
return;
}
userToReset = sel.value;
}
// Conferma
if (!confirm(`Vuoi davvero resettare l'utente "${userToReset}"?`)) {
return;
}
// Costruisci path per API
let path = `/initDBuser`;
if (payload.name === "Admin") {
path += `?user=${encodeURIComponent(userToReset)}`;
}
// Reset tramite API
await apiGet(path);
// Aggiorna UI
await readDB();
// Mostra output dellutente resettato
const outData = await apiGet(`/photos?user=${encodeURIComponent(userToReset)}`);
out(outData);
}
// ===============================
// CANCELLA FOTO PER ID
// ===============================
async function deletePhoto() {
const id = prompt("ID foto da cancellare:");
if (!id) return;
const data = await apiDelete(`/delphoto/${id}`);
out(data);
await readDB();
}
// ===============================
// CERCA ID IN index.json
// ===============================
async function findIdIndex() {
const id = prompt("ID da cercare:");
if (!id) return;
const data = await apiGet(`/findIdIndex/${id}`);
out(data);
}
// ===============================
// CERCA FOTO PER ID (nuovo /byIds)
// ===============================
async function searchPhotoById() {
const id = prompt("ID foto:");
if (!id) return;
const user = parseJwt(Admin.token).name;
const data = await apiGet(`/photos/byIds?id=${id}&user=${user}`);
out(data);
}
// ===============================
// MOSTRA CAMBIAMENTI
// ===============================
async function showChanges() {
const since = document.getElementById("sinceInput").value;
if (!since) return alert("Seleziona una data");
const iso = new Date(since).toISOString();
const user = parseJwt(Admin.token).name;
const data = await apiGet(`/photos/changes?since=${iso}&user=${user}`);
out(data);
}