Finish questions

This commit is contained in:
Isaac 2022-07-19 22:34:45 +01:00
parent 5786c3598d
commit 98b35e3a81
No known key found for this signature in database
GPG Key ID: F4EAABEB0FFCC06A
5 changed files with 95 additions and 9 deletions

View File

@ -137,11 +137,11 @@ model Question {
id String @id @default(uuid())
category Category @relation(fields: [categoryId], references: [id], onDelete: Cascade)
categoryId Int
label String
label String @db.VarChar(45)
maxLength Int? @default(4000)
minLength Int? @default(0)
order Int
placeholder String?
placeholder String? @db.VarChar(100)
required Boolean @default(true)
style Int @default(2)
value String? @db.Text

View File

@ -6,11 +6,13 @@ log:
joined: '{user} {verb} {targetType}'
target:
category: 'a category'
question: 'a question'
settings: 'the settings'
title:
joined: '{targetType} {verb}'
target:
category: 'Category'
question: 'Question'
settings: 'Settings'
verb:
create: 'created'

View File

@ -4,15 +4,29 @@ const {
} = require('discord.js');
const { diff: getDiff } = require('object-diffy');
const exists = thing => (typeof thing === 'string' && thing.length > 0) && thing !== null && thing !== undefined;
const arrToObj = obj => {
for (const key in obj) {
if (obj[key] instanceof Array && obj[key][0]?.id) {
const temp = {};
obj[key].forEach(v => (temp[v.id] = v));
obj[key] = temp;
}
}
return obj;
};
function makeDiff({
original, updated,
}) {
const diff = getDiff(original, updated);
const diff = getDiff(arrToObj(original), arrToObj(updated));
const fields = [];
for (const key in diff) {
if (key === 'createdAt') continue; // object-diffy doesn't like dates
const from = diff[key].from === null ? '' : `- ${diff[key].from.toString().replace(/\n/g, '\\n')}\n`;
const to = diff[key].to === null ? '' : `+ ${diff[key].to.toString().replace(/\n/g, '\\n')}\n`;
const from = exists(diff[key].from) ? `- ${String(diff[key].from).replace(/\n/g, '\\n')}\n` : '';
const to = exists(diff[key].to) ? `+ ${String(diff[key].to).replace(/\n/g, '\\n')}\n` : '';
fields.push({
inline: true,
name: key,

View File

@ -1,4 +1,4 @@
const { logAdminEvent } = require('../../../../../../lib/logging');
const { logAdminEvent } = require('../../../../../../../lib/logging');
const { randomUUID } = require('crypto');
module.exports.delete = fastify => ({
@ -7,7 +7,7 @@ module.exports.delete = fastify => ({
const client = res.context.config.client;
const guildId = req.params.guild;
const categoryId = Number(req.params.category);
const original = req.params.category && await client.prisma.category.findUnique({ where: { id: categoryId } });
const original = categoryId && await client.prisma.category.findUnique({ where: { id: categoryId } });
if (!original || original.guildId !== guildId) return res.status(404).send(new Error('Not Found'));
const category = await client.prisma.category.delete({ where: { id: categoryId } });
@ -68,9 +68,49 @@ module.exports.patch = fastify => ({
const categoryId = Number(req.params.category);
const guild = client.guilds.cache.get(req.params.guild);
const data = req.body;
const original = req.params.category && await client.prisma.category.findUnique({ where: { id: categoryId } });
if (!original || original.guildId !== guildId) return res.status(404).send(new Error('Not Found'));
const select = {
channelName: true,
claiming: true,
// createdAt: true,
description: true,
discordCategory: true,
emoji: true,
enableFeedback: true,
guildId: true,
id: true,
image: true,
memberLimit: true,
name: true,
openingMessage: true,
pingRoles: true,
questions: {
select: {
// createdAt: true,
id: true,
label: true,
maxLength: true,
minLength: true,
order: true,
placeholder: true,
required: true,
style: true,
value: true,
},
},
ratelimit: true,
requireTopic: true,
requiredRoles: true,
staffRoles: true,
totalLimit: true,
};
const original = req.params.category && await client.prisma.category.findUnique({
select,
where: { id: categoryId },
});
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;
@ -89,6 +129,7 @@ module.exports.patch = fastify => ({
}),
},
},
select,
where: { id: categoryId },
});

View File

@ -0,0 +1,29 @@
const { logAdminEvent } = require('../../../../../../../../lib/logging');
module.exports.delete = fastify => ({
handler: async (req, res) => {
/** @type {import('client')} */
const client = res.context.config.client;
const guildId = req.params.guild;
const categoryId = Number(req.params.category);
const questionId = req.params.question;
const original = questionId && await client.prisma.question.findUnique({ where: { id: questionId } });
const category = categoryId && await client.prisma.category.findUnique({ where: { id: categoryId } });
if (original?.categoryId !== categoryId || category.guildId !== guildId) return res.status(404).send(new Error('Not Found'));
const question = await client.prisma.question.delete({ where: { id: questionId } });
logAdminEvent(client, {
action: 'delete',
guildId: req.params.guild,
target: {
id: question.id,
name: question.label,
type: 'question',
},
userId: req.user.payload.id,
});
return question;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});