From cdb8fa04c4b30809ae8bd80c595cded400e1a718 Mon Sep 17 00:00:00 2001 From: Isaac Date: Fri, 5 Aug 2022 14:01:19 +0100 Subject: [PATCH] Closes #321 --- .../admin/guilds/[guild]/categories/index.js | 47 ++++++++++++------- src/routes/api/admin/guilds/[guild]/index.js | 1 + src/routes/api/client.js | 3 +- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/routes/api/admin/guilds/[guild]/categories/index.js b/src/routes/api/admin/guilds/[guild]/categories/index.js index ec9a5a0..ac30fce 100644 --- a/src/routes/api/admin/guilds/[guild]/categories/index.js +++ b/src/routes/api/admin/guilds/[guild]/categories/index.js @@ -1,33 +1,44 @@ const { logAdminEvent } = require('../../../../../../lib/logging'); const emoji = require('node-emoji'); const { ChannelType: { GuildCategory } } = require('discord.js'); +const ms = require('ms'); module.exports.get = fastify => ({ handler: async (req, res) => { /** @type {import('client')} */ const client = res.context.config.client; - const { categories } = await client.prisma.guild.findUnique({ - select: { categories: true }, + let { categories } = await client.prisma.guild.findUnique({ + select: { + categories: { + select: { + createdAt: true, + description: true, + discordCategory: true, + emoji: true, + id: true, + image: true, + name: true, + requiredRoles: true, + staffRoles: true, + tickets: true, + }, + }, + }, where: { id: req.params.guild }, }); - // include: { - // questions: { - // select: { - // createdAt: true, - // id: true, - // label: true, - // maxLength: true, - // minLength: true, - // order: true, - // placeholder: true, - // required: true, - // style: true, - // value: true, - // }, - // }, - // }, + categories = categories.map(c => { + c = { + ...c, + stats: { + avgResolutionTime: ms(c.tickets.reduce((total, ticket) => total + (ticket.closedAt - ticket.createdAt), 0) ?? 1 / c.tickets.length), + avgResponseTime: ms(c.tickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / c.tickets.length), + }, + }; + delete c.tickets; + return c; + }); return categories; }, diff --git a/src/routes/api/admin/guilds/[guild]/index.js b/src/routes/api/admin/guilds/[guild]/index.js index 4c51f79..2c10f98 100644 --- a/src/routes/api/admin/guilds/[guild]/index.js +++ b/src/routes/api/admin/guilds/[guild]/index.js @@ -34,6 +34,7 @@ module.exports.get = fastify => ({ logo: guild.iconURL(), name: guild.name, stats: { + avgResolutionTime: ms(tickets.reduce((total, ticket) => total + (ticket.closedAt - ticket.createdAt), 0) ?? 1 / tickets.length), avgResponseTime: ms(tickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / tickets.length), categories: categories.map(c => ({ id: c.id, diff --git a/src/routes/api/client.js b/src/routes/api/client.js index b9821f9..b3321a6 100644 --- a/src/routes/api/client.js +++ b/src/routes/api/client.js @@ -23,6 +23,7 @@ module.exports.get = () => ({ stats: { activatedUsers: users.length, archivedMessages: users.reduce((total, user) => total + user.messageCount, 0), // don't count archivedMessage table rows, they get deleted + avgResolutionTime: ms(tickets.reduce((total, ticket) => total + (ticket.closedAt - ticket.createdAt), 0) ?? 1 / tickets.length), avgResponseTime: ms(tickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / tickets.length), categories: await client.prisma.category.count(), guilds: client.guilds.cache.size, @@ -32,7 +33,7 @@ module.exports.get = () => ({ }, username: client.user.username, }; - await client.keyv.set(cacheKey, cached, ms('5m')); + await client.keyv.set(cacheKey, cached, ms('15m')); } return cached;