Skip to content

Commit

Permalink
chore: little bit of tidying up
Browse files Browse the repository at this point in the history
  • Loading branch information
potts99 committed Nov 14, 2024
1 parent 924d34f commit fbf8369
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 86 deletions.
23 changes: 23 additions & 0 deletions apps/api/src/controllers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,27 @@ export function configRoutes(fastify: FastifyInstance) {
});
}
);

// Toggle all roles
fastify.patch(
"/api/v1/config/toggle-roles",

async (request: FastifyRequest, reply: FastifyReply) => {
const { isActive }: any = request.body;

const config = await prisma.config.findFirst();

await prisma.config.update({
where: { id: config!.id },
data: {
roles_active: isActive,
},
});

reply.send({
success: true,
message: "Roles updated!",
});
}
);
}
79 changes: 44 additions & 35 deletions apps/api/src/controllers/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function roleRoutes(fastify: FastifyInstance) {
fastify.post(
"/api/v1/role/create",
{
preHandler: requirePermission(['role::create']),
preHandler: requirePermission(["role::create"]),
},
async (request: FastifyRequest, reply: FastifyReply) => {
const user = await checkSession(request);
Expand All @@ -20,9 +20,9 @@ export function roleRoutes(fastify: FastifyInstance) {
});

if (existingRole) {
return reply.status(400).send({
message: "Role already exists",
success: false
return reply.status(400).send({
message: "Role already exists",
success: false,
});
}

Expand Down Expand Up @@ -50,28 +50,34 @@ export function roleRoutes(fastify: FastifyInstance) {
fastify.get(
"/api/v1/roles/all",
{
preHandler: requirePermission(['role::read']),
preHandler: requirePermission(["role::read"]),
},
async (request: FastifyRequest, reply: FastifyReply) => {
const roles = await prisma.role.findMany({
include: {
users: true,
users: false,
},
});

const active = await prisma.config.findFirst({
select: {
roles_active: true,
},
});

reply.status(200).send({ roles, success: true });
reply.status(200).send({ roles, success: true, roles_active: active });
}
);

// Get role by ID
fastify.get(
"/api/v1/role/:id",
{
preHandler: requirePermission(['role::read']),
preHandler: requirePermission(["role::read"]),
},
async (request: FastifyRequest, reply: FastifyReply) => {
const { id }: any = request.params;

const role = await prisma.role.findUnique({
where: { id },
include: {
Expand All @@ -80,9 +86,9 @@ export function roleRoutes(fastify: FastifyInstance) {
});

if (!role) {
return reply.status(404).send({
message: "Role not found",
success: false
return reply.status(404).send({
message: "Role not found",
success: false,
});
}

Expand All @@ -94,11 +100,12 @@ export function roleRoutes(fastify: FastifyInstance) {
fastify.put(
"/api/v1/role/:id/update",
{
preHandler: requirePermission(['role::update']),
preHandler: requirePermission(["role::update"]),
},
async (request: FastifyRequest, reply: FastifyReply) => {
const { id }: any = request.params;
const { name, description, permissions, isDefault, users }: any = request.body;
const { name, description, permissions, isDefault, users }: any =
request.body;

try {
const updatedRole = await prisma.role.update({
Expand All @@ -110,17 +117,19 @@ export function roleRoutes(fastify: FastifyInstance) {
isDefault,
updatedAt: new Date(),
users: {
set: Array.isArray(users) ? users.map(userId => ({ id: userId })) : [{ id: users }], // Ensure users is an array of objects with unique IDs when updating
set: Array.isArray(users)
? users.map((userId) => ({ id: userId }))
: [{ id: users }], // Ensure users is an array of objects with unique IDs when updating
},
},
});

reply.status(200).send({ role: updatedRole, success: true });
} catch (error: any) {
if (error.code === 'P2025') {
return reply.status(404).send({
message: "Role not found",
success: false
if (error.code === "P2025") {
return reply.status(404).send({
message: "Role not found",
success: false,
});
}
throw error;
Expand All @@ -132,22 +141,22 @@ export function roleRoutes(fastify: FastifyInstance) {
fastify.delete(
"/api/v1/role/:id/delete",
{
preHandler: requirePermission(['role::delete']),
preHandler: requirePermission(["role::delete"]),
},
async (request: FastifyRequest, reply: FastifyReply) => {
const { id }: any = request.params;

try {
await prisma.role.delete({
where: { id },
});

reply.status(200).send({ success: true });
} catch (error: any) {
if (error.code === 'P2025') {
return reply.status(404).send({
message: "Role not found",
success: false
if (error.code === "P2025") {
return reply.status(404).send({
message: "Role not found",
success: false,
});
}
throw error;
Expand All @@ -159,7 +168,7 @@ export function roleRoutes(fastify: FastifyInstance) {
fastify.post(
"/api/v1/role/assign",
{
preHandler: requirePermission(['role::update']),
preHandler: requirePermission(["role::update"]),
},
async (request: FastifyRequest, reply: FastifyReply) => {
const { userId, roleId }: any = request.body;
Expand All @@ -179,10 +188,10 @@ export function roleRoutes(fastify: FastifyInstance) {

reply.status(200).send({ user: updatedUser, success: true });
} catch (error: any) {
if (error.code === 'P2025') {
return reply.status(404).send({
message: "User or Role not found",
success: false
if (error.code === "P2025") {
return reply.status(404).send({
message: "User or Role not found",
success: false,
});
}
throw error;
Expand All @@ -194,7 +203,7 @@ export function roleRoutes(fastify: FastifyInstance) {
fastify.post(
"/api/v1/role/remove",
{
// preHandler: requirePermission(['role::remove']),
// preHandler: requirePermission(['role::remove']),
},
async (request: FastifyRequest, reply: FastifyReply) => {
const { userId, roleId }: any = request.body;
Expand All @@ -214,10 +223,10 @@ export function roleRoutes(fastify: FastifyInstance) {

reply.status(200).send({ user: updatedUser, success: true });
} catch (error: any) {
if (error.code === 'P2025') {
return reply.status(404).send({
message: "User or Role not found",
success: false
if (error.code === "P2025") {
return reply.status(404).send({
message: "User or Role not found",
success: false,
});
}
throw error;
Expand Down
1 change: 0 additions & 1 deletion apps/client/pages/admin/roles/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ export default function UpdateRole() {

return (
<div className="p-4">
{/* ... same stepper UI ... */}

{step === 1 ? (
<Card>
Expand Down
Loading

0 comments on commit fbf8369

Please sign in to comment.