From 87f3c8a2f0666090d06ed40ffb0bc123843e1986 Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 8 Oct 2026 22:24:22 +0300 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=B2=D1=82=D0=BE=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D0=B9=20=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=20=D0=B8=20=D0=BF=D1=83=D1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.js | 93 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/server.js b/server.js index d64b4ad..eea90bc 100644 --- a/server.js +++ b/server.js @@ -7730,34 +7730,35 @@ app.get('/api/system-info', requireAdmin, async (_, res) => { }); app.get('/api/dashboard', requireAuth, async (req, res) => { - const payload = await cacheWrap('dashboard:' + scopeKey(req.user), STATS_TTL_MS, async () => { - const DAYS = ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб']; - const tz = await appTimezone(); - const isAdmin = req.user.role === 'admin'; - const s = branchScope(req.user); - const branchIds = isAdmin ? [] : s.ids; - const whereGroup = isAdmin ? '' : (branchIds.length ? ` AND g.branch_id IN (${branchIds.map((_, i) => `$${i + 1}`).join(',')})` : ' AND 1 = 0'); - const whereGroupParams = isAdmin ? [] : branchIds; - const whereEntry = isAdmin ? '' : (branchIds.length ? ` AND g.branch_id IN (${branchIds.map((_, i) => `$${i + 1}`).join(',')})` : ' AND 1 = 0'); + try { + const payload = await cacheWrap('dashboard:' + scopeKey(req.user), STATS_TTL_MS, async () => { + const DAYS = ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб']; + const tz = await appTimezone(); + const isAdmin = req.user.role === 'admin'; + const s = branchScope(req.user); + const branchIds = isAdmin ? [] : s.ids; + const whereGroup = isAdmin ? '' : (branchIds.length ? ` AND g.branch_id IN (${branchIds.map((_, i) => `$${i + 1}`).join(',')})` : ' AND 1 = 0'); + const whereGroupParams = isAdmin ? [] : branchIds; + const whereEntry = isAdmin ? '' : (branchIds.length ? ` AND g.branch_id IN (${branchIds.map((_, i) => `$${i + 1}`).join(',')})` : ' AND 1 = 0'); - const [stats, activity, active, recent, top, photos, latestPhotos, lessons] = await Promise.all([ - (async () => { - const ew = !!whereEntry; - const entriesP = `SELECT count(*)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NULL${ew ? whereEntry : ''}`; - const [entries, trash, groups, students, today] = await Promise.all([ - pool.query(entriesP, ew ? whereGroupParams : []), - pool.query(`SELECT count(*)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NOT NULL${ew ? whereEntry : ''}`, ew ? whereGroupParams : []), - pool.query(`SELECT count(*)::int AS n FROM groups g${isAdmin ? '' : (branchIds.length ? ` WHERE g.branch_id IN (${branchIds.map((_, i) => `$${i + 1}`).join(',')})` : ' WHERE 1 = 0')}`, isAdmin ? [] : branchIds), - pool.query(`SELECT count(DISTINCT e.student_name)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NULL${ew ? whereEntry : ''}`, ew ? whereGroupParams : []), - (() => { const p = (ew ? whereGroupParams : []).slice(); p.push(tz); return pool.query(`SELECT count(*)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NULL AND e.created_at >= ${tzWall()}::date${ew ? whereEntry : ''}`.replace(/\$TZ\$/g, '$' + p.length), p); })(), - ]); - return { entries: entries.rows[0].n, trash: trash.rows[0].n, groups: groups.rows[0].n, students: students.rows[0].n, today: today.rows[0].n }; - })(), + const [stats, activity, active, recent, top, photos, latestPhotos, lessons] = await Promise.all([ + (async () => { + const ew = !!whereEntry; + const entriesP = `SELECT count(*)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NULL${ew ? whereEntry : ''}`; + const [entries, trash, groups, students, today] = await Promise.all([ + pool.query(entriesP, ew ? whereGroupParams : []), + pool.query(`SELECT count(*)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NOT NULL${ew ? whereEntry : ''}`, ew ? whereGroupParams : []), + pool.query(`SELECT count(*)::int AS n FROM groups g${isAdmin ? '' : (branchIds.length ? ` WHERE g.branch_id IN (${branchIds.map((_, i) => `$${i + 1}`).join(',')})` : ' WHERE 1 = 0')}`, isAdmin ? [] : branchIds), + pool.query(`SELECT count(DISTINCT e.student_name)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NULL${ew ? whereEntry : ''}`, ew ? whereGroupParams : []), + (() => { const p = (ew ? whereGroupParams : []).slice(); p.push(tz); return pool.query(`SELECT count(*)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NULL AND e.created_at >= ${tzWall()}::date${ew ? whereEntry : ''}`.replace(/\$TZ\$/g, '$' + p.length), p); })(), + ]); + return { entries: entries.rows[0].n, trash: trash.rows[0].n, groups: groups.rows[0].n, students: students.rows[0].n, today: today.rows[0].n }; + })(), pool.query( `SELECT to_char(e.created_at AT TIME ZONE $TZ$, 'YYYY-MM-DD') AS d, count(*)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NULL AND e.created_at >= (${tzWall()} - interval '13 days')::date${whereEntry} - GROUP BY 1 ORDER BY 1`.replace(/\$TZ\$/g, '$' + (whereEntry ? whereGroupParams.length : 0) + 1), + GROUP BY 1 ORDER BY 1`.replace(/\$TZ\$/g, '$' + ((whereEntry ? whereGroupParams.length : 0) + 1)), (whereEntry ? whereGroupParams : []).concat([tz]) ), pool.query( @@ -7765,7 +7766,7 @@ app.get('/api/dashboard', requireAuth, async (req, res) => { WHERE g.day_of_week IS NOT NULL AND g.time_start IS NOT NULL AND g.time_end IS NOT NULL AND g.day_of_week = EXTRACT(DOW FROM ${tzWall()})::int AND ${tzWall()}::time BETWEEN g.time_start AND g.time_end${whereGroup} - ORDER BY g.id`.replace(/\$TZ\$/g, '$' + (whereGroup ? whereGroupParams.length : 0) + 1), + ORDER BY g.id`.replace(/\$TZ\$/g, '$' + ((whereGroup ? whereGroupParams.length : 0) + 1)), (whereGroup ? whereGroupParams : []).concat([tz]) ), pool.query( @@ -7798,26 +7799,30 @@ app.get('/api/dashboard', requireAuth, async (req, res) => { whereGroup ? whereGroupParams : [] ), ]); - const activeGroups = active.rows.map(g => ({ - id: g.id, - name: g.name, - day_label: DAYS[g.day_of_week], - time_start: (g.time_start || '').slice(0, 5), - time_end: (g.time_end || '').slice(0, 5), - })); - return { - stats: stats, - activity: activity.rows, - active_groups: activeGroups, - recent_entries: recent.rows, - top_students: top.rows, - photos: photos.rows, - recent_lessons: lessons.rows, - latest_photo_taken: (latestPhotos.rows[0] || {}).taken_at || null, - disk: getDiskInfo(), - }; - }); - res.json(payload); + const activeGroups = active.rows.map(g => ({ + id: g.id, + name: g.name, + day_label: DAYS[g.day_of_week], + time_start: (g.time_start || '').slice(0, 5), + time_end: (g.time_end || '').slice(0, 5), + })); + return { + stats: stats, + activity: activity.rows, + active_groups: activeGroups, + recent_entries: recent.rows, + top_students: top.rows, + photos: photos.rows, + recent_lessons: lessons.rows, + latest_photo_taken: (latestPhotos.rows[0] || {}).taken_at || null, + disk: getDiskInfo(), + }; + }); + res.json(payload); + } catch (e) { + console.error('GET /api/dashboard error:', e); + res.status(500).json({ error: e.message || 'Ошибка загрузки дашборда' }); + } }); const entryFields = upload.fields([{ name: 'photo', maxCount: 10 }, { name: 'files', maxCount: 10 }]);