Календарь: история действий с отменой для админа
This commit is contained in:
@@ -138,6 +138,9 @@ const {
|
||||
softDeleteCalendarEvent,
|
||||
restoreCalendarEvent,
|
||||
deleteCalendarEventPermanent,
|
||||
listCalendarActions,
|
||||
getCalendarAction,
|
||||
undoCalendarAction,
|
||||
listWorkspaceSources,
|
||||
createCalendarReminderWorker,
|
||||
} = require('./lib/calendar');
|
||||
@@ -5617,6 +5620,52 @@ app.get('/api/calendar/users', apiLimiter, requireAuth, requireAdmin, async (req
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/calendar/history', apiLimiter, requireAuth, requireAdmin, async (req, res) => {
|
||||
try {
|
||||
const limit = req.query.limit == null ? 10 : parseInt(req.query.limit, 10);
|
||||
const offset = req.query.offset == null ? 0 : parseInt(req.query.offset, 10);
|
||||
if (!Number.isInteger(limit) || limit < 1 || limit > 100 || !Number.isInteger(offset) || offset < 0) {
|
||||
return res.status(400).json({ error: 'Invalid pagination' });
|
||||
}
|
||||
let userId = null;
|
||||
if (req.query.user_id != null && req.query.user_id !== '') {
|
||||
userId = parseInt(req.query.user_id, 10);
|
||||
if (!Number.isInteger(userId) || userId < 1) return res.status(400).json({ error: 'Invalid user_id' });
|
||||
}
|
||||
res.json(await listCalendarActions(pool, { limit, offset, userId }));
|
||||
} catch (e) {
|
||||
console.error('calendar history:', e);
|
||||
res.status(500).json({ error: 'Не удалось получить историю календаря' });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/calendar/history/:id', apiLimiter, requireAuth, requireAdmin, async (req, res) => {
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
const item = await getCalendarAction(pool, id);
|
||||
if (!item) return res.status(404).json({ error: 'Запись истории не найдена' });
|
||||
res.json(item);
|
||||
} catch (e) {
|
||||
console.error('calendar history item:', e);
|
||||
res.status(500).json({ error: 'Не удалось получить действие' });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/calendar/history/:id/undo', requireAuth, requireAdmin, async (req, res) => {
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
await undoCalendarAction(pool, { id, purgeDays: await trashPurgeDays(), actor: req.user });
|
||||
await logAudit(req, 'calendar.history.undo', { id });
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
if ([400, 403, 404, 409].includes(e.status)) return res.status(e.status).json({ error: e.message });
|
||||
console.error('calendar history undo:', e);
|
||||
res.status(500).json({ error: 'Не удалось отменить действие' });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/calendar/events', apiLimiter, requireAuth, async (req, res) => {
|
||||
try {
|
||||
const tz = await appTimezone();
|
||||
@@ -5639,7 +5688,7 @@ app.post('/api/calendar/events', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const audience = await calendarAudience(pool, req.user, req.body);
|
||||
const items = await createCalendarEvents(pool, {
|
||||
userId: req.user.id, audience, data: req.body, tz: await appTimezone(),
|
||||
userId: req.user.id, actor: req.user, audience, data: req.body, tz: await appTimezone(),
|
||||
});
|
||||
await logAudit(req, 'calendar.event.create', {
|
||||
count: items.length, user_ids: items.map(x => x.user_id),
|
||||
@@ -5659,7 +5708,7 @@ app.put('/api/calendar/events/:id', requireAuth, async (req, res) => {
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
const audience = await calendarAudience(pool, req.user, req.body);
|
||||
const item = await updateCalendarEvent(pool, {
|
||||
userId: req.user.id, audience, id, data: req.body, tz: await appTimezone(),
|
||||
userId: req.user.id, actor: req.user, audience, id, data: req.body, tz: await appTimezone(),
|
||||
});
|
||||
await logAudit(req, 'calendar.event.update', { id, title: item.title, starts_at: item.starts_at, user_id: item.user_id, is_common: item.is_common });
|
||||
res.json({ item });
|
||||
@@ -5674,7 +5723,7 @@ app.delete('/api/calendar/events/:id', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
await softDeleteCalendarEvent(pool, { userId: req.user.id, isAdmin: req.user.role === 'admin', id, purgeDays: await trashPurgeDays() });
|
||||
await softDeleteCalendarEvent(pool, { userId: req.user.id, actor: req.user, isAdmin: req.user.role === 'admin', id, purgeDays: await trashPurgeDays() });
|
||||
await logAudit(req, 'calendar.event.delete', { id });
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
@@ -5688,7 +5737,7 @@ app.put('/api/calendar/events/:id/restore', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
await restoreCalendarEvent(pool, { userId: req.user.id, isAdmin: req.user.role === 'admin', id });
|
||||
await restoreCalendarEvent(pool, { userId: req.user.id, actor: req.user, isAdmin: req.user.role === 'admin', id });
|
||||
await logAudit(req, 'calendar.event.restore', { id });
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
@@ -5702,7 +5751,7 @@ app.delete('/api/calendar/events/:id/permanent', requireAuth, async (req, res) =
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
await deleteCalendarEventPermanent(pool, { userId: req.user.id, isAdmin: req.user.role === 'admin', id });
|
||||
await deleteCalendarEventPermanent(pool, { userId: req.user.id, actor: req.user, isAdmin: req.user.role === 'admin', id });
|
||||
await logAudit(req, 'calendar.event.permanent-delete', { id });
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
@@ -7535,7 +7584,7 @@ function backupUploadGuard(req, res, next) {
|
||||
async function buildBackupArchive() {
|
||||
const staging = fs.mkdtempSync(path.join(os.tmpdir(), 'wido-bk-'));
|
||||
try {
|
||||
const [g, s, e, st, pf, br, us, ub, gp, ep, md, sp, sl, pj, lr, lrv, al, nt, nr, bi, ct, cm, ca, sm, fb, uss, un, bm, sfr, pfcs, swm, ds, da, di, df, si, atr, sar, dg, dgi, fm, ce] = await Promise.all([
|
||||
const [g, s, e, st, pf, br, us, ub, gp, ep, md, sp, sl, pj, lr, lrv, al, nt, nr, bi, ct, cm, ca, sm, fb, uss, un, bm, sfr, pfcs, swm, ds, da, di, df, si, atr, sar, dg, dgi, fm, ce, cal] = await Promise.all([
|
||||
pool.query('SELECT * FROM groups ORDER BY id'),
|
||||
pool.query('SELECT * FROM students ORDER BY id'),
|
||||
pool.query('SELECT * FROM entries ORDER BY id'),
|
||||
@@ -7578,6 +7627,7 @@ async function buildBackupArchive() {
|
||||
pool.query('SELECT * FROM digest_images ORDER BY id'),
|
||||
pool.query('SELECT * FROM farewell_messages ORDER BY id'),
|
||||
pool.query('SELECT * FROM calendar_events ORDER BY id'),
|
||||
pool.query('SELECT * FROM calendar_action_log ORDER BY id'),
|
||||
]);
|
||||
const settings = {};
|
||||
st.rows.forEach(r => { settings[r.key] = r.value; });
|
||||
@@ -7601,6 +7651,7 @@ async function buildBackupArchive() {
|
||||
doc_files: df.rowCount, sidebar_items: si.rowCount,
|
||||
digests: dg.rowCount, digest_images: dgi.rowCount, farewell_messages: fm.rowCount,
|
||||
calendar_events: ce.rowCount,
|
||||
calendar_action_log: cal.rowCount,
|
||||
},
|
||||
groups: g.rows, students: s.rows, entries: e.rows, settings, project_files: pf.rows, branches: br.rows, users: us.rows, user_branches: ub.rows, group_photos: gp.rows, entry_photos: ep.rows, modules: md.rows, student_photos: sp.rows, share_links: sl.rows, photo_jobs: pj.rows, lesson_reports: lr.rows, lesson_report_versions: lrv.rows, audit_log: al.rows, notifications: nt.rows, notification_reads: nr.rows, banned_ips: bi.rows, chat_threads: ct.rows, chat_messages: cm.rows, chat_attachments: ca.rows, short_messages: sm.rows, feedbacks: fb.rows, user_settings: uss.rows, user_notes: un.rows, bookmarks: bm.rows, student_face_refs: sfr.rows, photo_faces: pfcs.rows, student_work_marks: swm.rows,
|
||||
achievement_types: atr.rows, student_achievements: sar.rows,
|
||||
@@ -7608,6 +7659,7 @@ async function buildBackupArchive() {
|
||||
doc_files: df.rows, sidebar_items: si.rows,
|
||||
digests: dg.rows, digest_images: dgi.rows, farewell_messages: fm.rows,
|
||||
calendar_events: ce.rows,
|
||||
calendar_action_log: cal.rows,
|
||||
};
|
||||
fs.writeFileSync(path.join(staging, 'data.json'), JSON.stringify(payload));
|
||||
const files = await storage.downloadAll(path.join(staging, 'uploads'));
|
||||
@@ -7882,6 +7934,7 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
|
||||
await client.query('DELETE FROM user_settings');
|
||||
await client.query('DELETE FROM user_notes');
|
||||
await client.query('DELETE FROM calendar_events');
|
||||
await client.query('DELETE FROM calendar_action_log');
|
||||
await client.query('DELETE FROM bookmarks');
|
||||
await client.query('DELETE FROM achievement_types');
|
||||
await client.query('DELETE FROM photo_faces');
|
||||
@@ -7936,6 +7989,16 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
|
||||
[x.id, x.user_id, x.is_common === true, x.title, x.notes, x.location, x.color, x.starts_at, x.ends_at, x.all_day === true, x.repeat_rule, x.repeat_until || null, x.reminder_minutes || [], x.created_at || null, x.updated_at || null, x.deleted_at || null, x.purge_at || null]
|
||||
);
|
||||
}
|
||||
for (const x of (ndata.calendar_action_log || [])) {
|
||||
const okUser = x.user_id == null || (await client.query('SELECT 1 FROM users WHERE id = $1', [x.user_id])).rowCount;
|
||||
if (!okUser) continue;
|
||||
await client.query(
|
||||
'INSERT INTO calendar_action_log (id, event_ids, user_id, user_name, action, title, before_state, after_state, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7::jsonb,$8::jsonb,$9)',
|
||||
[x.id, x.event_ids || [], x.user_id || null, x.user_name || '', x.action, x.title || '',
|
||||
JSON.stringify(Array.isArray(x.before_state) ? x.before_state : []),
|
||||
JSON.stringify(Array.isArray(x.after_state) ? x.after_state : []), x.created_at || null]
|
||||
);
|
||||
}
|
||||
for (const x of (ndata.achievement_types || [])) {
|
||||
await client.query(
|
||||
'INSERT INTO achievement_types (id, name, sort_order, is_active, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6)',
|
||||
|
||||
Reference in New Issue
Block a user