From d38f3a7780aecdd9546bc85df912baf666c43205 Mon Sep 17 00:00:00 2001 From: fahed Date: Mon, 23 Feb 2026 15:23:41 +0300 Subject: [PATCH] Unify users and team members into a single model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove team_role filter from GET /api/users/team — all users now appear in the team view - POST /api/users now accepts team_role, brands, phone, modules - PATCH /api/users/:id now accepts team_role, phone, brands, modules - Users without team_role display their system role as fallback Co-Authored-By: Claude Opus 4.6 --- server/server.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/server/server.js b/server/server.js index e42fb45..dac78c5 100644 --- a/server/server.js +++ b/server/server.js @@ -781,19 +781,26 @@ app.get('/api/users', requireAuth, requireRole('superadmin'), async (req, res) = }); app.post('/api/users', requireAuth, requireRole('superadmin'), async (req, res) => { - const { name, email, password, role, avatar } = req.body; - if (!name || !email || !password || !role) return res.status(400).json({ error: 'Name, email, password, and role are required' }); + const { name, email, password, role, avatar, team_role, brands, phone, modules } = req.body; + if (!name || !email || !role) return res.status(400).json({ error: 'Name, email, and role are required' }); if (!['superadmin', 'manager', 'contributor'].includes(role)) return res.status(400).json({ error: 'Invalid role' }); try { const existing = authDb.prepare('SELECT id FROM auth_credentials WHERE email = ?').get(email); if (existing) return res.status(409).json({ error: 'Email already exists' }); - const created = await nocodb.create('Users', { name, email, role, avatar: avatar || null }); - const passwordHash = await bcrypt.hash(password, 10); + const created = await nocodb.create('Users', { + name, email, role, avatar: avatar || null, + team_role: team_role || null, + brands: JSON.stringify(brands || []), + phone: phone || null, + modules: JSON.stringify(modules || ALL_MODULES), + }); + const defaultPassword = password || 'changeme123'; + const passwordHash = await bcrypt.hash(defaultPassword, 10); authDb.prepare('INSERT INTO auth_credentials (email, password_hash, nocodb_user_id) VALUES (?, ?, ?)').run(email, passwordHash, created.Id); const user = await nocodb.get('Users', created.Id); - res.status(201).json(user); + res.status(201).json({ ...user, id: user.Id, _id: user.Id }); } catch (err) { console.error('Create user error:', err); res.status(500).json({ error: 'Failed to create user' }); @@ -808,9 +815,11 @@ app.patch('/api/users/:id', requireAuth, requireRole('superadmin'), async (req, if (req.body.role && !['superadmin', 'manager', 'contributor'].includes(req.body.role)) return res.status(400).json({ error: 'Invalid role' }); const data = {}; - for (const f of ['name', 'email', 'role', 'avatar']) { + for (const f of ['name', 'email', 'role', 'avatar', 'team_role', 'phone']) { if (req.body[f] !== undefined) data[f] = req.body[f]; } + if (req.body.brands !== undefined) data.brands = JSON.stringify(req.body.brands); + if (req.body.modules !== undefined) data.modules = JSON.stringify(req.body.modules); if (req.body.password) { const hash = await bcrypt.hash(req.body.password, 10); @@ -848,7 +857,6 @@ app.delete('/api/users/:id', requireAuth, requireRole('superadmin'), async (req, app.get('/api/users/assignable', requireAuth, async (req, res) => { try { const users = await nocodb.list('Users', { - where: '(team_role,isnot,null)', sort: 'name', }); res.json(users.map(u => ({ ...u, id: u.Id, _id: u.Id }))); @@ -862,7 +870,6 @@ app.get('/api/users/assignable', requireAuth, async (req, res) => { app.get('/api/users/team', requireAuth, async (req, res) => { try { const users = await nocodb.list('Users', { - where: '(team_role,isnot,null)', sort: 'name', });