userDemote.js 732 B

12345678910111213141516171819202122232425262728
  1. const Route = require('../../structures/Route');
  2. class userDemote extends Route {
  3. constructor() {
  4. super('/admin/users/demote', 'post', { adminOnly: true });
  5. }
  6. async run(req, res, db, user) {
  7. if (!req.body) return res.status(400).json({ message: 'No body provided' });
  8. const { id } = req.body;
  9. if (!id) return res.status(400).json({ message: 'No id provided' });
  10. if (id === user.id) return res.status(400).json({ message: 'You can\'t apply this action to yourself' });
  11. try {
  12. await db.table('users')
  13. .where({ id })
  14. .update({ isAdmin: false });
  15. } catch (error) {
  16. return super.error(res, error);
  17. }
  18. return res.json({
  19. message: 'Successfully demoted user'
  20. });
  21. }
  22. }
  23. module.exports = userDemote;