From 9fc1130c4c6e0ee6c31efaf4e4b2bc3b9a73170b Mon Sep 17 00:00:00 2001 From: Isaac Date: Thu, 2 Feb 2023 20:45:18 +0000 Subject: [PATCH] fix: update cache when tags are updated --- package.json | 2 +- .../api/admin/guilds/[guild]/tags/[tag].js | 23 +++++++++++++++++++ .../api/admin/guilds/[guild]/tags/index.js | 19 +++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3b411be..455c6b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-tickets", - "version": "4.0.0-beta.2", + "version": "4.0.0-beta.3", "private": "true", "description": "An open-source Discord bot for ticket management", "main": "src/", diff --git a/src/routes/api/admin/guilds/[guild]/tags/[tag].js b/src/routes/api/admin/guilds/[guild]/tags/[tag].js index 4a954dd..0f06192 100644 --- a/src/routes/api/admin/guilds/[guild]/tags/[tag].js +++ b/src/routes/api/admin/guilds/[guild]/tags/[tag].js @@ -1,3 +1,4 @@ +const ms = require('ms'); const { logAdminEvent } = require('../../../../../../lib/logging'); module.exports.delete = fastify => ({ @@ -10,6 +11,17 @@ module.exports.delete = fastify => ({ if (original.guildId !== guildId) return res.status(404).send(new Error('Not Found')); const tag = await client.prisma.tag.delete({ where: { id: tagId } }); + const cacheKey = `cache/guild-tags:${guildId}`; + client.keyv.set(cacheKey, await client.prisma.tag.findMany({ + select: { + content: true, + id: true, + name: true, + regex: true, + }, + where: { guildId: guildId }, + }), ms('1h')); + logAdminEvent(client, { action: 'delete', guildId: req.params.guild, @@ -62,6 +74,17 @@ module.exports.patch = fastify => ({ where: { id: tagId }, }); + const cacheKey = `cache/guild-tags:${guildId}`; + client.keyv.set(cacheKey, await client.prisma.tag.findMany({ + select: { + content: true, + id: true, + name: true, + regex: true, + }, + where: { guildId: guildId }, + }), ms('1h')); + logAdminEvent(client, { action: 'update', diff: { diff --git a/src/routes/api/admin/guilds/[guild]/tags/index.js b/src/routes/api/admin/guilds/[guild]/tags/index.js index 73f31d4..35af225 100644 --- a/src/routes/api/admin/guilds/[guild]/tags/index.js +++ b/src/routes/api/admin/guilds/[guild]/tags/index.js @@ -1,3 +1,4 @@ +const ms = require('ms'); const { logAdminEvent } = require('../../../../../../lib/logging'); module.exports.get = fastify => ({ @@ -29,6 +30,24 @@ module.exports.post = fastify => ({ }, }); + const cacheKey = `cache/guild-tags:${guild.id}`; + let tags = await client.keyv.get(cacheKey); + if (!tags) { + tags = await client.prisma.tag.findMany({ + select: { + content: true, + id: true, + name: true, + regex: true, + }, + where: { guildId: guild.id }, + }); + client.keyv.set(cacheKey, tags, ms('1h')); + } else { + tags.push(tag); + client.keyv.set(cacheKey, tags, ms('1h')); + } + logAdminEvent(client, { action: 'create', guildId: guild.id,