students: страница ученика с журналом, сообщениями, отзывами, фото и лицами
Агрегат GET /api/students/:id/overview (requireAuth) — один запрос на страницу: ученик с профилем, записи журнала (с фото), короткие сообщения, отзывы, фото и сводка по лицам. Идентификация по имени (=), филиалы через branchScope. Новая страница public/student.html + public/js/student.js: шапка с профилем, карточки-счётчики, табы (Журнал/Короткие сообщения/Обратная связь/Фото/Лица с ленивой загрузкой), просмотр фото с закрытием по клику и Escape. Кнопка «Страница» в списке учеников (data-open-student).
This commit is contained in:
@@ -7707,6 +7707,112 @@ app.get('/api/students/:id/photos', requireAuth, async (req, res) => {
|
||||
res.json({ photos: rows, photo_path: acc.student.photo_path || null });
|
||||
});
|
||||
|
||||
app.get('/api/students/:id/overview', requireAuth, async (req, res) => {
|
||||
let id;
|
||||
try {
|
||||
id = reqInt(req.params.id);
|
||||
} catch {
|
||||
return res.status(400).json({ error: 'Неверный id ученика' });
|
||||
}
|
||||
const acc = await studentProfileAccess(req.user, id);
|
||||
if (!acc.found) {
|
||||
return res.status(acc.forbidden ? 403 : 404).json({ error: acc.forbidden ? 'Нет доступа к этому ученику' : 'Ученик не найден' });
|
||||
}
|
||||
const student = acc.student;
|
||||
const name = student.name;
|
||||
const LIST_LIMIT = 200;
|
||||
try {
|
||||
const bs = branchScope(req.user);
|
||||
const eConds = ['e.student_name = $1', 'e.deleted_at IS NULL'];
|
||||
const eParams = [name];
|
||||
if (bs.ids) {
|
||||
if (!bs.ids.length) eConds.push('1 = 0');
|
||||
else { eParams.push(bs.ids); eConds.push(`g.branch_id = ANY($${eParams.length}::int[])`); }
|
||||
}
|
||||
const eWhere = eConds.join(' AND ');
|
||||
|
||||
const fConds = ['f.resident = $1', 'f.deleted_at IS NULL'];
|
||||
const fParams = [name];
|
||||
if (bs.ids) {
|
||||
if (!bs.ids.length) fConds.push('1 = 0');
|
||||
else { fParams.push(bs.ids); fConds.push(`f.branch_id = ANY($${fParams.length}::int[])`); }
|
||||
}
|
||||
const fWhere = fConds.join(' AND ');
|
||||
|
||||
const [groupRes, entriesTotalRes, entriesRes, fbTotalRes, fbRes, shortsTotalRes, shortsRes, photosRes, facesRes] = await Promise.all([
|
||||
student.group_id ? pool.query('SELECT id, name FROM groups WHERE id = $1', [student.group_id]) : Promise.resolve({ rows: [] }),
|
||||
pool.query(`SELECT count(*)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE ${eWhere}`, eParams),
|
||||
(() => {
|
||||
const p = eParams.slice();
|
||||
p.push(LIST_LIMIT);
|
||||
return pool.query(
|
||||
`SELECT e.id, e.description, e.created_at, e.ai_status, e.module_id, e.group_id, e.photo_path,
|
||||
g.name AS group_name, m.name AS module_name
|
||||
FROM entries e JOIN groups g ON g.id = e.group_id
|
||||
LEFT JOIN modules m ON m.id = e.module_id
|
||||
WHERE ${eWhere} ORDER BY e.created_at DESC LIMIT $${p.length}`,
|
||||
p
|
||||
);
|
||||
})(),
|
||||
pool.query(`SELECT count(*)::int AS n FROM feedbacks f WHERE ${fWhere}`, fParams),
|
||||
(() => {
|
||||
const p = fParams.slice();
|
||||
p.push(LIST_LIMIT);
|
||||
return pool.query(
|
||||
`SELECT f.id, f.feedback_date, f.topic, f.text, f.ai_status, f.sent_at, f.group_id, f.created_at, f.author_id,
|
||||
g.name AS group_name
|
||||
FROM feedbacks f LEFT JOIN groups g ON g.id = f.group_id
|
||||
WHERE ${fWhere} ORDER BY f.feedback_date DESC NULLS LAST, f.id DESC LIMIT $${p.length}`,
|
||||
p
|
||||
);
|
||||
})(),
|
||||
pool.query('SELECT count(*)::int AS n FROM short_messages WHERE resident = $1 AND deleted_at IS NULL', [name]),
|
||||
pool.query(
|
||||
`SELECT sm.id, sm.resident, sm.lesson_date, sm.topic, sm.message, sm.used, sm.used_at, sm.created_at,
|
||||
u.name AS author_name, u.username AS author_username
|
||||
FROM short_messages sm LEFT JOIN users u ON u.id = sm.author_id
|
||||
WHERE sm.resident = $1 AND sm.deleted_at IS NULL
|
||||
ORDER BY sm.lesson_date DESC, sm.id DESC LIMIT $2`,
|
||||
[name, LIST_LIMIT]
|
||||
),
|
||||
pool.query('SELECT id, photo_path, created_at FROM student_photos WHERE student_id = $1 ORDER BY id DESC LIMIT $2', [id, LIST_LIMIT]),
|
||||
pool.query(
|
||||
`SELECT
|
||||
(SELECT count(*)::int FROM student_face_refs WHERE student_id = $1) AS refs,
|
||||
(SELECT count(*)::int FROM photo_faces WHERE student_id = $1) AS found,
|
||||
(SELECT count(*)::int FROM photo_faces WHERE student_id IS NULL AND best_student_id = $1 AND best_score >= $2) AS similar`,
|
||||
[id, FACE_CANDIDATE_MIN]
|
||||
),
|
||||
]);
|
||||
|
||||
const entryIds = entriesRes.rows.map(r => r.id);
|
||||
if (entryIds.length) {
|
||||
const pr = await pool.query(
|
||||
'SELECT id, entry_id, photo_path, caption FROM entry_photos WHERE entry_id = ANY($1) ORDER BY sort_order, id',
|
||||
[entryIds]
|
||||
);
|
||||
const byEntry = {};
|
||||
pr.rows.forEach(p => { (byEntry[p.entry_id] = byEntry[p.entry_id] || []).push(p); });
|
||||
entriesRes.rows.forEach(r => { r.photos = byEntry[r.id] || []; });
|
||||
} else {
|
||||
entriesRes.rows.forEach(r => { r.photos = []; });
|
||||
}
|
||||
|
||||
const faces = facesRes.rows[0] || {};
|
||||
res.json({
|
||||
student: { ...student, group_name: groupRes.rows[0]?.name || null, profile: student.profile || null },
|
||||
entries: { items: entriesRes.rows, total: entriesTotalRes.rows[0].n },
|
||||
feedbacks: { items: fbRes.rows, total: fbTotalRes.rows[0].n },
|
||||
shorts: { items: shortsRes.rows, total: shortsTotalRes.rows[0].n },
|
||||
photos: { items: photosRes.rows, photo_path: student.photo_path || null },
|
||||
faces: { refs: faces.refs || 0, found: faces.found || 0, similar: faces.similar || 0 },
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('student overview:', e);
|
||||
res.status(500).json({ error: 'Internal error' });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/students/:id/photos', requireAuth, (req, res, next) => {
|
||||
studentPhotoUpload(req, res, (err) => {
|
||||
if (err) {
|
||||
|
||||
Reference in New Issue
Block a user