- Убрать окно совпадения по времени (LESSON_TOPIC_MATCH_WINDOW_SEC) из findCurrentLessonTopic: сортировка только по lesson_time NULLS LAST, id DESC - Убрать background:#d4d4d8 у .nav-dot в admin.css
39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
const LESSON_REPORT_EDIT_DENIED = 'Изменять отчёт о занятии может только его автор или администратор';
|
|
|
|
function lessonReportEditable(report, user) {
|
|
if (!report || !user) return false;
|
|
if (user.role === 'admin') return true;
|
|
if (report.author_id == null) return false;
|
|
return Number(report.author_id) === Number(user.id);
|
|
}
|
|
|
|
function lessonReportWriteBlocked(res, report, user) {
|
|
if (lessonReportEditable(report, user)) return false;
|
|
res.status(403).json({ error: LESSON_REPORT_EDIT_DENIED });
|
|
return true;
|
|
}
|
|
|
|
function stripLessonTopicNumber(topic) {
|
|
return String(topic || '').trim().replace(/\s*\d+\s*\/\s*\d+\s*$/, '').trim();
|
|
}
|
|
|
|
async function findCurrentLessonTopic(pool, { groupId, timezone }) {
|
|
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
|
|
ORDER BY lr.lesson_time NULLS LAST, lr.id DESC
|
|
LIMIT 1
|
|
`, [timezone, groupId]);
|
|
return rows.length ? stripLessonTopicNumber(rows[0].topic) || null : null;
|
|
}
|
|
|
|
module.exports = {
|
|
LESSON_REPORT_EDIT_DENIED,
|
|
lessonReportEditable,
|
|
lessonReportWriteBlocked,
|
|
stripLessonTopicNumber,
|
|
findCurrentLessonTopic,
|
|
};
|