Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 41 additions & 39 deletions src/controllers/admin.users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ const updateUserRole = async (req, res, next) => {
updatedAt: updatedUser.updatedAt,
deletedAt: updatedUser.deletedAt,
}, 200, 'User role updated successfully');
} catch (error) {
next(error);
}
};

/**
* Suspend or activate a user account (admin only)
* @route PATCH /api/admin/users/:id/status
* @access Admin only
Expand Down Expand Up @@ -178,72 +184,68 @@ const restoreUser = async (req, res, next) => {
};

/**
* List all users (including soft-deleted) (admin only)
* List all users (paginated)
* @route GET /api/admin/users
* @access Admin only
*/
const listUsers = async (req, res, next) => {
try {
const { includeDeleted } = req.query;
const query = includeDeleted === 'true' ? {} : { deletedAt: null };

const users = await User.find(query)
.select('-password -refreshTokenHash -resetPasswordToken -emailVerificationToken')
.sort({ createdAt: -1 });

return sendSuccess(res, users, 200, 'Users retrieved successfully');
} catch (error) {
next(error);
}
};

/**
* Update a user role (admin only)
* @route PATCH /api/admin/users/:id/role
* @access Admin only
*/
const updateUserRole = async (req, res, next) => {
try {
const { id } = req.params;
const { role } = req.body;

const validRoles = ['user', 'admin'];
if (!role || !validRoles.includes(role)) {
return sendError(res, 'Role must be either user or admin', 400);
const {
page = 1,
limit = 10,
search,
role,
kycStatus,
} = req.query;

const query = { deletedAt: null };

if (search) {
query.$or = [
{ fullName: { $regex: search, $options: 'i' } },
{ email: { $regex: search, $options: 'i' } },
];
}

if (req.userId === id && role === 'user') {
return sendError(res, 'You cannot downgrade your own role', 403);
if (role) {
query.role = role;
}

const user = await User.findById(id);
if (!user) {
return sendError(res, 'User not found', 404);
if (kycStatus) {
query.kycStatus = kycStatus;
}

user.role = role;
await user.save();
const users = await User.find(query)
.select('-password -refreshTokenHash -resetPasswordToken -emailVerificationToken')
.sort({ createdAt: -1 })
.skip((page - 1) * limit)
.limit(parseInt(limit));

const total = await User.countDocuments(query);

return sendSuccess(
res,
{
id: user.id,
email: user.email,
role: user.role,
data: users,
total,
page: parseInt(page),
totalPages: Math.ceil(total / limit),
},
200,
'User role updated successfully'
'Users retrieved successfully'
);
} catch (error) {
next(error);
}
};



module.exports = {
deleteUser,
getUserById,
restoreUser,
listUsers,
updateUserStatus,
updateUserRole,
};
};
5 changes: 2 additions & 3 deletions src/routes/admin.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ router.post('/users/:id/restore', restoreUser);
router.patch('/kyc/:id', validate(reviewKycSchema), reviewKyc);
// PATCH /api/admin/users/:id/status - Suspend or activate a user
router.patch('/users/:id/status', updateUserStatus);
// PATCH /api/admin/users/:id/role - Update a user role
router.patch('/users/:id/role', updateUserRole);

module.exports = router;

module.exports = router;
Loading