chỉnh sửa dashboard quản lí của thành viên

This commit is contained in:
2026-06-08 15:46:10 +07:00
parent 46a1079f84
commit 703000bbfc
6 changed files with 836 additions and 80 deletions
+110
View File
@@ -8,6 +8,7 @@ const User = require('../models/User');
const Asset = require('../models/Asset');
const Scene = require('../models/Scene');
const Hotspot = require('../models/Hotspot'); // Giả định bạn đã tạo model mới
const Setting = require('../models/Setting');
const { protect, optionalAuth } = require('../middlewares/authMiddleware');
const { verifyReferer, setNoCacheHeaders } = require('../middlewares/securityMiddleware');
@@ -515,6 +516,115 @@ router.delete('/scenes/:id', protect, async (req, res) => {
}
});
/**
* @route GET /api/me/profile
* @desc Lấy thông tin hồ sơ người dùng hiện tại
* @access Private
*/
router.get('/me/profile', protect, async (req, res) => {
try {
const user = await User.findById(req.user._id).select('-password');
res.json(user);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route PUT /api/me/profile
* @desc Cập nhật hồ sơ (đổi tên, mật khẩu)
* @access Private
*/
router.put('/me/profile', protect, async (req, res) => {
try {
const user = await User.findById(req.user._id);
if (!user) return res.status(404).json({ message: 'User not found' });
if (req.body.username) user.username = req.body.username;
if (req.body.password) user.password = req.body.password; // Hook pre-save sẽ tự hash
await user.save();
res.json({
message: 'Hồ sơ đã được cập nhật',
user: { id: user._id, username: user.username, role: user.role }
});
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route GET /api/me/scenes
* @desc Lấy danh sách các cảnh mẹ do người dùng tạo
* @access Private
*/
router.get('/me/scenes', protect, async (req, res) => {
try {
const scenes = await Scene.find({ createdBy: req.user._id })
.populate('assetId')
.sort({ createdAt: -1 });
res.json(scenes);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route GET /api/me/assets
* @desc Lấy danh sách media của người dùng
* @access Private
*/
router.get('/me/assets', protect, async (req, res) => {
try {
const assets = await Asset.find({ uploadedBy: req.user._id }).sort({ createdAt: -1 });
res.json(assets);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route GET /api/admin/users
* @desc Lấy toàn bộ danh sách người dùng (Chỉ Admin)
* @access Private (Admin)
*/
router.get('/admin/users', protect, async (req, res) => {
if (req.user.role !== 'Chủ sở hữu') {
return res.status(403).json({ message: 'Bạn không có quyền truy cập quản trị' });
}
try {
const users = await User.find({}).select('-password').sort({ createdAt: -1 });
res.json(users);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route GET/PUT /api/system/settings
* @desc Quản lý thiết lập hệ thống
* @access Private (Admin)
*/
router.get('/system/settings', optionalAuth, async (req, res) => {
try {
let settings = await Setting.findOne();
if (!settings) settings = await Setting.create({});
res.json(settings);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
router.put('/system/settings', protect, async (req, res) => {
if (req.user.role !== 'Chủ sở hữu') return res.status(403).json({ message: 'Forbidden' });
try {
const settings = await Setting.findOneAndUpdate({}, req.body, { new: true, upsert: true });
res.json(settings);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route POST /api/maintenance/reset-all
* @desc Wipe all scenes, assets, and physical files (DANGEROUS: For dev reset only)