Add logout & tags endpoints

This commit is contained in:
Isaac
2022-07-23 01:13:25 +01:00
parent cc97a58165
commit 08e757febf
5 changed files with 100 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
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 tagId = 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 } });
logAdminEvent(client, {
action: 'delete',
guildId: req.params.guild,
target: {
id: tag.id,
name: tag.name,
type: 'tag',
},
userId: req.user.payload.id,
});
return tag;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});
module.exports.get = 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 tag = await client.prisma.tag.findUnique({ where: { id: tagId } });
if (!tag || tag.guildId !== guildId) return res.status(404).send(new Error('Not Found'));
return tag;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});

View File

@@ -0,0 +1,46 @@
const { logAdminEvent } = require('../../../../../../lib/logging');
module.exports.get = fastify => ({
handler: async (req, res) => {
/** @type {import('client')} */
const client = res.context.config.client;
const { tags } = await client.prisma.guild.findUnique({
select: { tags: true },
where: { id: req.params.guild },
});
return tags;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});
module.exports.post = fastify => ({
handler: async (req, res) => {
/** @type {import('client')} */
const client = res.context.config.client;
const guild = client.guilds.cache.get(req.params.guild);
const data = req.body;
const tag = await client.prisma.tag.create({
data: {
guild: { connect: { id: guild.id } },
...data,
},
});
logAdminEvent(client, {
action: 'create',
guildId: guild.id,
target: {
id: tag.id,
name: tag.name,
type: 'tag',
},
userId: req.user.payload.id,
});
return tag;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});

View File

@@ -0,0 +1,7 @@
module.exports.get = () => ({
handler: async function (req, res) { // must NOT use arrow function syntax
res
.clearCookie('token', '/')
.send('Logged out.');
},
});