lesson-topic: автозаполнение темы модуля на главной по отчёту о занятии

This commit is contained in:
dev
2026-11-24 12:30:27 +03:00
parent 36baa45cd8
commit d7f4a4b457
7 changed files with 466 additions and 2 deletions
+27 -1
View File
@@ -1,4 +1,5 @@
const LESSON_REPORT_EDIT_DENIED = 'Изменять отчёт о занятии может только его автор или администратор';
const LESSON_TOPIC_MATCH_WINDOW_SEC = 3600;
function lessonReportEditable(report, user) {
if (!report || !user) return false;
@@ -13,4 +14,29 @@ function lessonReportWriteBlocked(res, report, user) {
return true;
}
module.exports = { LESSON_REPORT_EDIT_DENIED, lessonReportEditable, lessonReportWriteBlocked };
function stripLessonTopicNumber(topic) {
return String(topic || '').trim().replace(/\s*\d+\s*\/\s*\d+\s*$/, '').trim();
}
async function findCurrentLessonTopic(pool, { groupId, timezone, windowSec = LESSON_TOPIC_MATCH_WINDOW_SEC }) {
const { rows } = await pool.query(`
SELECT lr.topic
FROM lesson_reports lr
WHERE lr.group_id = $2
AND lr.lesson_date = (now() AT TIME ZONE $1)::date
AND lr.lesson_time IS NOT NULL
AND ABS(EXTRACT(EPOCH FROM (lr.lesson_time - (now() AT TIME ZONE $1)::time))) <= $3
ORDER BY ABS(EXTRACT(EPOCH FROM (lr.lesson_time - (now() AT TIME ZONE $1)::time))) ASC, lr.id DESC
LIMIT 1
`, [timezone, groupId, windowSec]);
return rows.length ? stripLessonTopicNumber(rows[0].topic) || null : null;
}
module.exports = {
LESSON_REPORT_EDIT_DENIED,
LESSON_TOPIC_MATCH_WINDOW_SEC,
lessonReportEditable,
lessonReportWriteBlocked,
stripLessonTopicNumber,
findCurrentLessonTopic,
};