aboutsummaryrefslogtreecommitdiff
path: root/bot/src/api/routes/quotes.js
blob: 4c584a9a97c98a7ce891e01e41cc0f6a4b22c2ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Router } from 'express';
import { pendingQuote, quote as newQuote } from '../../db/models/quote.js';
import { verifyToken } from './auth.js';

export function quoteRouter(client) {
    const router = Router();

    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' });
        }
    });

    router.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.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' });
        }
    });

    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 } });

                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' });
        }
    });

    return router;
}