mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
added and fix some of things
Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
@@ -705,6 +705,70 @@ exports.unbanUser = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
// ─── MAKE ADMIN ───────────────────────────────────────────────────────────────
|
||||
|
||||
exports.makeAdmin = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
if (Number(id) === req.user.user_id)
|
||||
return R.error(res, 'You cannot change your own role.', 400);
|
||||
|
||||
const user = await mdl_Users.findByPk(id);
|
||||
if (!user) return R.error(res, 'User not found.', 404);
|
||||
if (user.acc_type === 'admin')
|
||||
return R.error(res, 'User is already an administrator.', 400);
|
||||
|
||||
await user.update({ acc_type: 'admin', updatedBy: req.user.user_id });
|
||||
|
||||
logActivity(req.user.user_id, 'make_admin', { entityType: 'user', entityId: Number(id) });
|
||||
|
||||
const fullName = user.personal_info?.name?.full_name ?? 'User';
|
||||
sendEmail({
|
||||
to: user.email,
|
||||
type: 'MADE_ADMIN',
|
||||
data: { name: fullName.split(' ')[0], email: user.email },
|
||||
}).catch((err) => console.error('[ADMIN][MAKE ADMIN] Email failed:', err));
|
||||
|
||||
return R.success(res, 'User promoted to Administrator.');
|
||||
} catch (err) {
|
||||
console.error('[ADMIN][MAKE ADMIN]', err);
|
||||
return R.error(res, 'Could not update user role.', 500);
|
||||
}
|
||||
};
|
||||
|
||||
// ─── DEMOTE ADMIN ─────────────────────────────────────────────────────────────
|
||||
|
||||
exports.demoteAdmin = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
if (Number(id) === req.user.user_id)
|
||||
return R.error(res, 'You cannot change your own role.', 400);
|
||||
|
||||
const user = await mdl_Users.findByPk(id);
|
||||
if (!user) return R.error(res, 'User not found.', 404);
|
||||
if (user.acc_type !== 'admin')
|
||||
return R.error(res, 'User is not an administrator.', 400);
|
||||
|
||||
await user.update({ acc_type: 'user', updatedBy: req.user.user_id });
|
||||
|
||||
logActivity(req.user.user_id, 'demote_admin', { entityType: 'user', entityId: Number(id) });
|
||||
|
||||
const fullName = user.personal_info?.name?.full_name ?? 'User';
|
||||
sendEmail({
|
||||
to: user.email,
|
||||
type: 'DEMOTED_ADMIN',
|
||||
data: { name: fullName.split(' ')[0], email: user.email },
|
||||
}).catch((err) => console.error('[ADMIN][DEMOTE ADMIN] Email failed:', err));
|
||||
|
||||
return R.success(res, 'Administrator access removed.');
|
||||
} catch (err) {
|
||||
console.error('[ADMIN][DEMOTE ADMIN]', err);
|
||||
return R.error(res, 'Could not update user role.', 500);
|
||||
}
|
||||
};
|
||||
|
||||
// ─── BULK BAN ─────────────────────────────────────────────────────────────────
|
||||
|
||||
exports.bulkBanUsers = async (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user