182 lines
4.3 KiB
JavaScript
182 lines
4.3 KiB
JavaScript
// public/js/admin/cacheSync.js
|
|
|
|
function mapById(arr) {
|
|
const map = new Map();
|
|
|
|
for (const item of arr || []) {
|
|
if (item && item.id != null) {
|
|
map.set(String(item.id), item);
|
|
}
|
|
}
|
|
|
|
return map;
|
|
}
|
|
|
|
function readLocalArray(cacheKey) {
|
|
try {
|
|
const raw = localStorage.getItem(cacheKey);
|
|
const arr = raw ? JSON.parse(raw) : [];
|
|
|
|
return Array.isArray(arr) ? arr : [];
|
|
} catch (err) {
|
|
console.warn(`[AdminCacheSync] errore lettura ${cacheKey}:`, err.message);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function writeLocalArray(cacheKey, arr) {
|
|
const safeArr = Array.isArray(arr) ? arr : [];
|
|
localStorage.setItem(cacheKey, JSON.stringify(safeArr));
|
|
}
|
|
|
|
function removeItemFromLocalCache(cacheKey, id) {
|
|
if (id == null) {
|
|
return {
|
|
ok: false,
|
|
reason: "missing id",
|
|
cacheKey
|
|
};
|
|
}
|
|
|
|
const arr = readLocalArray(cacheKey);
|
|
const before = arr.length;
|
|
|
|
const next = arr.filter(item => String(item.id) !== String(id));
|
|
|
|
writeLocalArray(cacheKey, next);
|
|
|
|
return {
|
|
ok: true,
|
|
cacheKey,
|
|
id,
|
|
before,
|
|
after: next.length,
|
|
removed: before - next.length
|
|
};
|
|
}
|
|
|
|
function removePhotoFromCache(id) {
|
|
return removeItemFromLocalCache("photosCache", id);
|
|
}
|
|
|
|
function removeActivityFromCache(id) {
|
|
return removeItemFromLocalCache("activitiesCache", id);
|
|
}
|
|
|
|
async function progressiveSyncPhotosCacheFromAdmin() {
|
|
const lastSync = localStorage.getItem("lastSyncPhotos");
|
|
const localPhotos = readLocalArray("photosCache");
|
|
|
|
// Se non ho cache o lastSync, faccio full load foto
|
|
if (!lastSync || localPhotos.length === 0) {
|
|
const photos = await apiGet("/photos");
|
|
|
|
writeLocalArray("photosCache", photos);
|
|
localStorage.setItem("lastSyncPhotos", new Date().toISOString());
|
|
|
|
return {
|
|
mode: "photos",
|
|
type: "full",
|
|
count: Array.isArray(photos) ? photos.length : 0
|
|
};
|
|
}
|
|
|
|
const changed = await apiGet(
|
|
`/photos/changes?since=${encodeURIComponent(lastSync)}`
|
|
);
|
|
|
|
const hardDeletedRes = await apiGet(
|
|
`/photos/deleted_hard?since=${encodeURIComponent(lastSync)}`
|
|
);
|
|
|
|
const hardDeleted = hardDeletedRes.deleted || [];
|
|
const map = mapById(localPhotos);
|
|
|
|
for (const p of changed || []) {
|
|
if (p && p.id != null) {
|
|
map.set(String(p.id), p);
|
|
}
|
|
}
|
|
|
|
for (const d of hardDeleted || []) {
|
|
if (d && d.id != null) {
|
|
map.delete(String(d.id));
|
|
}
|
|
}
|
|
|
|
const merged = Array.from(map.values());
|
|
|
|
writeLocalArray("photosCache", merged);
|
|
localStorage.setItem("lastSyncPhotos", new Date().toISOString());
|
|
|
|
return {
|
|
mode: "photos",
|
|
type: "progressive",
|
|
changed: Array.isArray(changed) ? changed.length : 0,
|
|
hardDeleted: hardDeleted.length,
|
|
count: merged.length
|
|
};
|
|
}
|
|
|
|
async function progressiveSyncActivitiesCacheFromAdmin() {
|
|
const lastSync = localStorage.getItem("lastSyncActivities");
|
|
const localActivities = readLocalArray("activitiesCache");
|
|
|
|
// Se non ho cache o lastSync, faccio full load attività
|
|
if (!lastSync || localActivities.length === 0) {
|
|
const activities = await apiGet("/activities");
|
|
|
|
writeLocalArray("activitiesCache", activities);
|
|
localStorage.setItem("lastSyncActivities", new Date().toISOString());
|
|
|
|
return {
|
|
mode: "activities",
|
|
type: "full",
|
|
count: Array.isArray(activities) ? activities.length : 0
|
|
};
|
|
}
|
|
|
|
const changed = await apiGet(
|
|
`/activities/changes?since=${encodeURIComponent(lastSync)}`
|
|
);
|
|
|
|
const hardDeletedRes = await apiGet(
|
|
`/activities/deleted_hard?since=${encodeURIComponent(lastSync)}`
|
|
);
|
|
|
|
const hardDeleted = hardDeletedRes.deleted || [];
|
|
const map = mapById(localActivities);
|
|
|
|
for (const a of changed || []) {
|
|
if (a && a.id != null) {
|
|
map.set(String(a.id), a);
|
|
}
|
|
}
|
|
|
|
for (const d of hardDeleted || []) {
|
|
if (d && d.id != null) {
|
|
map.delete(String(d.id));
|
|
}
|
|
}
|
|
|
|
const merged = Array.from(map.values());
|
|
|
|
writeLocalArray("activitiesCache", merged);
|
|
localStorage.setItem("lastSyncActivities", new Date().toISOString());
|
|
|
|
return {
|
|
mode: "activities",
|
|
type: "progressive",
|
|
changed: Array.isArray(changed) ? changed.length : 0,
|
|
hardDeleted: hardDeleted.length,
|
|
count: merged.length
|
|
};
|
|
}
|
|
|
|
// Export globale per uso negli altri file admin
|
|
window.AdminCacheSync = {
|
|
progressiveSyncPhotosCacheFromAdmin,
|
|
progressiveSyncActivitiesCacheFromAdmin,
|
|
removePhotoFromCache,
|
|
removeActivityFromCache
|
|
};
|