Fix everything (db/encryption, logging etc)

This commit is contained in:
Isaac 2022-07-17 23:01:00 +01:00
parent 24d5e6d99d
commit 4a48ba635a
6 changed files with 61 additions and 43 deletions

View File

@ -67,7 +67,7 @@ model ArchivedUser {
} }
model Category { model Category {
channelName String @default("ticket-{num}") channelName String
claiming Boolean @default(false) claiming Boolean @default(false)
createdAt DateTime @default(now()) createdAt DateTime @default(now())
description String @db.VarChar(512) description String @db.VarChar(512)

View File

@ -1,9 +1,7 @@
command: command:
log: log:
admin: admin:
changes: changes: 'Changes'
title: 'Changes'
description: 'These were the changes made:'
description: description:
joined: '{user} {verb} {targetType}' joined: '{user} {verb} {targetType}'
target: target:

View File

@ -60,40 +60,36 @@ async function logAdminEvent(client, {
}; };
const channel = client.channels.cache.get(settings.logChannel); const channel = client.channels.cache.get(settings.logChannel);
if (!channel) return; 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({ if (diff && diff.original) {
embeds: [ embeds.push(
new MessageEmbed() new MessageEmbed()
.setColor('ORANGE') .setColor('ORANGE')
.setAuthor({ .setTitle(getMessage('log.admin.changes'))
iconURL: user.avatarURL(), .setFields(makeDiff(diff)),
name: user.username, );
}) }
.setTitle(getMessage('log.admin.title.joined', {
...i18nOptions, return await channel.send({ embeds });
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)),
],
],
});
} }
module.exports = { module.exports = {

View File

@ -17,7 +17,7 @@ const fields = [
'regex', 'regex',
]; ];
const shouldEncrypt = ['create', 'createMany', 'update', 'updateMany', 'upsert']; 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) => { return async (params, next) => {
if (params.args.data && shouldEncrypt.includes(params.action)) params.args = encrypt(params.args); if (params.args.data && shouldEncrypt.includes(params.action)) params.args = encrypt(params.args);
let result = await next(params); 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; return result;
}; };
}; };

View File

@ -1,10 +1,33 @@
const { logAdminEvent } = require('../../../../../../lib/logging'); 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 => ({ module.exports.get = fastify => ({
handler: async (req, res) => { handler: async (req, res) => {
/** @type {import('client')} */ /** @type {import('client')} */
const client = res.context.config.client; const client = res.context.config.client;
const categoryId = Number(req.params.category);
const category = await client.prisma.category.findUnique({ const category = await client.prisma.category.findUnique({
include: { include: {
questions: { questions: {
@ -22,7 +45,7 @@ module.exports.get = fastify => ({
}, },
}, },
}, },
where: { id: Number(req.params.category) }, where: { id: categoryId },
}); });
return category; return category;
@ -34,12 +57,12 @@ module.exports.patch = fastify => ({
handler: async (req, res) => { handler: async (req, res) => {
/** @type {import('client')} */ /** @type {import('client')} */
const client = res.context.config.client; const client = res.context.config.client;
const categoryId = Number(req.params.category);
const user = await client.users.fetch(req.user.payload.id); const user = await client.users.fetch(req.user.payload.id);
const guild = client.guilds.cache.get(req.params.guild); const guild = client.guilds.cache.get(req.params.guild);
const data = req.body; const data = req.body;
const allow = ['VIEW_CHANNEL', 'READ_MESSAGE_HISTORY', 'SEND_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES']; 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 (!original) return res.status(404);
if (!data.discordCategory) { if (!data.discordCategory) {
@ -69,7 +92,6 @@ module.exports.patch = fastify => ({
const category = await client.prisma.category.update({ const category = await client.prisma.category.update({
data: { data: {
guild: { connect: { id: guild.id } },
...data, ...data,
questions: { questions: {
upsert: data.questions?.map(q => ({ upsert: data.questions?.map(q => ({
@ -79,6 +101,7 @@ module.exports.patch = fastify => ({
})), })),
}, },
}, },
where: { id: categoryId },
}); });
logAdminEvent(client, { logAdminEvent(client, {

View File

@ -69,7 +69,7 @@ module.exports.post = fastify => ({
data.discordCategory = channel.id; 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({ const category = await client.prisma.category.create({
data: { data: {