From 36f0456750604aeb5c37fe0b06c396b4da707cde Mon Sep 17 00:00:00 2001 From: Hiroyuki Date: Sat, 20 Mar 2021 17:16:04 -0400 Subject: [PATCH] Final CR backend changes --- src/api/board.ins/routes/root.ts | 121 ++++++++++++++++++++++++++----- src/events/messageReactionAdd.ts | 2 +- 2 files changed, 103 insertions(+), 20 deletions(-) diff --git a/src/api/board.ins/routes/root.ts b/src/api/board.ins/routes/root.ts index 8f50c18..422f77b 100644 --- a/src/api/board.ins/routes/root.ts +++ b/src/api/board.ins/routes/root.ts @@ -91,7 +91,7 @@ export default class Root extends Route { const embed = new RichEmbed(); embed.setTitle(title); embed.setAuthor(`${director.member.username}#${director.member.discriminator}, ${director.user.pn.join(', ')}`, director.member.avatarURL); - embed.setDescription(`${id}\n\n_This action is available on the Board Register System Directory. You can make changes or edit it [here](https://board.ins/directory?id=${id})._`); + embed.setDescription(`${id}\n\n_This action is available on the Board Register System Directory. You can make changes or edit it [here](https://board.ins/repository)._`); embed.addField('Subject', payload.subject); embed.addField('Body', payload.body); embed.setColor(color); @@ -107,21 +107,89 @@ export default class Root extends Route { next(); }); - this.router.get('/', async (_req, res) => { - const eo = await this.server.client.db.ExecutiveOrder.countDocuments(); - const motion = await this.server.client.db.Motion.countDocuments(); - const proc = await this.server.client.db.Proclamation.countDocuments(); - const resolution = await this.server.client.db.Proclamation.countDocuments(); + this.router.get('/', async (req, res) => { + const page = !Number.isNaN(Number(req.query.page)) ? Number(req.query.page) : 0; + + const counts = { + eo: await this.server.client.db.ExecutiveOrder.countDocuments(), + motion: await this.server.client.db.Motion.countDocuments(), + proc: await this.server.client.db.Proclamation.countDocuments(), + resolution: await this.server.client.db.Resolution.countDocuments(), + }; + + const eo = await this.server.client.db.ExecutiveOrder.find().skip(page * 4); + const motion = await this.server.client.db.Motion.find().skip(page * 4); + const proc = await this.server.client.db.Proclamation.find().skip(page * 4); + const resolution = await this.server.client.db.Resolution.find().skip(page * 4); + + const returned: { + id: string; + author: string; + subject: string; + type: string; + at: number; + }[] = []; + + for (const item of eo) { + const member = this.guild.members.get(item.issuer); + const author = member ? `${member.username}#${member.discriminator}` : 'Deleted User#0000'; + + returned.push({ + id: item.oID, + author, + subject: item.subject, + type: 'Executive Order', + at: item.at, + }); + } + + for (const item of motion) { + const member = this.guild.members.get(item.issuer); + const author = member ? `${member.username}#${member.discriminator}` : 'Deleted User#0000'; + + returned.push({ + id: item.oID, + author, + subject: item.subject, + type: 'Motion', + at: item.at, + }); + } + + for (const item of proc) { + const member = this.guild.members.get(item.issuer); + const author = member ? `${member.username}#${member.discriminator}` : 'Deleted User#0000'; + + returned.push({ + id: item.oID, + author, + subject: item.subject, + type: 'Proclamation', + at: item.at, + }); + } + + for (const item of resolution) { + const member = this.guild.members.get(item.issuer); + const author = member ? `${member.username}#${member.discriminator}` : 'Deleted User#0000'; + + returned.push({ + id: item.oID, + author, + subject: item.subject, + type: 'Resolution', + at: item.at, + }); + } + + returned.slice(0, 12); + + const total = counts.eo + counts.motion + counts.proc + counts.resolution; res.status(200).json({ code: this.constants.codes.SUCCESS, - counts: { - eo, - motion, - proc, - resolution, - total: eo + motion + proc + resolution, - }, + results: returned, + total, }); }); @@ -340,7 +408,7 @@ export default class Root extends Route { }); } - if (motion.issuer !== authenticated.member.id && authenticated.member.id !== this.chairman) { + if ((motion.issuer !== authenticated.member.id || motion.processed) && authenticated.member.id !== this.chairman) { return res.status(403).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED, @@ -373,7 +441,7 @@ export default class Root extends Route { }); } - if (proc.issuer !== authenticated.member.id && authenticated.member.id !== this.chairman) { + if ((proc.issuer !== authenticated.member.id || proc.processed) && authenticated.member.id !== this.chairman) { return res.status(403).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED, @@ -399,6 +467,7 @@ export default class Root extends Route { } const issuer = this.guild.members.get(eo.issuer); + const director = await this.server.client.db.Staff.findOne({ userID: issuer.id }); res.status(200).json({ code: this.constants.codes.SUCCESS, @@ -406,6 +475,8 @@ export default class Root extends Route { username: issuer.username, discriminator: issuer.discriminator, avatarURL: issuer.avatarURL, + id: issuer.id, + pn: director.pn, }, subject: eo.subject, body: eo.body, @@ -425,6 +496,7 @@ export default class Root extends Route { } const issuer = this.guild.members.get(motion.issuer); + const director = await this.server.client.db.Staff.findOne({ userID: issuer.id }); res.status(200).json({ code: this.constants.codes.SUCCESS, @@ -432,12 +504,15 @@ export default class Root extends Route { username: issuer.username, discriminator: issuer.discriminator, avatarURL: issuer.avatarURL, + id: issuer.id, + pn: director.pn, }, subject: motion.subject, body: motion.body, issuedAt: motion.at, id: motion.oID, voteResults: motion.results || null, + processed: motion.processed, }); }); @@ -452,6 +527,7 @@ export default class Root extends Route { } const issuer = this.guild.members.get(proc.issuer); + const director = await this.server.client.db.Staff.findOne({ userID: issuer.id }); res.status(200).json({ code: this.constants.codes.SUCCESS, @@ -459,12 +535,15 @@ export default class Root extends Route { username: issuer.username, discriminator: issuer.discriminator, avatarURL: issuer.avatarURL, + id: issuer.id, + pn: director.pn, }, subject: proc.subject, body: proc.body, issuedAt: proc.at, id: proc.oID, voteResults: proc.results || null, + processed: proc.processed, }); }); @@ -479,6 +558,7 @@ export default class Root extends Route { } const issuer = this.guild.members.get(resolution.issuer); + const director = await this.server.client.db.Staff.findOne({ userID: issuer.id }); res.status(200).json({ code: this.constants.codes.SUCCESS, @@ -486,6 +566,8 @@ export default class Root extends Route { username: issuer.username, discriminator: issuer.discriminator, avatarURL: issuer.avatarURL, + id: issuer.id, + pn: director.pn, }, subject: resolution.subject, body: resolution.body, @@ -567,7 +649,7 @@ export default class Root extends Route { }); } - if (motion.issuer !== authenticated.member.id && authenticated.member.id !== this.chairman) { + if ((motion.issuer !== authenticated.member.id || motion.processed) && authenticated.member.id !== this.chairman) { return res.status(403).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED, @@ -621,7 +703,7 @@ export default class Root extends Route { }); } - if (proc.issuer !== authenticated.member.id && authenticated.member.id !== this.chairman) { + if ((proc.issuer !== authenticated.member.id || proc.processed) && authenticated.member.id !== this.chairman) { return res.status(403).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED, @@ -675,7 +757,7 @@ export default class Root extends Route { }); } - if (resolution.issuer !== authenticated.member.id && authenticated.member.id !== this.chairman) { + if (authenticated.member.id !== this.chairman) { return res.status(403).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.codes.UNAUTHORIZED, @@ -894,13 +976,14 @@ export default class Root extends Route { }, ); const resolutionMessage = await this.directorLogs.createMessage({ embed: resolutionEmbed }); + const resolutionID = genUUID(); await this.server.client.db.Resolution.create({ issuer: motion.issuer, subject: motion.subject, body: motion.body, at: Date.now(), - oID: motion.oID, + oID: resolutionID, results: { yea, nay, diff --git a/src/events/messageReactionAdd.ts b/src/events/messageReactionAdd.ts index 9b6fac6..a346e60 100644 --- a/src/events/messageReactionAdd.ts +++ b/src/events/messageReactionAdd.ts @@ -1,4 +1,4 @@ -import { Emoji, Guild, GuildTextableChannel, Member, Message } from 'eris'; +import { Emoji, GuildTextableChannel, Member, Message } from 'eris'; import { Client, Event } from '../class'; export default class MessageReactionAdd extends Event {