(fastify: FastifyInstance)
| 5 | import { prisma } from "../prisma"; |
| 6 | |
| 7 | export function roleRoutes(fastify: FastifyInstance) { |
| 8 | // Create a new role |
| 9 | fastify.post( |
| 10 | "/api/v1/role/create", |
| 11 | { |
| 12 | preHandler: requirePermission(["role::create"]), |
| 13 | }, |
| 14 | async (request: FastifyRequest, reply: FastifyReply) => { |
| 15 | const user = await checkSession(request); |
| 16 | const { name, description, permissions, isDefault }: any = request.body; |
| 17 | |
| 18 | const existingRole = await prisma.role.findUnique({ |
| 19 | where: { name }, |
| 20 | }); |
| 21 | |
| 22 | if (existingRole) { |
| 23 | return reply.status(400).send({ |
| 24 | message: "Role already exists", |
| 25 | success: false, |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | await prisma.role.create({ |
| 30 | data: { |
| 31 | name, |
| 32 | description, |
| 33 | permissions, |
| 34 | isDefault: isDefault || false, |
| 35 | }, |
| 36 | }); |
| 37 | |
| 38 | const client = track(); |
| 39 | client.capture({ |
| 40 | event: "role_created", |
| 41 | distinctId: "uuid", |
| 42 | }); |
| 43 | client.shutdownAsync(); |
| 44 | |
| 45 | reply.status(200).send({ message: "Role created!", success: true }); |
| 46 | } |
| 47 | ); |
| 48 | |
| 49 | // Get all roles |
| 50 | fastify.get( |
| 51 | "/api/v1/roles/all", |
| 52 | { |
| 53 | preHandler: requirePermission(["role::read"]), |
| 54 | }, |
| 55 | async (request: FastifyRequest, reply: FastifyReply) => { |
| 56 | const roles = await prisma.role.findMany({ |
| 57 | include: { |
| 58 | users: false, |
| 59 | }, |
| 60 | }); |
| 61 | |
| 62 | const active = await prisma.config.findFirst({ |
| 63 | select: { |
| 64 | roles_active: true, |
no test coverage detected