feat(shorts): фильтры по группе, датам, оценке и сортировке

- GET /api/shorts: group_id, date_from/date_to, delta, order (asc/desc)
- фильтр группы учитывает филиалы (branchScope), search+group_id
- панель фильтров: поиск, группа, даты, «Использовано», оценка,
  сортировка, удалённые
This commit is contained in:
dev
2026-10-14 14:20:59 +03:00
parent 51702ce6fe
commit 6d77037028
3 changed files with 441 additions and 209 deletions
+47 -2
View File
@@ -3536,6 +3536,18 @@ async function ensureStudentWorkMarksTable() {
await pool.query(`ALTER TABLE student_work_marks ALTER COLUMN author_id DROP NOT NULL`);
}
function shortDateParam(v) {
const s = v ? String(v).trim() : '';
if (!/^\d{4}-\d{2}-\d{2}$/.test(s)) return null;
return s;
}
function shortDeltaParam(v) {
const n = parseInt(v, 10);
if (Number.isNaN(n) || n < -1 || n > 1) return null;
return n;
}
async function shortMessageDelta(shortMessageId) {
const { rows } = await pool.query(
'SELECT COALESCE(MAX(delta), 0)::int AS d FROM student_work_marks WHERE short_message_id = $1',
@@ -3678,11 +3690,44 @@ app.get('/api/shorts', requireAuth, async (req, res) => {
params.push(`%${search}%`);
where += ` AND (sm.resident ILIKE $${params.length} OR sm.topic ILIKE $${params.length} OR sm.message ILIKE $${params.length})`;
}
const scope = branchScope(req.user);
const dateFrom = shortDateParam(req.query.date_from);
if (dateFrom) {
params.push(dateFrom);
where += ` AND sm.lesson_date >= $${params.length}::date`;
}
const dateTo = shortDateParam(req.query.date_to);
if (dateTo) {
params.push(dateTo);
where += ` AND sm.lesson_date <= $${params.length}::date`;
}
const deltaFilter = shortDeltaParam(req.query.delta);
if (deltaFilter !== null) {
params.push(deltaFilter);
where += ` AND COALESCE((SELECT MAX(swm.delta) FROM student_work_marks swm WHERE swm.short_message_id = sm.id), 0) = $${params.length}`;
}
const groupId = Math.max(parseInt(req.query.group_id, 10) || 0, 0);
if (groupId) {
params.push(groupId);
const groupParam = params.length;
if (!scope.admin && !scope.ids.length) {
where += ' AND FALSE';
} else if (!scope.admin) {
params.push(scope.ids);
const branchParam = params.length;
where += ` AND EXISTS (SELECT 1 FROM students s2 LEFT JOIN groups g2 ON g2.id = s2.group_id
WHERE TRIM(s2.name) = TRIM(sm.resident) AND s2.deleted_at IS NULL
AND s2.group_id = $${groupParam} AND g2.branch_id = ANY($${branchParam}::int[]))`;
} else {
where += ` AND EXISTS (SELECT 1 FROM students s2 WHERE TRIM(s2.name) = TRIM(sm.resident)
AND s2.deleted_at IS NULL AND s2.group_id = $${groupParam})`;
}
}
const countParams = params.slice();
const limitParam = params.length + 1;
const offsetParam = params.length + 2;
params.push(limit, offset);
const scope = branchScope(req.user);
const orderAsc = req.query.order === 'asc';
let studentIdSql;
if (scope.admin) {
studentIdSql = `(SELECT s.id FROM students s WHERE TRIM(s.name) = TRIM(sm.resident) AND s.deleted_at IS NULL LIMIT 1)`;
@@ -3701,7 +3746,7 @@ app.get('/api/shorts', requireAuth, async (req, res) => {
COALESCE((SELECT MAX(swm.delta) FROM student_work_marks swm WHERE swm.short_message_id = sm.id), 0) AS delta,
${studentIdSql} AS student_id
FROM short_messages sm LEFT JOIN users u ON u.id = sm.author_id
${where} ORDER BY sm.lesson_date DESC, sm.id DESC LIMIT $${limitParam} OFFSET $${offsetParam}`,
${where} ORDER BY sm.lesson_date ${orderAsc ? 'ASC' : 'DESC'}, sm.id ${orderAsc ? 'ASC' : 'DESC'} LIMIT $${limitParam} OFFSET $${offsetParam}`,
params
);
const { rows: cnt } = await pool.query(