feat(students): add active/paused/left status with hidden filter
This commit is contained in:
@@ -44,6 +44,7 @@ const {
|
||||
tarEntryFilter,
|
||||
sweepRestoreTempDirs,
|
||||
} = require('./lib/restore');
|
||||
const { normalizeStudentStatus, ensureStudentsTable } = require('./lib/students');
|
||||
|
||||
const https = require('https');
|
||||
const path = require('path');
|
||||
@@ -6327,8 +6328,8 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
|
||||
}
|
||||
for (const x of ndata.students) {
|
||||
await client.query(
|
||||
'INSERT INTO students (id, name, created_at, group_id, photo_path, profile, deleted_at, purge_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
|
||||
[x.id, x.name, x.created_at, x.group_id, x.photo_path, x.profile ? JSON.stringify(x.profile) : null, x.deleted_at, x.purge_at]
|
||||
'INSERT INTO students (id, name, created_at, group_id, photo_path, profile, status, deleted_at, purge_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)',
|
||||
[x.id, x.name, x.created_at, x.group_id, x.photo_path, x.profile ? JSON.stringify(x.profile) : null, x.status || 'active', x.deleted_at, x.purge_at]
|
||||
);
|
||||
}
|
||||
for (const x of (ndata.student_face_refs || [])) {
|
||||
@@ -8429,7 +8430,7 @@ app.post('/api/students', requireAuth, async (req, res) => {
|
||||
} catch (e) {
|
||||
if (e.code === '23505') {
|
||||
const { rows: revived } = await pool.query(
|
||||
'UPDATE students SET deleted_at = NULL, purge_at = NULL, group_id = $2 WHERE name = $1 AND deleted_at IS NOT NULL RETURNING *',
|
||||
'UPDATE students SET deleted_at = NULL, purge_at = NULL, group_id = $2, status = \'active\' WHERE name = $1 AND deleted_at IS NOT NULL RETURNING *',
|
||||
[name.trim(), gid]
|
||||
);
|
||||
if (revived.length) {
|
||||
@@ -8445,8 +8446,10 @@ app.post('/api/students', requireAuth, async (req, res) => {
|
||||
});
|
||||
|
||||
app.put('/api/students/:id', requireAuth, async (req, res) => {
|
||||
const { name, group_id } = req.body;
|
||||
const { name, group_id, status } = req.body;
|
||||
if (!name?.trim()) return res.status(400).json({ error: 'Name required' });
|
||||
const nextStatus = normalizeStudentStatus(status);
|
||||
if (nextStatus === null) return res.status(400).json({ error: 'Неверный статус ученика' });
|
||||
const newGid = group_id === undefined || group_id === null || group_id === '' ? null : Number(group_id);
|
||||
if (req.user.role !== 'admin') {
|
||||
const { rows: cur } = await pool.query(
|
||||
@@ -8466,12 +8469,13 @@ app.put('/api/students/:id', requireAuth, async (req, res) => {
|
||||
const { rows } = await pool.query(
|
||||
`UPDATE students SET
|
||||
name = $1,
|
||||
group_id = $2
|
||||
WHERE id = $3 AND deleted_at IS NULL RETURNING *`,
|
||||
[name.trim(), newGid, req.params.id]
|
||||
group_id = $2,
|
||||
status = COALESCE($3::text, status)
|
||||
WHERE id = $4 AND deleted_at IS NULL RETURNING *`,
|
||||
[name.trim(), newGid, nextStatus || null, req.params.id]
|
||||
);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||
await logAudit(req, 'student.update', { id: req.params.id, name: name.trim() });
|
||||
await logAudit(req, 'student.update', { id: req.params.id, name: name.trim(), status: rows[0].status });
|
||||
invalidateStudents();
|
||||
res.json(rows[0]);
|
||||
} catch (e) {
|
||||
@@ -12748,7 +12752,7 @@ apiV1.get('/students', async (req, res) => {
|
||||
params
|
||||
);
|
||||
const total = crows[0].n;
|
||||
const q = `SELECT s.id, s.name, s.group_id, g.name AS group_name, s.photo_path, s.created_at
|
||||
const q = `SELECT s.id, s.name, s.group_id, g.name AS group_name, s.photo_path, s.status, s.created_at
|
||||
FROM students s
|
||||
LEFT JOIN groups g ON g.id = s.group_id
|
||||
WHERE s.deleted_at IS NULL${extra}${bw.where}
|
||||
@@ -12760,7 +12764,7 @@ apiV1.get('/students', async (req, res) => {
|
||||
|
||||
apiV1.get('/students/:id', async (req, res) => {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT s.id, s.name, s.group_id, g.name AS group_name, s.photo_path, s.profile, s.created_at
|
||||
`SELECT s.id, s.name, s.group_id, g.name AS group_name, s.photo_path, s.profile, s.status, s.created_at
|
||||
FROM students s
|
||||
LEFT JOIN groups g ON g.id = s.group_id
|
||||
WHERE s.deleted_at IS NULL AND s.id = $1`,
|
||||
@@ -13135,6 +13139,8 @@ apiV1.post('/students', apiWrite('write'), async (req, res) => {
|
||||
|
||||
apiV1.put('/students/:id', apiWrite('write'), async (req, res) => {
|
||||
const name = reqStr(req.body?.name, 150);
|
||||
const nextStatus = normalizeStudentStatus(req.body?.status);
|
||||
if (nextStatus === null) return res.status(400).json({ error: 'Неверный статус ученика' });
|
||||
const cur = await pool.query('SELECT id, group_id FROM students WHERE id = $1 AND deleted_at IS NULL', [req.params.id]);
|
||||
if (!cur.rows.length) return res.status(404).json({ error: 'Not found' });
|
||||
if (req.user.role !== 'admin' && cur.rows[0].group_id && !(await groupBelongsToBranches(req.user, cur.rows[0].group_id))) {
|
||||
@@ -13146,9 +13152,9 @@ apiV1.put('/students/:id', apiWrite('write'), async (req, res) => {
|
||||
if (grp.error) return res.status(grp.code || 400).json({ error: grp.error });
|
||||
gid = grp.group.id;
|
||||
}
|
||||
const { rows } = await pool.query('UPDATE students SET name = $1, group_id = $2 WHERE id = $3 AND deleted_at IS NULL RETURNING *', [name, gid, req.params.id]);
|
||||
const { rows } = await pool.query('UPDATE students SET name = $1, group_id = $2, status = COALESCE($3::text, status) WHERE id = $4 AND deleted_at IS NULL RETURNING *', [name, gid, nextStatus || null, req.params.id]);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||
await apiAudit(req, 'api.student.update', { id: rows[0].id, name });
|
||||
await apiAudit(req, 'api.student.update', { id: rows[0].id, name, status: rows[0].status });
|
||||
invalidateStudents();
|
||||
res.json(rows[0]);
|
||||
});
|
||||
@@ -13313,6 +13319,7 @@ if (fs.existsSync(certPath) && fs.existsSync(keyPath)) {
|
||||
try { await ensureModulesTable(); } catch (err) { console.error('Modules table:', err); }
|
||||
try { await ensureEntryPhotosTable(); } catch (err) { console.error('Entry photos table:', err); }
|
||||
try { await ensureStudentPhotosTable(); } catch (err) { console.error('Student photos table:', err); }
|
||||
try { await ensureStudentsTable(pool); } catch (err) { console.error('Students table:', err); }
|
||||
try { await ensurePhotoOriginalColumn(); } catch (err) { console.error('Entry original photo column:', err); }
|
||||
try { await ensureFaceTables(); } catch (err) { console.error('Face tables:', err); }
|
||||
try { await ensureEntryAiColumns(); } catch (err) { console.error('Entry AI columns:', err); }
|
||||
|
||||
Reference in New Issue
Block a user