costco-grocery-list/backend/controllers/users.controller.js
2025-11-22 00:42:15 -08:00

42 lines
1.0 KiB
JavaScript

const User = require("../models/user.model");
exports.getAllUsers = async (req, res) => {
const users = await User.getAllUsers();
console.log(users);
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" });
}
};