388 lines
11 KiB
JavaScript
388 lines
11 KiB
JavaScript
// public/js/admin/db.js
|
|
// ===============================
|
|
// JWT PARSER
|
|
// ===============================
|
|
function parseJwt(t) {
|
|
try {
|
|
return JSON.parse(atob(t.split('.')[1]));
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
// ===============================
|
|
// COMPATTA OUTPUT ATTIVITÀ ADMIN
|
|
// track_preview viene mostrato solo come numero punti
|
|
// ===============================
|
|
function compactActivitiesForOutput(data) {
|
|
if (!Array.isArray(data)) return data;
|
|
|
|
return data.map(a => {
|
|
const previewLen = Array.isArray(a.track_preview)
|
|
? a.track_preview.length
|
|
: 0;
|
|
|
|
return {
|
|
...a,
|
|
track_preview: previewLen
|
|
};
|
|
});
|
|
}
|
|
|
|
// ===============================
|
|
// LETTURA DB FOTO — utente corrente
|
|
// ===============================
|
|
async function readDBUser() {
|
|
const photos = await apiGet(`/photos`);
|
|
out(photos);
|
|
}
|
|
|
|
// ===============================
|
|
// LETTURA DB FOTO — Admin
|
|
// ===============================
|
|
async function readDB() {
|
|
Admin.db = await apiGet(`/photos?user=Admin`);
|
|
out(Admin.db);
|
|
}
|
|
|
|
// ===============================
|
|
// RESET COMPLETO DB FOTO
|
|
// ===============================
|
|
async function resetDB() {
|
|
if (!confirm("Vuoi davvero resettare il DB foto?")) {
|
|
return;
|
|
}
|
|
|
|
await apiPost(`/photos/initDB`);
|
|
|
|
localStorage.removeItem("photosCache");
|
|
localStorage.removeItem("lastSyncPhotos");
|
|
|
|
await readDB();
|
|
}
|
|
|
|
// ===============================
|
|
// RESET DB FOTO PER UTENTE
|
|
// ===============================
|
|
async function resetDBuser() {
|
|
const payload = parseJwt(Admin.token);
|
|
let userToReset = payload.name;
|
|
|
|
if (payload.name === "Admin") {
|
|
const sel = document.getElementById("userSelect");
|
|
|
|
if (!sel || !sel.value) {
|
|
alert("Seleziona un utente");
|
|
return;
|
|
}
|
|
|
|
userToReset = sel.value;
|
|
}
|
|
|
|
if (!confirm(`Vuoi davvero resettare l'utente "${userToReset}"?`)) {
|
|
return;
|
|
}
|
|
|
|
let path = `/photos/initDBuser`;
|
|
|
|
if (payload.name === "Admin") {
|
|
path += `?user=${encodeURIComponent(userToReset)}`;
|
|
}
|
|
|
|
await apiPost(path);
|
|
|
|
localStorage.removeItem("photosCache");
|
|
localStorage.removeItem("lastSyncPhotos");
|
|
|
|
await readDB();
|
|
|
|
const outData = await apiGet(`/photos?user=${encodeURIComponent(userToReset)}`);
|
|
out(outData);
|
|
}
|
|
|
|
// ===============================
|
|
// CANCELLA FOTO PER ID — HARD DELETE
|
|
// ===============================
|
|
async function deletePhoto() {
|
|
const id = prompt("ID foto da cancellare definitivamente:");
|
|
if (!id) return;
|
|
|
|
if (!confirm(`Vuoi davvero cancellare definitivamente la foto "${id}"?`)) {
|
|
return;
|
|
}
|
|
|
|
const data = await apiDelete(`/photos/${id}`);
|
|
out(data);
|
|
|
|
try {
|
|
if (window.AdminCacheSync?.removePhotoFromCache) {
|
|
const result = AdminCacheSync.removePhotoFromCache(id);
|
|
console.log("[admin delete foto] photosCache aggiornata:", result);
|
|
} else {
|
|
localStorage.setItem("photosNeedsSync", "1");
|
|
console.warn("[admin delete foto] AdminCacheSync non disponibile, imposto photosNeedsSync");
|
|
}
|
|
} catch (err) {
|
|
localStorage.setItem("photosNeedsSync", "1");
|
|
console.warn("[admin delete foto] aggiornamento cache fallito:", err.message);
|
|
}
|
|
|
|
//await readDBUser();
|
|
}
|
|
|
|
// ===============================
|
|
// TOGGLE SOFT DELETE FOTO PER ID
|
|
// ===============================
|
|
async function toggleSoftDelete() {
|
|
const id = prompt("ID foto da togglare soft delete:");
|
|
if (!id) return;
|
|
|
|
const data = await apiPost(`/photos/${id}/toggle-soft`);
|
|
out(data);
|
|
|
|
try {
|
|
if (window.AdminCacheSync?.progressiveSyncPhotosCacheFromAdmin) {
|
|
const result = await AdminCacheSync.progressiveSyncPhotosCacheFromAdmin();
|
|
console.log("[admin toggle soft foto] photosCache aggiornata:", result);
|
|
} else {
|
|
localStorage.setItem("photosNeedsSync", "1");
|
|
console.warn("[admin toggle soft foto] AdminCacheSync non disponibile, imposto photosNeedsSync");
|
|
}
|
|
} catch (err) {
|
|
localStorage.setItem("photosNeedsSync", "1");
|
|
console.warn("[admin toggle soft foto] sync cache fallita:", err.message);
|
|
}
|
|
|
|
// await readDBUser();
|
|
}
|
|
|
|
// ===============================
|
|
// CERCA ID IN index.json — foto
|
|
// ===============================
|
|
async function findIdIndex() {
|
|
const id = prompt("ID da cercare:");
|
|
if (!id) return;
|
|
|
|
const data = await apiGet(`/findIdIndex/${id}`);
|
|
out(data);
|
|
}
|
|
|
|
// ===============================
|
|
// CERCA FOTO PER ID
|
|
// ===============================
|
|
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 FOTO
|
|
// ===============================
|
|
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);
|
|
}
|
|
|
|
// ======================================================
|
|
// SEZIONE ATTIVITÀ
|
|
// ======================================================
|
|
|
|
// ===============================
|
|
// LETTURA DB ATTIVITÀ — utente corrente
|
|
// ===============================
|
|
async function readActivitiesDBUser() {
|
|
const activities = await apiGet(`/activities`);
|
|
outActivities(compactActivitiesForOutput(activities));
|
|
}
|
|
|
|
// ===============================
|
|
// LETTURA DB ATTIVITÀ — Admin
|
|
// ===============================
|
|
async function readActivitiesDB() {
|
|
Admin.dbActivities = await apiGet(`/activities?user=Admin`);
|
|
outActivities(compactActivitiesForOutput(Admin.dbActivities));
|
|
}
|
|
|
|
// ===============================
|
|
// RESET COMPLETO DB ATTIVITÀ
|
|
// ===============================
|
|
async function resetActivitiesDB() {
|
|
if (!confirm("Vuoi davvero resettare il DB attività?")) {
|
|
return;
|
|
}
|
|
|
|
await apiPost(`/activities/initDB`);
|
|
|
|
localStorage.removeItem("activitiesCache");
|
|
localStorage.removeItem("lastSyncActivities");
|
|
|
|
await readActivitiesDBUser();
|
|
}
|
|
|
|
// ===============================
|
|
// RESET DB ATTIVITÀ PER UTENTE
|
|
// ===============================
|
|
async function resetActivitiesDBuser() {
|
|
const payload = parseJwt(Admin.token);
|
|
let userToReset = payload.name;
|
|
|
|
if (payload.name === "Admin") {
|
|
const sel = document.getElementById("userSelectActivities");
|
|
|
|
if (!sel || !sel.value) {
|
|
alert("Seleziona un utente");
|
|
return;
|
|
}
|
|
|
|
userToReset = sel.value;
|
|
}
|
|
|
|
if (!confirm(`Vuoi davvero resettare le attività dell'utente "${userToReset}"?`)) {
|
|
return;
|
|
}
|
|
|
|
if (payload.name === "Admin") {
|
|
await apiPost(`/activities/initDBuser?user=${encodeURIComponent(userToReset)}`);
|
|
} else {
|
|
await apiPost(`/activities/initDB`);
|
|
}
|
|
|
|
localStorage.removeItem("activitiesCache");
|
|
localStorage.removeItem("lastSyncActivities");
|
|
|
|
const outData = await apiGet(`/activities`);
|
|
outActivities(compactActivitiesForOutput(outData));
|
|
}
|
|
|
|
// ===============================
|
|
// CANCELLA ATTIVITÀ PER ID — HARD DELETE
|
|
// ===============================
|
|
async function deleteActivity() {
|
|
const id = prompt("ID attività da cancellare definitivamente:");
|
|
if (!id) return;
|
|
|
|
if (!confirm(`Vuoi davvero cancellare definitivamente l'attività "${id}"?`)) {
|
|
return;
|
|
}
|
|
|
|
const data = await apiDelete(`/activities/${id}`);
|
|
outActivities(compactActivitiesForOutput(Array.isArray(data) ? data : [data]));
|
|
|
|
try {
|
|
if (window.AdminCacheSync?.removeActivityFromCache) {
|
|
const result = AdminCacheSync.removeActivityFromCache(id);
|
|
console.log("[admin delete attività] activitiesCache aggiornata:", result);
|
|
} else {
|
|
localStorage.setItem("activitiesNeedsSync", "1");
|
|
console.warn("[admin delete attività] AdminCacheSync non disponibile, imposto activitiesNeedsSync");
|
|
}
|
|
} catch (err) {
|
|
localStorage.setItem("activitiesNeedsSync", "1");
|
|
console.warn("[admin delete attività] aggiornamento cache fallito:", err.message);
|
|
}
|
|
|
|
// await readActivitiesDBUser();
|
|
}
|
|
|
|
// ===============================
|
|
// TOGGLE SOFT DELETE ATTIVITÀ PER ID
|
|
// ===============================
|
|
async function toggleSoftDeleteActivity() {
|
|
const id = prompt("ID attività da togglare soft delete:");
|
|
if (!id) return;
|
|
|
|
const data = await apiPost(`/activities/${id}/toggle-soft`);
|
|
outActivities(compactActivitiesForOutput(Array.isArray(data) ? data : [data]));
|
|
|
|
try {
|
|
if (window.AdminCacheSync?.progressiveSyncActivitiesCacheFromAdmin) {
|
|
const result = await AdminCacheSync.progressiveSyncActivitiesCacheFromAdmin();
|
|
console.log("[admin toggle soft attività] activitiesCache aggiornata:", result);
|
|
} else {
|
|
localStorage.setItem("activitiesNeedsSync", "1");
|
|
console.warn("[admin toggle soft attività] AdminCacheSync non disponibile, imposto activitiesNeedsSync");
|
|
}
|
|
} catch (err) {
|
|
localStorage.setItem("activitiesNeedsSync", "1");
|
|
console.warn("[admin toggle soft attività] sync cache fallita:", err.message);
|
|
}
|
|
|
|
// await readActivitiesDBUser();
|
|
}
|
|
|
|
// ===============================
|
|
// CERCA ATTIVITÀ PER ID
|
|
// ===============================
|
|
async function searchActivityById() {
|
|
const id = prompt("ID attività:");
|
|
if (!id) return;
|
|
|
|
const user = parseJwt(Admin.token).name;
|
|
|
|
const data = await apiGet(`/activities/byIds?id=${id}&user=${user}`);
|
|
outActivities(compactActivitiesForOutput(data));
|
|
}
|
|
|
|
// ===============================
|
|
// MOSTRA CAMBIAMENTI ATTIVITÀ
|
|
// ===============================
|
|
async function showActivitiesChanges() {
|
|
const since = document.getElementById("sinceInputActivities").value;
|
|
if (!since) return alert("Seleziona una data");
|
|
|
|
const iso = new Date(since).toISOString();
|
|
const user = parseJwt(Admin.token).name;
|
|
|
|
const data = await apiGet(`/activities/changes?since=${iso}&user=${user}`);
|
|
outActivities(compactActivitiesForOutput(data));
|
|
}
|
|
|
|
// ===============================
|
|
// MOSTRA HARD DELETED ATTIVITÀ
|
|
// ===============================
|
|
async function showActivitiesHardDeleted() {
|
|
const since = document.getElementById("sinceInputActivities").value;
|
|
if (!since) return alert("Seleziona una data");
|
|
|
|
const iso = new Date(since).toISOString();
|
|
const user = parseJwt(Admin.token).name;
|
|
|
|
const data = await apiGet(`/activities/deleted_hard?since=${iso}&user=${user}`);
|
|
outActivities(data);
|
|
}
|
|
|
|
// ===============================
|
|
// EXPORT
|
|
// ===============================
|
|
window.AdminDB = {
|
|
readDBUser,
|
|
readDB,
|
|
resetDB,
|
|
resetDBuser,
|
|
deletePhoto,
|
|
toggleSoftDelete,
|
|
findIdIndex,
|
|
searchPhotoById,
|
|
showChanges,
|
|
|
|
readActivitiesDBUser,
|
|
readActivitiesDB,
|
|
resetActivitiesDB,
|
|
resetActivitiesDBuser,
|
|
deleteActivity,
|
|
toggleSoftDeleteActivity,
|
|
searchActivityById,
|
|
showActivitiesChanges,
|
|
showActivitiesHardDeleted
|
|
};
|