feat(dashboard): post-lesson tasks with blinking next step and manual close

This commit is contained in:
dev
2026-11-01 22:12:45 +03:00
parent 574f1bc204
commit b2f070ee7b
3 changed files with 270 additions and 0 deletions
+134
View File
@@ -4512,6 +4512,7 @@ app.post('/api/feedbacks', requireAuth, asyncRoute(async (req, res) => {
await client.query('COMMIT');
await logAudit(req, 'feedback.create', { id: fb.id, group_id: groupId, resident, ai_check: aiCheck, ai_profile: aiProfile || null, ai_status: fb.ai_status, short_count: shortIds.length });
invalidateFeedbacks();
invalidateStats();
if (aiCheck) wakeFeedbackWorker();
res.status(201).json(fb);
} catch (e) {
@@ -5703,6 +5704,7 @@ app.put('/api/feedbacks/:id', requireAuth, asyncRoute(async (req, res) => {
client.release();
}
invalidateFeedbacks();
invalidateStats();
if (shortsTouched) invalidateShorts();
if (aiCheck) wakeFeedbackWorker();
await logAudit(req, 'feedback.update', { id, ai_check: aiCheck, ai_profile: aiProfile || null });
@@ -5737,6 +5739,7 @@ app.delete('/api/feedbacks/:id', requireAuth, async (req, res) => {
}
await logAudit(req, 'feedback.delete', { id });
invalidateFeedbacks();
invalidateStats();
if (shortsTouched) invalidateShorts();
res.json({ ok: true });
});
@@ -5767,6 +5770,7 @@ app.put('/api/feedbacks/:id/restore', requireAuth, async (req, res) => {
}
await logAudit(req, 'feedback.restore', { id });
invalidateFeedbacks();
invalidateStats();
if (shortsTouched) invalidateShorts();
res.json({ ok: true });
});
@@ -5797,6 +5801,7 @@ app.delete('/api/feedbacks/:id/permanent', requireAuth, async (req, res) => {
}
await logAudit(req, 'feedback.permanent-delete', { id });
invalidateFeedbacks();
invalidateStats();
if (shortsTouched) invalidateShorts();
res.json({ ok: true });
});
@@ -5828,6 +5833,7 @@ app.put('/api/feedbacks/:id/sent', requireAuth, async (req, res) => {
}
await logAudit(req, 'feedback.sent', { id, sent, short_count: shortIds.length });
invalidateFeedbacks();
invalidateStats();
if (shortsTouched) invalidateShorts();
res.json({ ok: true, sent });
} catch (err) {
@@ -9036,6 +9042,7 @@ app.post('/api/lesson-reports', requireAuth, async (req, res) => {
await saveLessonReportVersion(created.id, body, 'manual', req.user.id);
await logAudit(req, 'lesson_report.create', { id: created.id, group_id: grp.group.id, lesson_date: date, ai_status: created.ai_status });
invalidateLessonReports();
invalidateStats();
if (aiWanted) wakeLessonAiWorker();
await pushNotification({
type: 'lesson.report',
@@ -9110,6 +9117,7 @@ app.put('/api/lesson-reports/:id', requireAuth, async (req, res) => {
if (body) await saveLessonReportVersion(report.id, body, 'manual', req.user.id);
await logAudit(req, 'lesson_report.update', { id: report.id, group_id: nextGroup, lesson_date: date, ai_status: updated.ai_status });
invalidateLessonReports();
invalidateStats();
if (aiWanted) wakeLessonAiWorker();
res.json(updated);
});
@@ -9194,6 +9202,7 @@ app.delete('/api/lesson-reports/:id', requireAuth, async (req, res) => {
await pool.query('DELETE FROM lesson_reports WHERE id = $1', [report.id]);
await logAudit(req, 'lesson_report.delete', { id: report.id, group_id: report.group_id, lesson_date: report.lesson_date });
invalidateLessonReports();
invalidateStats();
res.json({ ok: true });
});
@@ -11833,6 +11842,128 @@ app.get('/api/setup/tls-package', requireAdmin, async (req, res) => {
}
});
async function buildPostLessonTasks(user) {
const isAdmin = user.role === 'admin';
const s = branchScope(user);
const branchIds = isAdmin ? [] : (s.ids || []);
if (!isAdmin && !user.id) return [];
let whereGroup = '';
const groupParams = [];
if (!isAdmin) {
whereGroup = ' AND g.tutor_id = $1';
groupParams.push(user.id);
}
const { rows: groups } = await pool.query(
`SELECT g.id, g.name, g.branch_id, g.tutor_id, g.day_of_week, g.time_start, g.time_end
FROM groups g
WHERE g.deleted_at IS NULL AND g.day_of_week IS NOT NULL AND g.time_start IS NOT NULL AND g.time_end IS NOT NULL${whereGroup}`,
groupParams
);
if (!groups.length) return [];
const tz = await appTimezone();
const today = await appTodayIso();
const { rows: nowRows } = await pool.query("SELECT to_char(now() AT TIME ZONE $1, 'HH24:MI') AS t", [tz]);
const nowHm = (nowRows[0] && nowRows[0].t) || '00:00';
const dates = [];
for (let i = 13; i >= 0; i--) {
const d = new Date(today + 'T00:00:00Z');
d.setUTCDate(d.getUTCDate() - i);
dates.push(d.toISOString().slice(0, 10));
}
const candidates = [];
for (const g of groups) {
for (const d of dates) {
const dow = new Date(d + 'T00:00:00Z').getUTCDay();
if (dow !== g.day_of_week) continue;
if (d > today) continue;
if (d === today) {
const end = String(g.time_end).slice(0, 5);
if (end > nowHm) continue;
}
candidates.push({ g, date: d });
}
}
if (!candidates.length) return [];
const groupIds = [...new Set(candidates.map(c => c.g.id))];
const dateList = [...new Set(candidates.map(c => c.date))];
const reportSet = new Set();
const { rows: reportRows } = await pool.query(
`SELECT group_id, lesson_date::text AS d FROM lesson_reports WHERE group_id = ANY($1::int[]) AND lesson_date = ANY($2::date[])`,
[groupIds, dateList]
);
reportRows.forEach(r => reportSet.add(r.group_id + '|' + r.d));
const feedbackSet = new Set();
const { rows: fbRows } = await pool.query(
`SELECT group_id, feedback_date::text AS d FROM feedbacks WHERE deleted_at IS NULL AND group_id = ANY($1::int[]) AND feedback_date = ANY($2::date[])`,
[groupIds, dateList]
);
fbRows.forEach(r => feedbackSet.add(r.group_id + '|' + r.d));
const { rows: students } = await pool.query(
`SELECT name, group_id FROM students WHERE group_id = ANY($1::int[]) AND deleted_at IS NULL`,
[groupIds]
);
const studentsByGroup = new Map();
for (const st of students) {
if (!studentsByGroup.has(st.group_id)) studentsByGroup.set(st.group_id, []);
studentsByGroup.get(st.group_id).push(String(st.name || '').trim().toLowerCase());
}
const groupsByStudentName = new Map();
for (const g of groups) {
for (const name of (studentsByGroup.get(g.id) || [])) {
if (!groupsByStudentName.has(name)) groupsByStudentName.set(name, new Set());
groupsByStudentName.get(name).add(g.id);
}
}
const shortsSet = new Set();
const { rows: shortRows } = await pool.query(
`SELECT lesson_date::text AS d, resident FROM short_messages WHERE deleted_at IS NULL AND lesson_date = ANY($1::date[])`,
[dateList]
);
for (const sm of shortRows) {
const gids = groupsByStudentName.get(String(sm.resident || '').trim().toLowerCase());
if (gids) for (const gid of gids) shortsSet.add(gid + '|' + sm.d);
}
const tutorIds = [...new Set(groups.map(g => g.tutor_id).filter(Boolean))];
const tutorMap = new Map();
if (tutorIds.length) {
const { rows: users } = await pool.query('SELECT id, name, username FROM users WHERE id = ANY($1::int[])', [tutorIds]);
for (const u of users) tutorMap.set(u.id, u.name || u.username || '');
}
const tasks = candidates.map(({ g, date }) => {
const key = g.id + '|' + date;
const reportDone = reportSet.has(key);
const shortsDone = shortsSet.has(key);
const feedbackDone = feedbackSet.has(key);
const steps = [
{ key: 'report', label: 'Отчёт о занятии', done: reportDone, link: '/lessons.html' },
{ key: 'shorts', label: 'Короткие сообщения', done: shortsDone, link: '/shorts.html' },
{ key: 'feedback', label: 'Обратная связь', done: feedbackDone, link: '/feedback.html' },
];
const completed = steps.filter(x => x.done).length;
return {
key,
group_id: g.id,
group_name: g.name,
tutor_id: g.tutor_id || null,
tutor_name: g.tutor_id ? (tutorMap.get(g.tutor_id) || '') : '',
lesson_date: date,
lesson_time: String(g.time_start).slice(0, 5) + '–' + String(g.time_end).slice(0, 5),
steps,
completed,
total: 3,
overdue: date < today,
all_done: completed === 3,
};
});
tasks.sort((a, b) => {
if (a.all_done !== b.all_done) return a.all_done ? 1 : -1;
if (a.overdue !== b.overdue) return a.overdue ? -1 : 1;
if (a.lesson_date !== b.lesson_date) return b.lesson_date.localeCompare(a.lesson_date);
return String(a.group_name).localeCompare(String(b.group_name));
});
return tasks;
}
app.get('/api/dashboard', requireAuth, async (req, res) => {
try {
const payload = await cacheWrap('dashboard:' + scopeKey(req.user), STATS_TTL_MS, async () => {
@@ -11948,6 +12079,7 @@ app.get('/api/dashboard', requireAuth, async (req, res) => {
recent_lessons: lessons.rows,
work_marks: marks.rows,
latest_photo_taken: (latestPhotos.rows[0] || {}).taken_at || null,
post_lesson_tasks: await buildPostLessonTasks(req.user),
disk: getDiskInfo(),
};
});
@@ -14112,6 +14244,7 @@ apiV1.post('/lesson-reports', apiWrite('write'), async (req, res) => {
await saveLessonReportVersion(created.id, text, 'manual', req.user.id);
await apiAudit(req, 'api.lesson_report.create', { id: created.id, group_id: grp.group.id, lesson_date: date });
invalidateLessonReports();
invalidateStats();
if (aiWanted) wakeLessonAiWorker();
res.status(201).json(created);
});
@@ -14163,6 +14296,7 @@ apiV1.put('/lesson-reports/:id', apiWrite('write'), async (req, res) => {
if (body) await saveLessonReportVersion(report.id, body, 'manual', req.user.id);
await apiAudit(req, 'api.lesson_report.update', { id: report.id, lesson_date: date });
invalidateLessonReports();
invalidateStats();
if (aiWanted) wakeLessonAiWorker();
res.json(rows[0]);
});