feat(sidebar): виды пунктов меню (item/divider/group), restore и upsert по id

- схема sidebar_items: колонка kind (item|divider|group), pagenullable, label с дефолтом '', constraint種別
- миграция для существующих БД: ADD COLUMN kind, DROP NOT NULL page, SET DEFAULT label, пересоздание constraint
- restore: upsert по id (обновление существующих, вставка новых, удаление отсутствующих вpayload)
- normalizeRestoreData: kind валидируется, filter по kind/item требует page+label
- фронт: добавление разделителя/группы, удаление по cid, порядок из DOM через _cid, payload с kind/id
This commit is contained in:
dev
2026-11-03 16:18:58 +03:00
parent 183ca47f17
commit 46f5e8ee95
9 changed files with 208 additions and 58 deletions
+27 -7
View File
@@ -6020,6 +6020,8 @@ app.get('/api/sidebar', requireAuth, async (req, res) => {
out = items.filter(i => i.section === 'base' && i.enabled && i.tutor_visible);
}
out = out.map(i => ({
id: i.id,
kind: i.kind || 'item',
page: i.page,
label: i.label,
icon: i.icon,
@@ -6042,12 +6044,30 @@ app.put('/api/sidebar', requireAdmin, async (req, res) => {
const client = await pool.connect();
try {
await client.query('BEGIN');
const existing = await client.query('SELECT id FROM sidebar_items');
const existingIds = new Set(existing.rows.map(r => r.id));
const submittedIds = [];
for (const it of normalized) {
await client.query(
`UPDATE sidebar_items SET label=$1, icon=$2, section=$3, sort_order=$4, tutor_visible=$5, enabled=$6, updated_at=now()
WHERE page=$7`,
[it.label, it.icon, it.section, it.sort_order, it.tutor_visible, it.enabled, it.page]
);
if (it.id && existingIds.has(it.id)) {
await client.query(
`UPDATE sidebar_items SET kind=$1, page=$2, label=$3, icon=$4, section=$5, sort_order=$6, tutor_visible=$7, enabled=$8, updated_at=now()
WHERE id=$9`,
[it.kind, it.page, it.label, it.icon, it.section, it.sort_order, it.tutor_visible, it.enabled, it.id]
);
submittedIds.push(it.id);
} else {
const ins = await client.query(
`INSERT INTO sidebar_items (kind, page, label, icon, section, sort_order, tutor_visible, enabled)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING id`,
[it.kind, it.page, it.label, it.icon, it.section, it.sort_order, it.tutor_visible, it.enabled]
);
submittedIds.push(ins.rows[0].id);
}
}
if (submittedIds.length) {
await client.query('DELETE FROM sidebar_items WHERE NOT (id = ANY($1::int[]))', [submittedIds]);
} else {
await client.query('DELETE FROM sidebar_items');
}
await client.query('COMMIT');
} catch (e) {
@@ -7195,8 +7215,8 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
}
for (const x of (ndata.sidebar_items || [])) {
await client.query(
'INSERT INTO sidebar_items (id, page, label, icon, section, sort_order, tutor_visible, enabled, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)',
[x.id, x.page, x.label, x.icon, x.section, x.sort_order, x.tutor_visible, x.enabled, x.created_at || null, x.updated_at || null]
'INSERT INTO sidebar_items (id, kind, page, label, icon, section, sort_order, tutor_visible, enabled, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)',
[x.id, x.kind || 'item', x.page, x.label, x.icon, x.section, x.sort_order, x.tutor_visible, x.enabled, x.created_at || null, x.updated_at || null]
);
}
for (const x of (ndata.doc_articles || [])) {