calendar: страница с 4 видами, личными событиями и источниками из системы
This commit is contained in:
@@ -119,6 +119,17 @@ const {
|
||||
} = require('./lib/achievements');
|
||||
const { ensureSidebarItemsTable, listSidebarItems, normalizeSidebarPayload } = require('./lib/sidebar');
|
||||
const { ensureDigestTable, listDigests, createDigest, updateDigest, softDeleteDigest, restoreDigest, deleteDigestPermanent, toggleDigestPin, listDigestImages, addDigestImage, deleteDigestImage, publishDueDigests } = require('./lib/digest');
|
||||
const {
|
||||
ensureCalendarEventsTable,
|
||||
normalizeCalendarRange,
|
||||
listCalendarEvents,
|
||||
createCalendarEvent,
|
||||
updateCalendarEvent,
|
||||
softDeleteCalendarEvent,
|
||||
restoreCalendarEvent,
|
||||
deleteCalendarEventPermanent,
|
||||
listWorkspaceSources,
|
||||
} = require('./lib/calendar');
|
||||
const { parseTaskKey, getTaskStates, getClosures, saveStepState, closeTask, reopenTask, ensurePostLessonTaskTables } = require('./lib/post-lesson-tasks');
|
||||
const { normalizeEntryClientId, findEntryByClientId, entryExistsByClientId } = require('./lib/entry-submit');
|
||||
|
||||
@@ -1740,6 +1751,8 @@ async function purgeScheduledDeletions() {
|
||||
const fbcount = fbres.rowCount;
|
||||
const fwres = await pool.query('DELETE FROM farewell_messages WHERE purge_at IS NOT NULL AND purge_at <= now()');
|
||||
const fwcount = fwres.rowCount;
|
||||
const calres = await pool.query('DELETE FROM calendar_events WHERE purge_at IS NOT NULL AND purge_at <= now()');
|
||||
const calCount = calres.rowCount;
|
||||
const bires = await pool.query('DELETE FROM broken_images WHERE purge_at IS NOT NULL AND purge_at <= now()');
|
||||
const bicount = bires.rowCount;
|
||||
const strefs = await pool.query('SELECT id FROM students WHERE purge_at IS NOT NULL AND purge_at <= now()');
|
||||
@@ -1762,7 +1775,7 @@ async function purgeScheduledDeletions() {
|
||||
if (fbcount) invalidateFeedbacks();
|
||||
if (stcount) invalidateStudents();
|
||||
if (bicount) invalidateAll();
|
||||
return { entries: erefs.rowCount, groups: gcount, chat_threads: ccount, short_messages: scount, feedbacks: fbcount, farewell_messages: fwcount, students: stcount, broken_images: bicount };
|
||||
return { entries: erefs.rowCount, groups: gcount, chat_threads: ccount, short_messages: scount, feedbacks: fbcount, farewell_messages: fwcount, students: stcount, broken_images: bicount, calendar_events: calCount };
|
||||
}
|
||||
|
||||
function safeUnlink(relPath) {
|
||||
@@ -2958,6 +2971,7 @@ async function ensureUsersAndFirstAdmin() {
|
||||
await ensureDocsTables(pool);
|
||||
await ensureBackupTicketsTable(pool);
|
||||
await ensureDigestTable(pool);
|
||||
await ensureCalendarEventsTable(pool);
|
||||
await ensureFarewellTable(pool);
|
||||
await ensureFirstAdmin();
|
||||
}
|
||||
@@ -5564,6 +5578,94 @@ app.delete('/api/digests/:id/preview', requireAuth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// --- Calendar ---
|
||||
|
||||
app.get('/api/calendar/events', apiLimiter, requireAuth, async (req, res) => {
|
||||
try {
|
||||
const tz = await appTimezone();
|
||||
const range = normalizeCalendarRange({ from: req.query.from, to: req.query.to, today: await appTodayIso() });
|
||||
const scope = branchScope(req.user);
|
||||
const [items, sources] = await Promise.all([
|
||||
listCalendarEvents(pool, { userId: req.user.id, ...range, tz }),
|
||||
listWorkspaceSources(pool, { admin: scope.admin, branchIds: scope.ids, ...range, tz }),
|
||||
]);
|
||||
res.json({ items, sources });
|
||||
} catch (e) {
|
||||
if (e.status === 400) return res.status(400).json({ error: e.message });
|
||||
console.error('calendar list:', e);
|
||||
res.status(500).json({ error: 'Не удалось получить календарь' });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/calendar/events', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const item = await createCalendarEvent(pool, { userId: req.user.id, data: req.body, tz: await appTimezone() });
|
||||
await logAudit(req, 'calendar.event.create', { id: item.id, title: item.title, starts_at: item.starts_at });
|
||||
res.json({ item });
|
||||
} catch (e) {
|
||||
if (e.status === 400) return res.status(400).json({ error: e.message });
|
||||
console.error('calendar create:', e);
|
||||
res.status(500).json({ error: 'Не удалось создать событие' });
|
||||
}
|
||||
});
|
||||
|
||||
app.put('/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' });
|
||||
const item = await updateCalendarEvent(pool, { userId: req.user.id, id, data: req.body, tz: await appTimezone() });
|
||||
await logAudit(req, 'calendar.event.update', { id, title: item.title, starts_at: item.starts_at });
|
||||
res.json({ item });
|
||||
} catch (e) {
|
||||
if (e.status === 400) return res.status(400).json({ error: e.message });
|
||||
if (e.status === 404) return res.status(404).json({ error: e.message });
|
||||
console.error('calendar update:', e);
|
||||
res.status(500).json({ error: 'Не удалось обновить событие' });
|
||||
}
|
||||
});
|
||||
|
||||
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, id, purgeDays: await trashPurgeDays() });
|
||||
await logAudit(req, 'calendar.event.delete', { id });
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
if (e.status === 404) return res.status(404).json({ error: e.message });
|
||||
console.error('calendar delete:', e);
|
||||
res.status(500).json({ error: 'Не удалось удалить событие' });
|
||||
}
|
||||
});
|
||||
|
||||
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, id });
|
||||
await logAudit(req, 'calendar.event.restore', { id });
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
if (e.status === 404) return res.status(404).json({ error: e.message });
|
||||
console.error('calendar restore:', e);
|
||||
res.status(500).json({ error: 'Не удалось восстановить событие' });
|
||||
}
|
||||
});
|
||||
|
||||
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, id });
|
||||
await logAudit(req, 'calendar.event.permanent-delete', { id });
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
if (e.status === 404) return res.status(404).json({ error: e.message });
|
||||
console.error('calendar permanent delete:', e);
|
||||
res.status(500).json({ error: 'Не удалось удалить событие навсегда' });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/achievement-types', requireAuth, async (req, res) => {
|
||||
try {
|
||||
res.json({ items: await achListTypes(pool) });
|
||||
@@ -7374,7 +7476,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] = 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] = 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'),
|
||||
@@ -7416,6 +7518,7 @@ async function buildBackupArchive() {
|
||||
pool.query('SELECT * FROM digests ORDER BY id'),
|
||||
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'),
|
||||
]);
|
||||
const settings = {};
|
||||
st.rows.forEach(r => { settings[r.key] = r.value; });
|
||||
@@ -7438,12 +7541,14 @@ async function buildBackupArchive() {
|
||||
doc_sections: ds.rowCount, doc_articles: da.rowCount, doc_images: di.rowCount,
|
||||
doc_files: df.rowCount, sidebar_items: si.rowCount,
|
||||
digests: dg.rowCount, digest_images: dgi.rowCount, farewell_messages: fm.rowCount,
|
||||
calendar_events: ce.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,
|
||||
doc_sections: ds.rows, doc_articles: da.rows, doc_images: di.rows,
|
||||
doc_files: df.rows, sidebar_items: si.rows,
|
||||
digests: dg.rows, digest_images: dgi.rows, farewell_messages: fm.rows,
|
||||
calendar_events: ce.rows,
|
||||
};
|
||||
fs.writeFileSync(path.join(staging, 'data.json'), JSON.stringify(payload));
|
||||
const files = await storage.downloadAll(path.join(staging, 'uploads'));
|
||||
@@ -7717,6 +7822,7 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
|
||||
await client.query('DELETE FROM audit_log');
|
||||
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 bookmarks');
|
||||
await client.query('DELETE FROM achievement_types');
|
||||
await client.query('DELETE FROM photo_faces');
|
||||
@@ -7763,6 +7869,14 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
|
||||
[x.id, x.title, x.url, x.description, x.photo_path || null, x.image_mode === 'cover' ? 'cover' : 'logo', x.open_new_tab !== false, x.is_active !== false, x.sort_order, x.created_at || null, x.updated_at || null]
|
||||
);
|
||||
}
|
||||
for (const x of (ndata.calendar_events || [])) {
|
||||
const okUser = (await client.query('SELECT 1 FROM users WHERE id = $1', [x.user_id])).rowCount;
|
||||
if (!okUser) continue;
|
||||
await client.query(
|
||||
'INSERT INTO calendar_events (id, user_id, title, notes, location, color, starts_at, ends_at, all_day, repeat_rule, repeat_until, reminder_minutes, created_at, updated_at, deleted_at, purge_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)',
|
||||
[x.id, x.user_id, 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.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