Вынести HTTP-слой учеников в lib/student-routes.js
- registerStudentRoutes / registerStudentFaceRoutes / registerStudentApiRoutes забирают все роуты /api/students*, /api/export/student, эталоны и найденные лица ученика, /api/v1/students* из server.js - ensureStudentPhotosTable и createStudentProfileAccess перенесены в lib/students.js - AGENTS.md: задокументированы lib/students.js и lib/student-routes.js в File Map
This commit is contained in:
+26
-1
@@ -6,6 +6,31 @@ function normalizeStudentStatus(value) {
|
||||
return STUDENT_STATUSES.includes(status) ? status : null;
|
||||
}
|
||||
|
||||
async function ensureStudentPhotosTable(pool) {
|
||||
await pool.query(`CREATE TABLE IF NOT EXISTS student_photos (
|
||||
id SERIAL PRIMARY KEY,
|
||||
student_id INT NOT NULL REFERENCES students(id) ON DELETE CASCADE,
|
||||
photo_path VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT now()
|
||||
)`);
|
||||
await pool.query('CREATE INDEX IF NOT EXISTS idx_student_photos_student_id ON student_photos(student_id)');
|
||||
}
|
||||
|
||||
function createStudentProfileAccess(pool, groupBelongsToBranches) {
|
||||
return async function studentProfileAccess(user, id) {
|
||||
const { rows } = await pool.query(
|
||||
'SELECT id, name, group_id, photo_path, profile FROM students WHERE id = $1 AND deleted_at IS NULL',
|
||||
[id]
|
||||
);
|
||||
if (!rows.length) return { found: false };
|
||||
const gid = rows[0].group_id;
|
||||
if (user.role !== 'admin' && gid && !(await groupBelongsToBranches(user, gid))) {
|
||||
return { found: false, forbidden: true };
|
||||
}
|
||||
return { found: true, student: rows[0] };
|
||||
};
|
||||
}
|
||||
|
||||
async function ensureStudentsTable(pool) {
|
||||
await pool.query(`ALTER TABLE students ADD COLUMN IF NOT EXISTS status VARCHAR(20) NOT NULL DEFAULT 'active'`);
|
||||
const { rows } = await pool.query(
|
||||
@@ -19,4 +44,4 @@ async function ensureStudentsTable(pool) {
|
||||
await pool.query(`ALTER TABLE students VALIDATE CONSTRAINT students_status_check`);
|
||||
}
|
||||
|
||||
module.exports = { STUDENT_STATUSES, normalizeStudentStatus, ensureStudentsTable };
|
||||
module.exports = { STUDENT_STATUSES, normalizeStudentStatus, ensureStudentsTable, ensureStudentPhotosTable, createStudentProfileAccess };
|
||||
|
||||
Reference in New Issue
Block a user