aboutsummaryrefslogtreecommitdiff
path: root/bot/src/api/routes
diff options
context:
space:
mode:
Diffstat (limited to 'bot/src/api/routes')
-rw-r--r--bot/src/api/routes/quotes.js138
1 files changed, 78 insertions, 60 deletions
diff --git a/bot/src/api/routes/quotes.js b/bot/src/api/routes/quotes.js
index f362d7e..1b89f67 100644
--- a/bot/src/api/routes/quotes.js
+++ b/bot/src/api/routes/quotes.js
@@ -2,70 +2,88 @@ import { Router } from 'express';
import { pendingQuote, quote as newQuote } from '../../models/quote.js';
import { verifyToken } from './auth.js';
-export const quoteRouter = Router();
+export function quoteRouter(client) {
+ const router = Router();
-quoteRouter.get('/quotes/pending', verifyToken, async (req, res) => {
- try {
- const quotes = await pendingQuote.findAll();
- res.json(quotes);
- } catch (error) {
- console.error('Error fetching quotes:', error);
- res.status(500).send({ message: 'Internal Server Error' });
- }
-});
-
-quoteRouter.post('/quotes/add', verifyToken, async (req, res) => {
- const { author, authorImage, quote, year, submitterID } = req.body;
- try {
- await newQuote.create({
- author: author,
- authorImage: authorImage,
- quote: quote,
- year: year,
- submitter: submitterID
- });
- res.status(200).send({ message: 'Added a new quote' });
- } catch (error) {
- console.error('Something went wrong:', error);
- res.status(500).send({ message: 'Internal Server Error' });
- }
-});
+ router.get('/quotes/pending', verifyToken, async (req, res) => {
+ try {
+ const quotes = await pendingQuote.findAll();
+ res.json(quotes);
+ } catch (error) {
+ console.error('Error fetching quotes:', error);
+ res.status(500).send({ message: 'Internal Server Error' });
+ }
+ });
-quoteRouter.post('/quotes/approve', verifyToken, async (req, res) => {
- const { id } = req.body;
- try {
- const quote = await pendingQuote.findByPk(id);
- if (quote) {
+ router.post('/quotes/add', verifyToken, async (req, res) => {
+ const { author, authorImage, quote, year, submitterID } = req.body;
+ try {
await newQuote.create({
- author: quote.author,
- authorImage: quote.authorImage,
- quote: quote.quote,
- year: quote.year,
- submitter: quote.submitterID
+ author: author,
+ authorImage: authorImage,
+ quote: quote,
+ year: year,
+ submitter: submitterID
});
- await pendingQuote.destroy({ where: { id } });
- res.status(200).send({ message: 'Quote approved' });
- } else {
- res.status(404).send({ message: 'Quote not found' });
+ res.status(200).send({ message: 'Added a new quote' });
+ } catch (error) {
+ console.error('Something went wrong:', error);
+ res.status(500).send({ message: 'Internal Server Error' });
+ }
+ });
+
+ router.post('/quotes/approve', verifyToken, async (req, res) => {
+ const { id } = req.body;
+ try {
+ const quote = await pendingQuote.findByPk(id);
+ if (quote) {
+ await newQuote.create({
+ author: quote.author,
+ authorImage: quote.authorImage,
+ quote: quote.quote,
+ year: quote.year,
+ submitter: quote.submitterID
+ });
+
+ await pendingQuote.destroy({ where: {id} });
+ res.status(200).send({ message: 'Quote approved' });
+ } else {
+ res.status(404).send({ message: 'Quote not found '});
+ }
+ } catch (error) {
+ console.error('Error approving quote:', error);
+ res.status(500).send({ message: 'Internal Server Error' });
}
- } catch (error) {
- console.error('Error approving quote:', error);
- res.status(500).send({ message: 'Internal Server Error' });
- }
-});
+ });
+
+ router.post('/quotes/reject', verifyToken, async (req, res) => {
+ const { id } = req.body;
+ try {
+ const quote = await pendingQuote.findByPk(id);
+ if (quote) {
+ await pendingQuote.destroy({ where: {id} });
-quoteRouter.post('/quotes/reject', verifyToken, async (req, res) => {
- const { id } = req.body;
- try {
- const quote = await pendingQuote.findByPk(id);
- if (quote) {
- await pendingQuote.destroy({ where: { id } });
- res.status(200).send({ message: 'Quote rejected' });
- } else {
- res.status(404).send({ message: 'Quote not found' });
+ if (!req.body.silent) {
+ client.users.fetch(quote.submitterID).then((user) => {
+ if (req.body.reason) {
+ user.send(`Hello ${user.displayName},\nYour quote was rejected for the following reason:\n\`\`\`\n${req.body.reason}\n\`\`\``);
+ } else {
+ user.send(`Hello ${user.displayName},\nYour quote was rejected.`);
+ }
+ }).catch((err) => {
+ console.error('Error sending rejection message:', err);
+ });
+ }
+
+ res.status(200).send({ message: 'Quote rejected', reason: req.body.reason });
+ } else {
+ res.status(404).send({ message: 'Quote not found' });
+ }
+ } catch (error) {
+ console.error('Error rejecting quote:', error);
+ res.status(500).send({ message: 'Internal Server Error' });
}
- } catch (error) {
- console.error('Error rejecting quote:', error);
- res.status(500).send({ message: 'Internal Server Error' });
- }
-});
+ });
+
+ return router;
+}