Skip to content

Commit

Permalink
reorganize /api/admin
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Oct 17, 2023
1 parent 5a4005a commit c7829b6
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 87 deletions.
86 changes: 0 additions & 86 deletions routes/api/admin.js

This file was deleted.

51 changes: 51 additions & 0 deletions routes/api/admin/geoword.js
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 33 additions & 0 deletions routes/api/admin/index.js
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions routes/api/admin/list-reports.js
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 19 additions & 0 deletions routes/api/admin/update-subcategory.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion routes/api/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit c7829b6

Please sign in to comment.