From 31635cc7cb3cc84157f46b494ac33effd2d51288 Mon Sep 17 00:00:00 2001 From: Isaac Date: Mon, 25 Jul 2022 00:25:26 +0100 Subject: [PATCH] Fix tags --- .../api/admin/guilds/[guild]/tags/[tag].js | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/routes/api/admin/guilds/[guild]/tags/[tag].js b/src/routes/api/admin/guilds/[guild]/tags/[tag].js index 6edea35..4a954dd 100644 --- a/src/routes/api/admin/guilds/[guild]/tags/[tag].js +++ b/src/routes/api/admin/guilds/[guild]/tags/[tag].js @@ -5,7 +5,7 @@ module.exports.delete = fastify => ({ /** @type {import('client')} */ const client = res.context.config.client; const guildId = req.params.guild; - const tagId = req.params.tag; + const tagId = Number(req.params.tag); const original = tagId && await client.prisma.tag.findUnique({ where: { id: tagId } }); if (original.guildId !== guildId) return res.status(404).send(new Error('Not Found')); const tag = await client.prisma.tag.delete({ where: { id: tagId } }); @@ -39,4 +39,45 @@ module.exports.get = fastify => ({ return tag; }, onRequest: [fastify.authenticate, fastify.isAdmin], +}); + +module.exports.patch = fastify => ({ + handler: async (req, res) => { + /** @type {import('client')} */ + const client = res.context.config.client; + const guildId = req.params.guild; + const tagId = Number(req.params.tag); + const guild = client.guilds.cache.get(req.params.guild); + const data = req.body; + + const original = req.params.tag && await client.prisma.tag.findUnique({ where: { id: tagId } }); + + if (!original || original.guildId !== guildId) return res.status(404).send(new Error('Not Found')); + + if (data.hasOwnProperty('id')) delete data.id; + if (data.hasOwnProperty('createdAt')) delete data.createdAt; + + const tag = await client.prisma.tag.update({ + data, + where: { id: tagId }, + }); + + logAdminEvent(client, { + action: 'update', + diff: { + original, + updated: tag, + }, + guildId: guild.id, + target: { + id: tag.id, + name: tag.name, + type: 'tag', + }, + userId: req.user.payload.id, + }); + + return tag; + }, + onRequest: [fastify.authenticate, fastify.isAdmin], }); \ No newline at end of file