From 4a48ba635a8722c74f1ea7d5232f69933e986b5f Mon Sep 17 00:00:00 2001 From: Isaac Date: Sun, 17 Jul 2022 23:01:00 +0100 Subject: [PATCH] Fix everything (db/encryption, logging etc) --- prisma/schema.prisma | 2 +- src/i18n/en-GB.yml | 4 +- src/lib/logging.js | 58 +++++++++---------- src/lib/prisma.js | 5 +- .../guilds/[guild]/categories/[category].js | 33 +++++++++-- .../admin/guilds/[guild]/categories/index.js | 2 +- 6 files changed, 61 insertions(+), 43 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 5a06197..11909de 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -67,7 +67,7 @@ model ArchivedUser { } model Category { - channelName String @default("ticket-{num}") + channelName String claiming Boolean @default(false) createdAt DateTime @default(now()) description String @db.VarChar(512) diff --git a/src/i18n/en-GB.yml b/src/i18n/en-GB.yml index 9998e1e..2657f6a 100644 --- a/src/i18n/en-GB.yml +++ b/src/i18n/en-GB.yml @@ -1,9 +1,7 @@ command: log: admin: - changes: - title: 'Changes' - description: 'These were the changes made:' + changes: 'Changes' description: joined: '{user} {verb} {targetType}' target: diff --git a/src/lib/logging.js b/src/lib/logging.js index 7bab016..7bae537 100644 --- a/src/lib/logging.js +++ b/src/lib/logging.js @@ -60,40 +60,36 @@ async function logAdminEvent(client, { }; const channel = client.channels.cache.get(settings.logChannel); if (!channel) return; + const embeds = [ + new MessageEmbed() + .setColor('ORANGE') + .setAuthor({ + iconURL: user.avatarURL(), + name: user.username, + }) + .setTitle(getMessage('log.admin.title.joined', { + ...i18nOptions, + targetType: getMessage(`log.admin.title.target.${target.type}`), + verb: getMessage(`log.admin.verb.${action}`), + })) + .setDescription(getMessage('log.admin.description.joined', { + ...i18nOptions, + targetType: getMessage(`log.admin.description.target.${target.type}`), + verb: getMessage(`log.admin.verb.${action}`), + })) + .addField(getMessage(`log.admin.title.target.${target.type}`), target.name ?? target.id), + ]; - return await channel.send({ - embeds: [ + if (diff && diff.original) { + embeds.push( new MessageEmbed() .setColor('ORANGE') - .setAuthor({ - iconURL: user.avatarURL(), - name: user.username, - }) - .setTitle(getMessage('log.admin.title.joined', { - ...i18nOptions, - targetType: getMessage(`log.admin.title.target.${target.type}`), - verb: getMessage(`log.admin.verb.${action}`), - })) - .setDescription(getMessage('log.admin.description.joined', { - ...i18nOptions, - targetType: getMessage(`log.admin.description.target.${target.type}`), - verb: getMessage(`log.admin.verb.${action}`), - })) - .addField(getMessage(`log.admin.title.target.${target.type}`), target.name ?? target.id), - // .setFooter({ - // iconURL: client.guilds.cache.get(guildId).iconURL(), - // text: settings.footer, - // }), - ...[ - diff?.original && - new MessageEmbed() - .setColor('ORANGE') - .setTitle(getMessage('log.admin.changes.title')) - .setDescription(getMessage('log.admin.changes.description')) - .setFields(makeDiff(diff)), - ], - ], - }); + .setTitle(getMessage('log.admin.changes')) + .setFields(makeDiff(diff)), + ); + } + + return await channel.send({ embeds }); } module.exports = { diff --git a/src/lib/prisma.js b/src/lib/prisma.js index 204b327..d0818c9 100644 --- a/src/lib/prisma.js +++ b/src/lib/prisma.js @@ -17,7 +17,7 @@ const fields = [ 'regex', ]; const shouldEncrypt = ['create', 'createMany', 'update', 'updateMany', 'upsert']; -const shouldDecrypt = ['findUnique', 'findFirst', 'findMany']; +// const shouldDecrypt = ['findUnique', 'findFirst', 'findMany']; @@ -62,7 +62,8 @@ module.exports = log => { return async (params, next) => { if (params.args.data && shouldEncrypt.includes(params.action)) params.args = encrypt(params.args); let result = await next(params); - if (result && shouldDecrypt.includes(params.action)) result = decrypt(result); + // if (result && shouldDecrypt.includes(params.action)) result = decrypt(result); + if (result) result = decrypt(result); return result; }; }; \ No newline at end of file diff --git a/src/routes/api/admin/guilds/[guild]/categories/[category].js b/src/routes/api/admin/guilds/[guild]/categories/[category].js index 36854e7..eb7228a 100644 --- a/src/routes/api/admin/guilds/[guild]/categories/[category].js +++ b/src/routes/api/admin/guilds/[guild]/categories/[category].js @@ -1,10 +1,33 @@ const { logAdminEvent } = require('../../../../../../lib/logging'); +module.exports.delete = fastify => ({ + handler: async (req, res) => { + /** @type {import('client')} */ + const client = res.context.config.client; + const categoryId = Number(req.params.category); + const category = await client.prisma.category.delete({ where: { id: categoryId } }); + + logAdminEvent(client, { + action: 'delete', + guildId: req.params.guild, + target: { + id: category.id, + name: category.name, + type: 'category', + }, + userId: req.user.payload.id, + }); + + return category; + }, + onRequest: [fastify.authenticate, fastify.isAdmin], +}); + module.exports.get = fastify => ({ handler: async (req, res) => { /** @type {import('client')} */ const client = res.context.config.client; - + const categoryId = Number(req.params.category); const category = await client.prisma.category.findUnique({ include: { questions: { @@ -22,7 +45,7 @@ module.exports.get = fastify => ({ }, }, }, - where: { id: Number(req.params.category) }, + where: { id: categoryId }, }); return category; @@ -34,12 +57,12 @@ module.exports.patch = fastify => ({ handler: async (req, res) => { /** @type {import('client')} */ const client = res.context.config.client; - + const categoryId = Number(req.params.category); const user = await client.users.fetch(req.user.payload.id); const guild = client.guilds.cache.get(req.params.guild); const data = req.body; const allow = ['VIEW_CHANNEL', 'READ_MESSAGE_HISTORY', 'SEND_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES']; - const original = req.params.category && await client.prisma.category.findUnique({ where: { id: req.params.category } }); + const original = req.params.category && await client.prisma.category.findUnique({ where: { id: categoryId } }); if (!original) return res.status(404); if (!data.discordCategory) { @@ -69,7 +92,6 @@ module.exports.patch = fastify => ({ const category = await client.prisma.category.update({ data: { - guild: { connect: { id: guild.id } }, ...data, questions: { upsert: data.questions?.map(q => ({ @@ -79,6 +101,7 @@ module.exports.patch = fastify => ({ })), }, }, + where: { id: categoryId }, }); logAdminEvent(client, { diff --git a/src/routes/api/admin/guilds/[guild]/categories/index.js b/src/routes/api/admin/guilds/[guild]/categories/index.js index e20f34b..2d1d9bd 100644 --- a/src/routes/api/admin/guilds/[guild]/categories/index.js +++ b/src/routes/api/admin/guilds/[guild]/categories/index.js @@ -69,7 +69,7 @@ module.exports.post = fastify => ({ data.discordCategory = channel.id; } - if (data.channelName === null) data.channelName = undefined; + if (!data.channelName) data.channelName = 'ticket-{num}'; const category = await client.prisma.category.create({ data: {