lesson-topic: автозаполнение темы модуля на главной по отчёту о занятии
This commit is contained in:
+27
-1
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user