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 = `