Add soft-delete for students: deleted_at/purge_at columns, trash page, API filters and audit logs
This commit is contained in:
+100
-6
@@ -1,13 +1,17 @@
|
||||
const PAGE_SIZE = 20;
|
||||
let currentEntries = [];
|
||||
let currentGroups = [];
|
||||
let currentStudents = [];
|
||||
let pendingEntries = [];
|
||||
let pendingGroups = [];
|
||||
let pendingStudents = [];
|
||||
let page = 1;
|
||||
let totalEntries = 0;
|
||||
let totalGroups = 0;
|
||||
let totalStudents = 0;
|
||||
let pendingTotalEntries = 0;
|
||||
let pendingTotalGroups = 0;
|
||||
let pendingTotalStudents = 0;
|
||||
let purgeDays = 30;
|
||||
|
||||
function isImageFile(name) {
|
||||
@@ -85,6 +89,35 @@ function pendingGroupHTML(g) {
|
||||
`;
|
||||
}
|
||||
|
||||
function studentHTML(s) {
|
||||
return `\
|
||||
<div class="card">
|
||||
${s.photo_path ? `<img src="${thumbSrc(s.photo_path)}" data-img="${API}${s.photo_path}" loading="lazy" decoding="async" alt="">` : ''}
|
||||
<div class="info">
|
||||
<div class="name">${esc(s.name)}</div>
|
||||
${s.group_name ? `<div class="group">${esc(s.group_name)}</div>` : ''}
|
||||
<div class="trash-time">Удалена: ${fmtFull(s.deleted_at)}</div>
|
||||
</div>
|
||||
<button class="restore-btn" data-restore="${s.id}" title="Восстановить">Восстановить</button>
|
||||
<button class="del-btn" data-hard="${s.id}" title="Пометить на удаление (через ${purgeDays} дней будет удалена безвозвратно)">×</button>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function pendingStudentHTML(s) {
|
||||
return `\
|
||||
<div class="card pending">
|
||||
${s.photo_path ? `<img src="${thumbSrc(s.photo_path)}" data-img="${API}${s.photo_path}" loading="lazy" decoding="async" alt="">` : ''}
|
||||
<div class="info">
|
||||
<div class="name">${esc(s.name)}</div>
|
||||
${s.group_name ? `<div class="group">${esc(s.group_name)}</div>` : ''}
|
||||
<div class="trash-time warn">Удаление через ${purgeDays} дн.: ${fmtFull(s.purge_at)}</div>
|
||||
</div>
|
||||
<button class="restore-btn" data-unschedule="${s.id}" title="Вернуть в корзину">В корзину</button>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function renderGroups() {
|
||||
const grid = document.getElementById('groupsGrid');
|
||||
grid.className = 'grid';
|
||||
@@ -94,6 +127,16 @@ function renderGroups() {
|
||||
document.getElementById('groupsInfo').textContent =
|
||||
totalGroups > 0 ? `— групп в корзине: ${totalGroups}` : '';
|
||||
}
|
||||
function renderStudents() {
|
||||
const grid = document.getElementById('studentsGrid');
|
||||
grid.className = 'grid';
|
||||
grid.innerHTML = currentStudents.length
|
||||
? currentStudents.map(studentHTML).join('')
|
||||
: '<div class="empty">Учеников в корзине нет</div>';
|
||||
document.getElementById('studentsInfo').textContent =
|
||||
totalStudents > 0 ? `— учеников в корзине: ${totalStudents}` : '';
|
||||
}
|
||||
|
||||
|
||||
function renderGrid() {
|
||||
const grid = document.getElementById('grid');
|
||||
@@ -104,10 +147,10 @@ function renderGrid() {
|
||||
document.getElementById('entriesInfo').textContent =
|
||||
totalEntries > 0 ? `— записей в корзине: ${totalEntries}` : '';
|
||||
document.getElementById('trashInfo').textContent =
|
||||
totalEntries + totalGroups > 0
|
||||
? `В корзине: записей ${totalEntries}, групп ${totalGroups}`
|
||||
totalEntries + totalGroups + totalStudents > 0
|
||||
? `В корзине: записей ${totalEntries}, групп ${totalGroups}, учеников ${totalStudents}`
|
||||
: 'Корзина пуста';
|
||||
document.getElementById('emptyBtn').disabled = totalEntries + totalGroups === 0;
|
||||
document.getElementById('emptyBtn').disabled = totalEntries + totalGroups + totalStudents === 0;
|
||||
renderPager(document.getElementById('pager'), page, Math.max(1, Math.ceil(totalEntries / PAGE_SIZE)), goPage);
|
||||
}
|
||||
|
||||
@@ -122,15 +165,21 @@ function renderPending() {
|
||||
: '<div class="empty">Нет записей, помеченных на удаление</div>';
|
||||
document.getElementById('pendingGroupsInfo').textContent =
|
||||
pendingTotalGroups > 0 ? `— ${pendingTotalGroups}` : '';
|
||||
document.getElementById('pendingStudentsGrid').className = 'grid';
|
||||
document.getElementById('pendingStudentsGrid').innerHTML = pendingStudents.length
|
||||
? pendingStudents.map(pendingStudentHTML).join('')
|
||||
: '<div class="empty">Нет учеников, помеченных на удаление</div>';
|
||||
document.getElementById('pendingStudentsInfo').textContent =
|
||||
pendingTotalStudents > 0 ? `— ${pendingTotalStudents}` : '';
|
||||
document.getElementById('pendingEntriesInfo').textContent =
|
||||
pendingTotalEntries > 0 ? `— ${pendingTotalEntries}` : '';
|
||||
const n = pendingTotalEntries + pendingTotalGroups;
|
||||
const n = pendingTotalEntries + pendingTotalGroups + pendingTotalStudents;
|
||||
document.getElementById('pendingInfo').textContent =
|
||||
n > 0 ? `— будет удалено: ${n}` : '';
|
||||
const hint = document.getElementById('pendingToggleHint');
|
||||
if (hint) {
|
||||
hint.textContent = n > 0
|
||||
? `Помечено: записей ${pendingTotalEntries}, групп ${pendingTotalGroups} — безвозвратно удалятся через ${purgeDays} дней`
|
||||
? `Помечено: записей ${pendingTotalEntries}, групп ${pendingTotalGroups}, учеников ${pendingTotalStudents} — безвозвратно удалятся через ${purgeDays} дней`
|
||||
: 'Данных, помеченных на удаление, нет';
|
||||
}
|
||||
document.getElementById('pendingSection').hidden = !document.getElementById('pendingToggle').checked;
|
||||
@@ -150,12 +199,17 @@ async function loadTrash() {
|
||||
totalEntries = data.total;
|
||||
currentGroups = data.groups;
|
||||
totalGroups = data.total_groups;
|
||||
currentStudents = data.students ? data.students.rows : [];
|
||||
totalStudents = data.students ? data.students.total : 0;
|
||||
pendingEntries = pend.entries;
|
||||
pendingTotalEntries = pend.total;
|
||||
pendingGroups = pend.groups;
|
||||
pendingTotalGroups = pend.total_groups;
|
||||
pendingStudents = pend.students ? pend.students.rows : [];
|
||||
pendingTotalStudents = pend.students ? pend.students.total : 0;
|
||||
if (pend.purge_days > 0) purgeDays = pend.purge_days;
|
||||
renderGroups();
|
||||
renderStudents();
|
||||
renderGrid();
|
||||
renderPending();
|
||||
}
|
||||
@@ -200,10 +254,30 @@ async function unscheduleGroup(id) {
|
||||
else { const err = await res.json(); showToast(err.error || 'Ошибка'); }
|
||||
}
|
||||
|
||||
async function restoreStudent(id) {
|
||||
const res = await fetch(`${API}/api/students/${id}/restore`, { method: 'PUT', headers: hdr() });
|
||||
if (res.ok) { showToast('Ученик восстановлен'); loadTrash(); }
|
||||
else { const err = await res.json(); showToast(err.error || 'Ошибка восстановления'); }
|
||||
}
|
||||
|
||||
async function hardDeleteStudent(id, name) {
|
||||
if (!confirm(`Пометить ученика "${name}" на удаление? Он будет безвозвратно удалён через ${purgeDays} дней вместе с фото.`)) return;
|
||||
const res = await fetch(`${API}/api/students/${id}/permanent`, { method: 'DELETE', headers: hdr() });
|
||||
if (res.ok) { showToast(`Ученик помечен на удаление через ${purgeDays} дн.`); loadTrash(); }
|
||||
else { const err = await res.json(); showToast(err.error || 'Ошибка'); }
|
||||
}
|
||||
|
||||
async function unscheduleStudent(id) {
|
||||
const res = await fetch(`${API}/api/students/${id}/unschedule`, { method: 'PUT', headers: hdr() });
|
||||
if (res.ok) { showToast('Ученик возвращён в корзину'); loadTrash(); }
|
||||
else { const err = await res.json(); showToast(err.error || 'Ошибка'); }
|
||||
}
|
||||
|
||||
async function emptyTrash() {
|
||||
const parts = [];
|
||||
if (totalEntries) parts.push(`записей: ${totalEntries}`);
|
||||
if (totalGroups) parts.push(`групп: ${totalGroups}`);
|
||||
if (totalStudents) parts.push(`студентов: ${totalStudents}`);
|
||||
if (!parts.length) return;
|
||||
if (!confirm(`Пометить всё из корзины (${parts.join(', ')}) на удаление? Данные будут безвозвратно удалены через ${purgeDays} дней.`)) return;
|
||||
const res = await fetch(`${API}/api/trash`, { method: 'DELETE', headers: hdr() });
|
||||
@@ -241,6 +315,19 @@ document.getElementById('groupsGrid').addEventListener('click', e => {
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('studentsGrid').addEventListener('click', e => {
|
||||
const card = e.target.closest('.card');
|
||||
if (!card) return;
|
||||
const rs = card.querySelector('[data-restore]');
|
||||
if (rs && (e.target === rs || rs.contains(e.target))) { restoreStudent(parseInt(rs.dataset.restore, 10)); return; }
|
||||
const hd = card.querySelector('[data-hard]');
|
||||
if (hd && (e.target === hd || hd.contains(e.target))) {
|
||||
const id = parseInt(hd.dataset.hard, 10);
|
||||
const s = currentStudents.find(x => String(x.id) === String(id));
|
||||
hardDeleteStudent(id, s ? s.name : id);
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('pendingGrid').addEventListener('click', e => {
|
||||
const card = e.target.closest('.card');
|
||||
if (!card) return;
|
||||
@@ -255,8 +342,15 @@ document.getElementById('pendingGroupsGrid').addEventListener('click', e => {
|
||||
if (us && (e.target === us || us.contains(e.target))) { unscheduleGroup(parseInt(us.dataset.unscheduleGroup, 10)); }
|
||||
});
|
||||
|
||||
document.getElementById('pendingStudentsGrid').addEventListener('click', e => {
|
||||
const card = e.target.closest('.card');
|
||||
if (!card) return;
|
||||
const us = card.querySelector('[data-unschedule]');
|
||||
if (us && (e.target === us || us.contains(e.target))) { unscheduleStudent(parseInt(us.dataset.unschedule, 10)); }
|
||||
});
|
||||
|
||||
(async () => {
|
||||
if (await checkAuth()) {
|
||||
if (await requireAdminPage()) {
|
||||
buildSidebar(document.body.dataset.page);
|
||||
loadTrash();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user