137 lines
No EOL
2.9 KiB
JavaScript
137 lines
No EOL
2.9 KiB
JavaScript
// ===============================
|
||
// 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 dell’utente 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);
|
||
} |