style: run prettier on entire codebase
This commit is contained in:
@@ -1,136 +1,165 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import prisma from '../../config/database';
|
||||
import { requirePermission } from '../../middleware/auth';
|
||||
import { logAudit } from '../../services/audit';
|
||||
import { success, error, parseId } from '../../utils/response';
|
||||
import { parseBody } from '../../schemas/common';
|
||||
import { CreateRoleSchema, UpdateRoleSchema } from '../../schemas/roles.schema';
|
||||
import { FastifyInstance } from "fastify";
|
||||
import prisma from "../../config/database";
|
||||
import { requirePermission } from "../../middleware/auth";
|
||||
import { logAudit } from "../../services/audit";
|
||||
import { success, error, parseId } from "../../utils/response";
|
||||
import { parseBody } from "../../schemas/common";
|
||||
import { CreateRoleSchema, UpdateRoleSchema } from "../../schemas/roles.schema";
|
||||
|
||||
export default async function rolesRoutes(fastify: FastifyInstance): Promise<void> {
|
||||
export default async function rolesRoutes(
|
||||
fastify: FastifyInstance,
|
||||
): Promise<void> {
|
||||
// GET /api/admin/roles
|
||||
fastify.get('/', { preHandler: requirePermission('settings.roles') }, async (request, reply) => {
|
||||
const roles = await prisma.roles.findMany({
|
||||
include: {
|
||||
role_permissions: {
|
||||
include: { permissions: true },
|
||||
fastify.get(
|
||||
"/",
|
||||
{ preHandler: requirePermission("settings.roles") },
|
||||
async (request, reply) => {
|
||||
const roles = await prisma.roles.findMany({
|
||||
include: {
|
||||
role_permissions: {
|
||||
include: { permissions: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
});
|
||||
orderBy: { id: "asc" },
|
||||
});
|
||||
|
||||
const data = roles.map(r => ({
|
||||
...r,
|
||||
permissions: r.role_permissions.map(rp => rp.permissions),
|
||||
}));
|
||||
const data = roles.map((r) => ({
|
||||
...r,
|
||||
permissions: r.role_permissions.map((rp) => rp.permissions),
|
||||
}));
|
||||
|
||||
return success(reply, data);
|
||||
});
|
||||
return success(reply, data);
|
||||
},
|
||||
);
|
||||
|
||||
// GET /api/admin/roles/permissions
|
||||
fastify.get('/permissions', { preHandler: requirePermission('settings.roles') }, async (_request, reply) => {
|
||||
const permissions = await prisma.permissions.findMany({ orderBy: { module: 'asc' } });
|
||||
return success(reply, permissions);
|
||||
});
|
||||
fastify.get(
|
||||
"/permissions",
|
||||
{ preHandler: requirePermission("settings.roles") },
|
||||
async (_request, reply) => {
|
||||
const permissions = await prisma.permissions.findMany({
|
||||
orderBy: { module: "asc" },
|
||||
});
|
||||
return success(reply, permissions);
|
||||
},
|
||||
);
|
||||
|
||||
// POST /api/admin/roles
|
||||
fastify.post('/', { preHandler: requirePermission('settings.roles') }, async (request, reply) => {
|
||||
const parsed = parseBody(CreateRoleSchema, request.body);
|
||||
if ('error' in parsed) return error(reply, parsed.error, 400);
|
||||
const body = parsed.data;
|
||||
fastify.post(
|
||||
"/",
|
||||
{ preHandler: requirePermission("settings.roles") },
|
||||
async (request, reply) => {
|
||||
const parsed = parseBody(CreateRoleSchema, request.body);
|
||||
if ("error" in parsed) return error(reply, parsed.error, 400);
|
||||
const body = parsed.data;
|
||||
|
||||
const role = await prisma.roles.create({
|
||||
data: {
|
||||
name: String(body.name),
|
||||
display_name: String(body.display_name),
|
||||
description: body.description ? String(body.description) : null,
|
||||
},
|
||||
});
|
||||
|
||||
if (Array.isArray(body.permission_ids)) {
|
||||
await prisma.role_permissions.createMany({
|
||||
data: (body.permission_ids as number[]).map((pid) => ({
|
||||
role_id: role.id,
|
||||
permission_id: pid,
|
||||
})),
|
||||
const role = await prisma.roles.create({
|
||||
data: {
|
||||
name: String(body.name),
|
||||
display_name: String(body.display_name),
|
||||
description: body.description ? String(body.description) : null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
await logAudit({
|
||||
request,
|
||||
authData: request.authData,
|
||||
action: 'create',
|
||||
entityType: 'role',
|
||||
entityId: role.id,
|
||||
description: `Vytvořena role ${role.name}`,
|
||||
});
|
||||
if (Array.isArray(body.permission_ids)) {
|
||||
await prisma.role_permissions.createMany({
|
||||
data: (body.permission_ids as number[]).map((pid) => ({
|
||||
role_id: role.id,
|
||||
permission_id: pid,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
return success(reply, { id: role.id }, 201, 'Role byla vytvořena');
|
||||
});
|
||||
await logAudit({
|
||||
request,
|
||||
authData: request.authData,
|
||||
action: "create",
|
||||
entityType: "role",
|
||||
entityId: role.id,
|
||||
description: `Vytvořena role ${role.name}`,
|
||||
});
|
||||
|
||||
return success(reply, { id: role.id }, 201, "Role byla vytvořena");
|
||||
},
|
||||
);
|
||||
|
||||
// PUT /api/admin/roles/:id
|
||||
fastify.put<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('settings.roles') }, async (request, reply) => {
|
||||
const id = parseId(request.params.id, reply);
|
||||
if (id === null) return;
|
||||
const parsed = parseBody(UpdateRoleSchema, request.body);
|
||||
if ('error' in parsed) return error(reply, parsed.error, 400);
|
||||
const body = parsed.data;
|
||||
fastify.put<{ Params: { id: string } }>(
|
||||
"/:id",
|
||||
{ preHandler: requirePermission("settings.roles") },
|
||||
async (request, reply) => {
|
||||
const id = parseId(request.params.id, reply);
|
||||
if (id === null) return;
|
||||
const parsed = parseBody(UpdateRoleSchema, request.body);
|
||||
if ("error" in parsed) return error(reply, parsed.error, 400);
|
||||
const body = parsed.data;
|
||||
|
||||
const existing = await prisma.roles.findUnique({ where: { id } });
|
||||
if (!existing) return error(reply, 'Role nenalezena', 404);
|
||||
const existing = await prisma.roles.findUnique({ where: { id } });
|
||||
if (!existing) return error(reply, "Role nenalezena", 404);
|
||||
|
||||
await prisma.roles.update({
|
||||
where: { id },
|
||||
data: {
|
||||
display_name: body.display_name ? String(body.display_name) : undefined,
|
||||
description: body.description !== undefined ? String(body.description) : undefined,
|
||||
},
|
||||
});
|
||||
|
||||
if (Array.isArray(body.permission_ids)) {
|
||||
await prisma.role_permissions.deleteMany({ where: { role_id: id } });
|
||||
await prisma.role_permissions.createMany({
|
||||
data: (body.permission_ids as number[]).map((pid) => ({
|
||||
role_id: id,
|
||||
permission_id: pid,
|
||||
})),
|
||||
await prisma.roles.update({
|
||||
where: { id },
|
||||
data: {
|
||||
display_name: body.display_name
|
||||
? String(body.display_name)
|
||||
: undefined,
|
||||
description:
|
||||
body.description !== undefined
|
||||
? String(body.description)
|
||||
: undefined,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
await logAudit({
|
||||
request,
|
||||
authData: request.authData,
|
||||
action: 'update',
|
||||
entityType: 'role',
|
||||
entityId: id,
|
||||
description: `Upravena role ${existing.name}`,
|
||||
});
|
||||
if (Array.isArray(body.permission_ids)) {
|
||||
await prisma.role_permissions.deleteMany({ where: { role_id: id } });
|
||||
await prisma.role_permissions.createMany({
|
||||
data: (body.permission_ids as number[]).map((pid) => ({
|
||||
role_id: id,
|
||||
permission_id: pid,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
return success(reply, { id }, 200, 'Role byla aktualizována');
|
||||
});
|
||||
await logAudit({
|
||||
request,
|
||||
authData: request.authData,
|
||||
action: "update",
|
||||
entityType: "role",
|
||||
entityId: id,
|
||||
description: `Upravena role ${existing.name}`,
|
||||
});
|
||||
|
||||
return success(reply, { id }, 200, "Role byla aktualizována");
|
||||
},
|
||||
);
|
||||
|
||||
// DELETE /api/admin/roles/:id
|
||||
fastify.delete<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('settings.roles') }, async (request, reply) => {
|
||||
const id = parseId(request.params.id, reply);
|
||||
if (id === null) return;
|
||||
fastify.delete<{ Params: { id: string } }>(
|
||||
"/:id",
|
||||
{ preHandler: requirePermission("settings.roles") },
|
||||
async (request, reply) => {
|
||||
const id = parseId(request.params.id, reply);
|
||||
if (id === null) return;
|
||||
|
||||
const existing = await prisma.roles.findUnique({ where: { id } });
|
||||
if (!existing) return error(reply, 'Role nenalezena', 404);
|
||||
const existing = await prisma.roles.findUnique({ where: { id } });
|
||||
if (!existing) return error(reply, "Role nenalezena", 404);
|
||||
|
||||
if (existing.name === 'admin') {
|
||||
return error(reply, 'Nelze smazat roli admin', 400);
|
||||
}
|
||||
if (existing.name === "admin") {
|
||||
return error(reply, "Nelze smazat roli admin", 400);
|
||||
}
|
||||
|
||||
await prisma.roles.delete({ where: { id } });
|
||||
await prisma.roles.delete({ where: { id } });
|
||||
|
||||
await logAudit({
|
||||
request,
|
||||
authData: request.authData,
|
||||
action: 'delete',
|
||||
entityType: 'role',
|
||||
entityId: id,
|
||||
description: `Smazána role ${existing.name}`,
|
||||
});
|
||||
await logAudit({
|
||||
request,
|
||||
authData: request.authData,
|
||||
action: "delete",
|
||||
entityType: "role",
|
||||
entityId: id,
|
||||
description: `Smazána role ${existing.name}`,
|
||||
});
|
||||
|
||||
return success(reply, { id }, 200, 'Role byla smazána');
|
||||
});
|
||||
return success(reply, { id }, 200, "Role byla smazána");
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user