costco-grocery-list/backend/controllers/users.controller.js
Nico 4d8c3cb2e4 Add roles enum within backend
Add more admin functions
2025-11-21 18:51:40 -08:00

41 lines
1013 B
JavaScript

const User = require("../models/user.model");
exports.getAllUsers = async (req, res) => {
const users = await User.getAllUsers();
res.json(users);
};
exports.updateUserRole = async (req, res) => {
try {
const { id } = req.params;
const { role } = req.body;
if (!Object.values(ROLES).includes(role))
return res.status(400).json({ error: "Invalid role" });
const updated = await User.updateUserRole(id, role);
if (!updated)
return res.status(404).json({ error: "User not found" });
res.json({ message: "Role updated", id, role });
} catch (err) {
res.status(500).json({ error: "Failed to update role" });
}
};
exports.deleteUser = async (req, res) => {
try {
const { id } = req.params;
const deleted = await User.deleteUser(id);
if (!deleted)
return res.status(404).json({ error: "User not found" });
res.json({ message: "User deleted", id });
} catch (err) {
res.status(500).json({ error: "Failed to delete user" });
}
};