33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
const pool = require('../config/db');
|
|
const { logActivity } = require('../services/activityLog');
|
|
|
|
exports.listUsers = async (req, res) => {
|
|
try {
|
|
const [rows] = await pool.query(
|
|
'SELECT id, prenom, nom, societe, email, telephone, role, actif, date_adhesion, date_echeance FROM users ORDER BY prenom ASC'
|
|
);
|
|
res.json(rows);
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Server error' });
|
|
}
|
|
};
|
|
|
|
exports.updateUserRole = async (req, res) => {
|
|
try {
|
|
const { role } = req.body;
|
|
if (!['admin', 'member'].includes(role)) {
|
|
return res.status(400).json({ error: 'Role must be admin or member' });
|
|
}
|
|
const userId = req.params.id;
|
|
if (Number(userId) === req.user.id) {
|
|
return res.status(400).json({ error: 'Vous ne pouvez pas modifier votre propre rôle' });
|
|
}
|
|
await pool.query('UPDATE users SET role = ? WHERE id = ?', [role, userId]);
|
|
logActivity(req, 'role_changement', { targetUserId: userId, newRole: role });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Server error' });
|
|
}
|
|
};
|