From 7591ba8f2066fcfa02ac17d438caacee09c1200f Mon Sep 17 00:00:00 2001 From: Hiroyuki Date: Sun, 21 Feb 2021 23:47:41 -0400 Subject: [PATCH] Motion and EO endpoints --- .eslintrc.json | 3 +- src/api/board.ins/routes/root.ts | 90 +++++++++++++++++++++++++++++--- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 8e5b667..7cfeb4b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -39,7 +39,8 @@ "no-param-reassign": "off", "no-underscore-dangle": "off", "keyword-spacing": "off", - "no-multiple-empty-lines": "off" + "no-multiple-empty-lines": "off", + "consistent-return": "off" }, "ignorePatterns": "**/*.js" } diff --git a/src/api/board.ins/routes/root.ts b/src/api/board.ins/routes/root.ts index 51e4881..49da1cb 100644 --- a/src/api/board.ins/routes/root.ts +++ b/src/api/board.ins/routes/root.ts @@ -11,7 +11,7 @@ export default class Root extends Route { } public bind() { - this.router.post('/executive-orders', async (req, res) => { + this.router.post('/eo', async (req, res) => { if (!req.body.pin) { return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, @@ -59,13 +59,13 @@ export default class Root extends Route { const channel = this.server.client.getChannel('807444198969835550'); await channel.createMessage({ embed }); - return res.status(200).json({ + res.status(200).json({ code: this.constants.codes.SUCCESS, message: `Created new Executive Order with ID ${executiveOrder.oID} by ${staffDiscord.username}#${staffDiscord.discriminator}, ${staffInformation.pn.join(', ')}.`, }); }); - this.router.post('/motions', async (req, res) => { + this.router.post('/motion', async (req, res) => { if (!req.body.pin) { return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, @@ -119,13 +119,89 @@ export default class Root extends Route { processed: false, }); - return res.status(200).json({ + res.status(200).json({ code: this.constants.codes.SUCCESS, message: `Created new Motion with ID ${motion.oID} by ${staffDiscord.username}#${staffDiscord.discriminator}, ${staffInformation.pn.join(', ')}.`, }); }); - this.router.get('/motions', async (_req, res) => { + this.router.delete('/eo/:id', async (req, res) => { + if (!req.params.id) return res.status(400).send(this.constants.messages.CLIENT_ERROR); + if (!(await this.server.client.db.ExecutiveOrder.exists({ oID: req.params.id }))) return res.status(404).send(this.constants.messages.NOT_FOUND); + + await this.server.client.db.ExecutiveOrder.deleteOne({ oID: req.params.id }); + + res.status(200).send({ message: `Executive Order with ID ${req.params.id} deleted.` }); + }); + + this.router.delete('/motion/:id', async (req, res) => { + if (!req.params.id) return res.status(400).send(this.constants.messages.CLIENT_ERROR); + if (!(await this.server.client.db.Motion.exists({ oID: req.params.id }))) return res.status(404).send(this.constants.messages.NOT_FOUND); + + await this.server.client.db.Motion.deleteOne({ oID: req.params.id }); + + res.status(200).send({ message: `Motion with ID ${req.params.id} deleted.` }); + }); + + this.router.get('/eo/:id', async (req, res) => { + if (!req.params.id) return res.status(400).send(this.constants.messages.CLIENT_ERROR); + if (!(await this.server.client.db.ExecutiveOrder.exists({ oID: req.params.id }))) return res.status(404).send(this.constants.messages.NOT_FOUND); + + const executiveOrder = await this.server.client.db.ExecutiveOrder.findOne({ oID: req.params.id }); + + res.status(200).send({ + issuedBy: executiveOrder.issuedBy, + id: executiveOrder.oID, + subject: executiveOrder.subject, + body: executiveOrder.body, + at: new Date(executiveOrder.at), + }); + }); + + this.router.get('/motion/:id', async (req, res) => { + if (!req.params.id) return res.status(400).send(this.constants.messages.CLIENT_ERROR); + if (!(await this.server.client.db.Motion.exists({ oID: req.params.id }))) return res.status(404).send(this.constants.messages.NOT_FOUND); + + const motion = await this.server.client.db.Motion.findOne({ oID: req.params.id }); + + res.status(200).send({ + issuedBy: motion.issuedBy, + id: motion.oID, + subject: motion.subject, + body: motion.body, + at: new Date(motion.at), + }); + }); + + this.router.patch('/eo/:id', async (req, res) => { + if (!req.params.id) return res.status(400).send(this.constants.messages.CLIENT_ERROR); + if (!(await this.server.client.db.ExecutiveOrder.exists({ oID: req.params.id }))) return res.status(404).send(this.constants.messages.NOT_FOUND); + if (!req.body.subject && !req.body.body) return res.status(400).send(this.constants.messages.CLIENT_ERROR); + + const executiveOrder = await this.server.client.db.ExecutiveOrder.findOne({ oID: req.params.id }); + await executiveOrder.updateOne({ + subject: req.body.subject || executiveOrder.subject, + body: req.body.body || executiveOrder.body, + }); + + res.status(200).send({ message: `Updated Executive Order with ID ${executiveOrder.oID}.` }); + }); + + this.router.patch('/motion/:id', async (req, res) => { + if (!req.params.id) return res.status(400).send(this.constants.messages.CLIENT_ERROR); + if (!(await this.server.client.db.Motion.exists({ oID: req.params.id }))) return res.status(404).send(this.constants.messages.NOT_FOUND); + if (!req.body.subject && !req.body.body) return res.status(400).send(this.constants.messages.CLIENT_ERROR); + + const motion = await this.server.client.db.Motion.findOne({ oID: req.params.id }); + await motion.updateOne({ + subject: req.body.subject || motion.subject, + body: req.body.body || motion.body, + }); + + res.status(200).send({ message: `Updated Motion with ID ${motion.oID}.` }); + }); + + this.router.get('/motion', async (_req, res) => { const motions = await this.server.client.db.Motion.find().lean(); res.status(200).send({ @@ -133,7 +209,7 @@ export default class Root extends Route { }); }); - this.router.get('/resolutions', async (_req, res) => { + this.router.get('/resolution', async (_req, res) => { const resolutions = await this.server.client.db.Resolution.find().lean(); res.status(200).send({ @@ -141,7 +217,7 @@ export default class Root extends Route { }); }); - this.router.get('/executive-orders', async (_req, res) => { + this.router.get('/eo', async (_req, res) => { const executiveOrders = await this.server.client.db.ExecutiveOrder.find().lean(); res.status(200).send({