Автоматический коммит и пуш

This commit is contained in:
dev
2026-10-08 22:24:22 +03:00
parent b5d1fd0948
commit 87f3c8a2f0
+49 -44
View File
@@ -7730,34 +7730,35 @@ app.get('/api/system-info', requireAdmin, async (_, res) => {
}); });
app.get('/api/dashboard', requireAuth, async (req, res) => { app.get('/api/dashboard', requireAuth, async (req, res) => {
const payload = await cacheWrap('dashboard:' + scopeKey(req.user), STATS_TTL_MS, async () => { try {
const DAYS = ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб']; const payload = await cacheWrap('dashboard:' + scopeKey(req.user), STATS_TTL_MS, async () => {
const tz = await appTimezone(); const DAYS = ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'];
const isAdmin = req.user.role === 'admin'; const tz = await appTimezone();
const s = branchScope(req.user); const isAdmin = req.user.role === 'admin';
const branchIds = isAdmin ? [] : s.ids; const s = branchScope(req.user);
const whereGroup = isAdmin ? '' : (branchIds.length ? ` AND g.branch_id IN (${branchIds.map((_, i) => `$${i + 1}`).join(',')})` : ' AND 1 = 0'); const branchIds = isAdmin ? [] : s.ids;
const whereGroupParams = isAdmin ? [] : branchIds; const whereGroup = isAdmin ? '' : (branchIds.length ? ` AND g.branch_id IN (${branchIds.map((_, i) => `$${i + 1}`).join(',')})` : ' AND 1 = 0');
const whereEntry = 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([ const [stats, activity, active, recent, top, photos, latestPhotos, lessons] = await Promise.all([
(async () => { (async () => {
const ew = !!whereEntry; 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 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([ const [entries, trash, groups, students, today] = await Promise.all([
pool.query(entriesP, ew ? whereGroupParams : []), 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 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(*)::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 : []), 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); })(), (() => { 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 }; 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( pool.query(
`SELECT to_char(e.created_at AT TIME ZONE $TZ$, 'YYYY-MM-DD') AS d, count(*)::int AS n `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 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} 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]) (whereEntry ? whereGroupParams : []).concat([tz])
), ),
pool.query( 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 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 g.day_of_week = EXTRACT(DOW FROM ${tzWall()})::int
AND ${tzWall()}::time BETWEEN g.time_start AND g.time_end${whereGroup} 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]) (whereGroup ? whereGroupParams : []).concat([tz])
), ),
pool.query( pool.query(
@@ -7798,26 +7799,30 @@ app.get('/api/dashboard', requireAuth, async (req, res) => {
whereGroup ? whereGroupParams : [] whereGroup ? whereGroupParams : []
), ),
]); ]);
const activeGroups = active.rows.map(g => ({ const activeGroups = active.rows.map(g => ({
id: g.id, id: g.id,
name: g.name, name: g.name,
day_label: DAYS[g.day_of_week], day_label: DAYS[g.day_of_week],
time_start: (g.time_start || '').slice(0, 5), time_start: (g.time_start || '').slice(0, 5),
time_end: (g.time_end || '').slice(0, 5), time_end: (g.time_end || '').slice(0, 5),
})); }));
return { return {
stats: stats, stats: stats,
activity: activity.rows, activity: activity.rows,
active_groups: activeGroups, active_groups: activeGroups,
recent_entries: recent.rows, recent_entries: recent.rows,
top_students: top.rows, top_students: top.rows,
photos: photos.rows, photos: photos.rows,
recent_lessons: lessons.rows, recent_lessons: lessons.rows,
latest_photo_taken: (latestPhotos.rows[0] || {}).taken_at || null, latest_photo_taken: (latestPhotos.rows[0] || {}).taken_at || null,
disk: getDiskInfo(), disk: getDiskInfo(),
}; };
}); });
res.json(payload); 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 }]); const entryFields = upload.fields([{ name: 'photo', maxCount: 10 }, { name: 'files', maxCount: 10 }]);