From c9d477b6c576c6ca85b40e3efa9cdc444797a7ac Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 28 Nov 2026 12:34:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D0=B8:=20=D1=86=D0=B2=D0=B5=D1=82=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=B8=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backup-restore.js | 2 ++ backup.selftest.js | 11 +++++++++++ db/init.sql | 1 + db/migration.sql | 2 ++ lib/users.js | 16 ++++++++++++++++ public/js/users.js | 19 ++++++++++++++++++- public/users.html | 10 ++++++++++ server.js | 32 +++++++++++++++++++------------- 8 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 lib/users.js diff --git a/backup-restore.js b/backup-restore.js index 5aec17c..7bec47b 100644 --- a/backup-restore.js +++ b/backup-restore.js @@ -1,4 +1,5 @@ const { normalizeStudentStatus } = require('./lib/students'); +const { normalizeUserColor } = require('./lib/users'); const PHOTO_JOB_ACTIONS = new Set(['ai', 'ai_face', 'ai_upscale', 'enhance', 'restore', 'rollback']); const LESSON_REPORT_TEXT_MAX = 5000; const LESSON_REPORT_TOPIC_MAX = 300; @@ -369,6 +370,7 @@ function normalizeRestoreData(data) { password_hash: reqStr(x.password_hash, 255), name: optStr(x.name, 150), avatar: optUploadPath(x.avatar, 255), + color: normalizeUserColor(x.color), role: (x.role === 'admin' || x.role === 'tutor') ? x.role : 'tutor', is_active: !!x.is_active, created_at: optTs(x.created_at), diff --git a/backup.selftest.js b/backup.selftest.js index c0c0f9f..f99f8a2 100644 --- a/backup.selftest.js +++ b/backup.selftest.js @@ -79,6 +79,17 @@ const userAvatar = normalizeRestoreData({ }); ok('users.avatar сохраняется', userAvatar.users[0].avatar === '/uploads/avatar.jpg', userAvatar.users[0]); +const userColor = normalizeRestoreData({ + ...base, + users: [{ id: 1, username: 'u', password_hash: 'h', color: '#ff5733' }], +}); +ok('users.color сохраняется', userColor.users[0].color === '#ff5733', userColor.users[0]); +const badUserColor = normalizeRestoreData({ + ...base, + users: [{ id: 1, username: 'u', password_hash: 'h', color: 'red' }], +}); +ok('users.color невалидный заменяется дефолтом', badUserColor.users[0].color === '#2563eb', badUserColor.users[0]); + const prompts = normalizeRestoreData({ ...base, user_settings: [{ user_id: 1, key: 'feedback_prompts', value: 'x'.repeat(20000) }], diff --git a/db/init.sql b/db/init.sql index 73187ca..97dc553 100644 --- a/db/init.sql +++ b/db/init.sql @@ -217,6 +217,7 @@ CREATE TABLE IF NOT EXISTS users ( email VARCHAR(255), phone VARCHAR(50), avatar VARCHAR(255), + color VARCHAR(7) NOT NULL DEFAULT '#2563eb', role VARCHAR(20) NOT NULL DEFAULT 'tutor' CHECK (role IN ('admin','tutor')), is_active BOOLEAN DEFAULT true, created_at TIMESTAMPTZ DEFAULT now() diff --git a/db/migration.sql b/db/migration.sql index 701f2bd..c647938 100644 --- a/db/migration.sql +++ b/db/migration.sql @@ -172,6 +172,7 @@ CREATE TABLE IF NOT EXISTS users ( email VARCHAR(255), phone VARCHAR(50), avatar VARCHAR(255), + color VARCHAR(7) NOT NULL DEFAULT '#2563eb', role VARCHAR(20) NOT NULL DEFAULT 'tutor' CHECK (role IN ('admin','tutor')), is_active BOOLEAN DEFAULT true, created_at TIMESTAMPTZ DEFAULT now() @@ -179,6 +180,7 @@ CREATE TABLE IF NOT EXISTS users ( ALTER TABLE users ADD COLUMN IF NOT EXISTS email VARCHAR(255); ALTER TABLE users ADD COLUMN IF NOT EXISTS phone VARCHAR(50); ALTER TABLE users ADD COLUMN IF NOT EXISTS avatar VARCHAR(255); +ALTER TABLE users ADD COLUMN IF NOT EXISTS color VARCHAR(7) NOT NULL DEFAULT '#2563eb'; CREATE TABLE IF NOT EXISTS user_branches ( user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE, diff --git a/lib/users.js b/lib/users.js new file mode 100644 index 0000000..6a35e66 --- /dev/null +++ b/lib/users.js @@ -0,0 +1,16 @@ +const DEFAULT_USER_COLOR = '#2563eb'; +const USER_COLORS = ['#2563eb', '#7c3aed', '#db2777', '#dc2626', '#ea580c', '#d97706', '#16a34a', '#0d9488', '#0891b2', '#4f46e5', '#9333ea', '#475569']; + +function normalizeUserColor(value, fallback) { + const raw = String(value === undefined || value === null ? '' : value).trim(); + if (/^#[0-9a-fA-F]{6}$/.test(raw)) return raw.toLowerCase(); + const base = String(fallback === undefined || fallback === null ? '' : fallback).trim(); + if (/^#[0-9a-fA-F]{6}$/.test(base)) return base.toLowerCase(); + return DEFAULT_USER_COLOR; +} + +function randomUserColor() { + return USER_COLORS[Math.floor(Math.random() * USER_COLORS.length)]; +} + +module.exports = { DEFAULT_USER_COLOR, USER_COLORS, normalizeUserColor, randomUserColor }; diff --git a/public/js/users.js b/public/js/users.js index fa739ce..be0cb38 100644 --- a/public/js/users.js +++ b/public/js/users.js @@ -5,6 +5,9 @@ async function init() { document.getElementById('addUserBtn').addEventListener('click', () => openModal(null)); document.getElementById('uCancel').addEventListener('click', closeModal); document.getElementById('uSave').addEventListener('click', saveUser); + document.getElementById('uColorRandom').addEventListener('click', () => { + document.getElementById('uColor').value = randomUserColor(); + }); document.getElementById('uRole').addEventListener('change', () => { document.getElementById('uBranchesField').style.display = document.getElementById('uRole').value === 'admin' ? 'none' : ''; }); @@ -13,6 +16,16 @@ async function init() { let branches = []; let editingId = null; +const USER_COLORS = ['#2563eb', '#7c3aed', '#db2777', '#dc2626', '#ea580c', '#d97706', '#16a34a', '#0d9488', '#0891b2', '#4f46e5', '#9333ea', '#475569']; + +function userColor(value) { + const raw = String(value || '').trim(); + return /^#[0-9a-fA-F]{6}$/.test(raw) ? raw.toLowerCase() : USER_COLORS[0]; +} + +function randomUserColor() { + return USER_COLORS[Math.floor(Math.random() * USER_COLORS.length)]; +} async function loadUsers() { const [uRes, bRes] = await Promise.all([ @@ -37,11 +50,13 @@ function renderUsers(users) { const roleCls = u.role === 'admin' ? 'badge-admin' : 'badge-tutor'; const roleTxt = u.role === 'admin' ? 'Администратор' : 'Тутор'; const status = u.is_active ? 'Активен' : 'Заблокирован'; + const color = userColor(u.color); const delBtn = u.id === currentUser.id ? '' : ``; tr.innerHTML = ` ${u.id} + ${esc(u.username)} ${esc(u.name || '')} ${roleTxt} @@ -66,6 +81,7 @@ function openModal(user) { document.getElementById('userModalTitle').textContent = user ? `Пользователь: ${user.username}` : 'Новый пользователь'; document.getElementById('uUsername').value = user ? user.username : ''; document.getElementById('uName').value = user ? (user.name || '') : ''; + document.getElementById('uColor').value = user ? userColor(user.color) : randomUserColor(); document.getElementById('uRole').value = user ? user.role : 'tutor'; document.getElementById('uPassword').value = ''; document.getElementById('uActive').checked = user ? user.is_active : true; @@ -92,13 +108,14 @@ async function saveUser() { const username = document.getElementById('uUsername').value.trim(); const name = document.getElementById('uName').value.trim() || null; const role = document.getElementById('uRole').value; + const color = userColor(document.getElementById('uColor').value); const password = document.getElementById('uPassword').value; const is_active = document.getElementById('uActive').checked; const branchSel = document.getElementById('uBranches'); const branch_ids = role === 'admin' ? [] : [...branchSel.selectedOptions].map(o => Number(o.value)); if (!username) { showToast('Укажите логин'); return; } if (!editingId && password.length < 6) { showToast('Пароль должен быть не короче 6 символов'); return; } - const payload = { username, name, role, is_active, branch_ids }; + const payload = { username, name, role, is_active, branch_ids, color }; if (password) payload.password = password; const res = await fetch(editingId ? `${API}/api/users/${editingId}` : `${API}/api/users`, { method: editingId ? 'PUT' : 'POST', diff --git a/public/users.html b/public/users.html index 873956a..e70d1c5 100644 --- a/public/users.html +++ b/public/users.html @@ -26,6 +26,7 @@ ID + Цвет Логин Имя Роль @@ -53,6 +54,13 @@ +
+ +
+ + +
+