(fastify: FastifyInstance)
| 5 | import { prisma } from "../prisma"; |
| 6 | |
| 7 | export function webhookRoutes(fastify: FastifyInstance) { |
| 8 | // Create a new webhook |
| 9 | fastify.post( |
| 10 | "/api/v1/webhook/create", |
| 11 | { |
| 12 | preHandler: requirePermission(["webhook::create"]), |
| 13 | }, |
| 14 | async (request: FastifyRequest, reply: FastifyReply) => { |
| 15 | const user = await checkSession(request); |
| 16 | const { name, url, type, active, secret }: any = request.body; |
| 17 | await prisma.webhooks.create({ |
| 18 | data: { |
| 19 | name, |
| 20 | url, |
| 21 | type, |
| 22 | active, |
| 23 | secret, |
| 24 | createdBy: user!.id, |
| 25 | }, |
| 26 | }); |
| 27 | |
| 28 | const client = track(); |
| 29 | |
| 30 | client.capture({ |
| 31 | event: "webhook_created", |
| 32 | distinctId: "uuid", |
| 33 | }); |
| 34 | |
| 35 | client.shutdownAsync(); |
| 36 | |
| 37 | reply.status(200).send({ message: "Hook created!", success: true }); |
| 38 | } |
| 39 | ); |
| 40 | |
| 41 | // Get all webhooks |
| 42 | fastify.get( |
| 43 | "/api/v1/webhooks/all", |
| 44 | { |
| 45 | preHandler: requirePermission(["webhook::read"]), |
| 46 | }, |
| 47 | async (request: FastifyRequest, reply: FastifyReply) => { |
| 48 | const webhooks = await prisma.webhooks.findMany({}); |
| 49 | |
| 50 | reply.status(200).send({ webhooks: webhooks, success: true }); |
| 51 | } |
| 52 | ); |
| 53 | |
| 54 | // Delete a webhook |
| 55 | fastify.delete( |
| 56 | "/api/v1/admin/webhook/:id/delete", |
| 57 | { |
| 58 | preHandler: requirePermission(["webhook::delete"]), |
| 59 | }, |
| 60 | async (request: FastifyRequest, reply: FastifyReply) => { |
| 61 | const { id }: any = request.params; |
| 62 | await prisma.webhooks.delete({ |
| 63 | where: { |
| 64 | id: id, |
no test coverage detected