Skip to content

Commit 94e9939

Browse files
committed
feat(api): list and get ticket routes
1 parent 6facf1b commit 94e9939

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const { pools } = require('../../../../../../lib/threads');
2+
const { export: pool } = pools;
3+
4+
module.exports.get = fastify => ({
5+
handler: async (req, res) => {
6+
/** @type {import('client')} */
7+
const client = req.routeOptions.config.client;
8+
const guildId = req.params.guild;
9+
const ticketId = req.params.ticket;
10+
const ticket = await client.prisma.ticket.findUnique({
11+
include: {
12+
archivedChannels: true,
13+
archivedMessages: true,
14+
archivedRoles: true,
15+
archivedUsers: true,
16+
feedback: true,
17+
questionAnswers: true,
18+
},
19+
where: {
20+
guildId, // ! prevent unauthorised access
21+
id: ticketId,
22+
},
23+
});
24+
25+
if (!ticket) return res.status(404).send(new Error('Not Found'));
26+
27+
res.header('Content-Type', 'application/json'); // exportTicket returns stringified JSON
28+
return await pool.queue(w => w.exportTicket(ticket));
29+
},
30+
onRequest: [fastify.authenticate, fastify.isAdmin],
31+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { pools } = require('../../../../../../lib/threads');
2+
const { crypto } = pools;
3+
4+
module.exports.get = fastify => ({
5+
handler: async req => {
6+
/** @type {import('client')} */
7+
const client = req.routeOptions.config.client;
8+
const { query } = req;
9+
// TODO: advanced filters
10+
const tickets = await client.prisma.ticket.findMany({
11+
orderBy: { createdAt: 'asc' },
12+
where: {
13+
createdAt: { gte: query.since && new Date((Number(query.since) * 1000) || query.since) },
14+
guildId: req.params.guild,
15+
},
16+
});
17+
return Promise.all(
18+
tickets.map(async ticket => {
19+
ticket.closedReason &&= await crypto.queue(w => w.decrypt(ticket.closedReason));
20+
ticket.topic &&= await crypto.queue(w => w.decrypt(ticket.topic));
21+
return ticket;
22+
}),
23+
);
24+
},
25+
onRequest: [fastify.authenticate, fastify.isAdmin],
26+
});
27+

0 commit comments

Comments
 (0)