fix(shorts): правильный LIMIT/OFFSET и область видимости филиалов в /api/shorts
This commit is contained in:
@@ -3478,6 +3478,9 @@ app.get('/api/shorts', requireAuth, async (req, res) => {
|
|||||||
params.push(`%${search}%`);
|
params.push(`%${search}%`);
|
||||||
where += ` AND (sm.resident ILIKE $${params.length} OR sm.topic ILIKE $${params.length} OR sm.message ILIKE $${params.length})`;
|
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);
|
params.push(limit, offset);
|
||||||
const scope = branchScope(req.user);
|
const scope = branchScope(req.user);
|
||||||
let studentIdSql;
|
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)`;
|
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) {
|
} else if (scope.ids.length) {
|
||||||
params.push(scope.ids);
|
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
|
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
|
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 {
|
} 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)`;
|
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,
|
COALESCE((SELECT MAX(swm.delta) FROM student_work_marks swm WHERE swm.short_message_id = sm.id), 0) AS delta,
|
||||||
${studentIdSql} AS student_id
|
${studentIdSql} AS student_id
|
||||||
FROM short_messages sm LEFT JOIN users u ON u.id = sm.author_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
|
params
|
||||||
);
|
);
|
||||||
const { rows: cnt } = await pool.query(
|
const { rows: cnt } = await pool.query(
|
||||||
`SELECT COUNT(*)::int AS n FROM short_messages sm ${where}`,
|
`SELECT COUNT(*)::int AS n FROM short_messages sm ${where}`,
|
||||||
params.slice(0, -2)
|
countParams
|
||||||
);
|
);
|
||||||
res.json({ items: rows, total: cnt[0]?.n || 0 });
|
res.json({ items: rows, total: cnt[0]?.n || 0 });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user