From c7829b613fbe86d0cf413ae31c5ed06feb71238d Mon Sep 17 00:00:00 2001 From: Geoffrey Wu Date: Mon, 16 Oct 2023 21:29:51 -0400 Subject: [PATCH] reorganize /api/admin --- routes/api/admin.js | 86 -------------------------- routes/api/admin/geoword.js | 51 +++++++++++++++ routes/api/admin/index.js | 33 ++++++++++ routes/api/admin/list-reports.js | 12 ++++ routes/api/admin/update-subcategory.js | 19 ++++++ routes/api/index.js | 2 +- 6 files changed, 116 insertions(+), 87 deletions(-) delete mode 100644 routes/api/admin.js create mode 100644 routes/api/admin/geoword.js create mode 100644 routes/api/admin/index.js create mode 100644 routes/api/admin/list-reports.js create mode 100644 routes/api/admin/update-subcategory.js diff --git a/routes/api/admin.js b/routes/api/admin.js deleted file mode 100644 index fa8bf8043..000000000 --- a/routes/api/admin.js +++ /dev/null @@ -1,86 +0,0 @@ -import * as geoword from '../../database/geoword.js'; -import { getReports, updateSubcategory } from '../../database/questions.js'; -import { getUserId, isAdmin } from '../../database/users.js'; -import { checkToken } from '../../server/authentication.js'; - -import { Router } from 'express'; -import { ObjectId } from 'mongodb'; - -const router = Router(); - -router.use(async (req, res, next) => { - const { username, token } = req.session; - if (!checkToken(username, token)) { - delete req.session; - res.redirect('/geoword/login'); - return; - } - - const admin = await isAdmin(username); - if (!admin) { - res.status(403).redirect('/user/my-profile'); - return; - } - - next(); -}); - -router.get('/list-reports', async (req, res) => { - const { reason } = req.query; - return res.json(await getReports(reason)); -}); - -router.get('/geoword/compare', async (req, res) => { - const { packetName, division, player1, player2 } = req.query; - const player1Buzzes = await geoword.getBuzzes(packetName, division, await getUserId(player1)); - const player2Buzzes = await geoword.getBuzzes(packetName, division, await getUserId(player2)); - res.json({ player1Buzzes, player2Buzzes }); -}); - -router.get('/geoword/leaderboard', async (req, res) => { - const { packetName, division, includeInactive } = req.query; - const leaderboard = await geoword.getLeaderboard(packetName, division, includeInactive === 'true'); - res.json({ leaderboard }); -}); - -router.get('/geoword/player-list', async (req, res) => { - const { packetName, division } = req.query; - const players = await geoword.getPlayerList(packetName, division); - res.json({ players }); -}); - -router.get('/geoword/protests', async (req, res) => { - const { packetName, division } = req.query; - const { protests, packet } = await geoword.getProtests(packetName, division); - res.json({ protests, packet }); -}); - -router.post('/geoword/resolve-protest', async (req, res) => { - const { buzz_id, decision, reason } = req.body; - const result = await geoword.resolveProtest(new ObjectId(buzz_id), decision, reason); - - if (result) { - res.sendStatus(200); - } else { - res.sendStatus(500); - } -}); - -router.get('/geoword/stats', async (req, res) => { - const { packetName, division } = req.query; - const stats = await geoword.getAdminStats(packetName, division); - res.json({ stats }); -}); - -router.put('/update-subcategory', async (req, res) => { - const { _id, type, subcategory } = req.body; - const result = await updateSubcategory(new ObjectId(_id), type, subcategory); - - if (result) { - res.sendStatus(200); - } else { - res.sendStatus(500); - } -}); - -export default router; diff --git a/routes/api/admin/geoword.js b/routes/api/admin/geoword.js new file mode 100644 index 000000000..d8186e5b6 --- /dev/null +++ b/routes/api/admin/geoword.js @@ -0,0 +1,51 @@ +import { getAdminStats, getBuzzes, getLeaderboard, getPlayerList, getProtests, resolveProtest } from '../../../database/geoword.js'; +import { getUserId } from '../../../database/users.js'; + +import { Router } from 'express'; +import { ObjectId } from 'mongodb'; + +const router = Router(); + +router.get('/compare', async (req, res) => { + const { packetName, division, player1, player2 } = req.query; + const player1Buzzes = await getBuzzes(packetName, division, await getUserId(player1)); + const player2Buzzes = await getBuzzes(packetName, division, await getUserId(player2)); + res.json({ player1Buzzes, player2Buzzes }); +}); + +router.get('/leaderboard', async (req, res) => { + const { packetName, division, includeInactive } = req.query; + const leaderboard = await getLeaderboard(packetName, division, includeInactive === 'true'); + res.json({ leaderboard }); +}); + +router.get('/player-list', async (req, res) => { + const { packetName, division } = req.query; + const players = await getPlayerList(packetName, division); + res.json({ players }); +}); + +router.get('/protests', async (req, res) => { + const { packetName, division } = req.query; + const { protests, packet } = await getProtests(packetName, division); + res.json({ protests, packet }); +}); + +router.post('/resolve-protest', async (req, res) => { + const { buzz_id, decision, reason } = req.body; + const result = await resolveProtest(new ObjectId(buzz_id), decision, reason); + + if (result) { + res.sendStatus(200); + } else { + res.sendStatus(500); + } +}); + +router.get('/stats', async (req, res) => { + const { packetName, division } = req.query; + const stats = await getAdminStats(packetName, division); + res.json({ stats }); +}); + +export default router; diff --git a/routes/api/admin/index.js b/routes/api/admin/index.js new file mode 100644 index 000000000..064f625be --- /dev/null +++ b/routes/api/admin/index.js @@ -0,0 +1,33 @@ +import geowordRouter from './geoword.js'; +import listReportsRouter from './list-reports.js'; +import updateSubcategoryRouter from './update-subcategory.js'; + +import { isAdmin } from '../../../database/users.js'; +import { checkToken } from '../../../server/authentication.js'; + +import { Router } from 'express'; + +const router = Router(); + +router.use(async (req, res, next) => { + const { username, token } = req.session; + if (!checkToken(username, token)) { + delete req.session; + res.redirect('/geoword/login'); + return; + } + + const admin = await isAdmin(username); + if (!admin) { + res.status(403).redirect('/user/my-profile'); + return; + } + + next(); +}); + +router.use('/geoword', geowordRouter); +router.use('/list-reports', listReportsRouter); +router.use('/update-subcategory', updateSubcategoryRouter); + +export default router; diff --git a/routes/api/admin/list-reports.js b/routes/api/admin/list-reports.js new file mode 100644 index 000000000..824ce9504 --- /dev/null +++ b/routes/api/admin/list-reports.js @@ -0,0 +1,12 @@ +import { getReports } from '../../../database/questions.js'; + +import { Router } from 'express'; + +const router = Router(); + +router.get('/', async (req, res) => { + const { reason } = req.query; + return res.json(await getReports(reason)); +}); + +export default router; diff --git a/routes/api/admin/update-subcategory.js b/routes/api/admin/update-subcategory.js new file mode 100644 index 000000000..a880b4708 --- /dev/null +++ b/routes/api/admin/update-subcategory.js @@ -0,0 +1,19 @@ +import { updateSubcategory } from '../../../database/questions.js'; + +import { Router } from 'express'; +import { ObjectId } from 'mongodb'; + +const router = Router(); + +router.put('/', async (req, res) => { + const { _id, type, subcategory } = req.body; + const result = await updateSubcategory(new ObjectId(_id), type, subcategory); + + if (result) { + res.sendStatus(200); + } else { + res.sendStatus(500); + } +}); + +export default router; diff --git a/routes/api/index.js b/routes/api/index.js index 8a7f5dddc..9667ef3f2 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -1,4 +1,4 @@ -import adminRouter from './admin.js'; +import adminRouter from './admin/index.js'; import checkAnswerRouter from './check-answer.js'; import geowordRouter from './geoword/index.js'; import multiplayerRouter from './multiplayer.js';