fix(shorts): правильный LIMIT/OFFSET и область видимости филиалов в /api/shorts

This commit is contained in:
dev
2026-10-13 01:20:00 +03:00
parent 2c0a797341
commit 055084b9c5
+7 -3
View File
@@ -3478,6 +3478,9 @@ 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 countParams = params.slice();
const limitParam = params.length + 1;
const offsetParam = params.length + 2;
params.push(limit, offset);
const scope = branchScope(req.user);
let studentIdSql;
@@ -3485,9 +3488,10 @@ app.get('/api/shorts', requireAuth, async (req, res) => {
studentIdSql = `(SELECT s.id FROM students s WHERE TRIM(s.name) = TRIM(sm.resident) AND s.deleted_at IS NULL LIMIT 1)`;
} else if (scope.ids.length) {
params.push(scope.ids);
const branchParam = params.length;
studentIdSql = `(SELECT s.id FROM students s LEFT JOIN groups g ON g.id = s.group_id
WHERE TRIM(s.name) = TRIM(sm.resident) AND s.deleted_at IS NULL
AND (s.group_id IS NULL OR g.branch_id = ANY($${params.length}::int[])) LIMIT 1)`;
AND (s.group_id IS NULL OR g.branch_id = ANY($${branchParam}::int[])) LIMIT 1)`;
} else {
studentIdSql = `(SELECT s.id FROM students s WHERE TRIM(s.name) = TRIM(sm.resident) AND s.deleted_at IS NULL AND s.group_id IS NULL LIMIT 1)`;
}
@@ -3497,12 +3501,12 @@ 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 $${params.length - 1} OFFSET $${params.length}`,
${where} ORDER BY sm.lesson_date DESC, sm.id DESC LIMIT $${limitParam} OFFSET $${offsetParam}`,
params
);
const { rows: cnt } = await pool.query(
`SELECT COUNT(*)::int AS n FROM short_messages sm ${where}`,
params.slice(0, -2)
countParams
);
res.json({ items: rows, total: cnt[0]?.n || 0 });
} catch (e) {