diff options
| author | zwlucas <lucas.fariamo08@gmail.com> | 2026-07-25 21:23:29 +0000 |
|---|---|---|
| committer | zwlucas <lucas.fariamo08@gmail.com> | 2026-07-25 21:23:29 +0000 |
| commit | ef55d9bb24430610b60c1b77821307cd72c08c55 (patch) | |
| tree | e6935a810b34370b0a4c821e55f6397aac50c832 /api/src/modules/categories/categories.routes.ts | |
| download | helpdesk-master.tar.gz helpdesk-master.zip | |
Signed-off-by: zwlucas <lucas.fariamo08@gmail.com>
Diffstat (limited to 'api/src/modules/categories/categories.routes.ts')
| -rw-r--r-- | api/src/modules/categories/categories.routes.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/api/src/modules/categories/categories.routes.ts b/api/src/modules/categories/categories.routes.ts new file mode 100644 index 0000000..cdf3602 --- /dev/null +++ b/api/src/modules/categories/categories.routes.ts @@ -0,0 +1,92 @@ +/** + * categories.routes.ts + * + * CRUD de categorias com controle de acesso por role: + * GET /api/v1/categories → lista todas (qualquer autenticado) + * GET /api/v1/categories/:id → busca por ID (qualquer autenticado) + * POST /api/v1/categories → cria (admin) + * PATCH /api/v1/categories/:id → atualiza (admin) + * DELETE /api/v1/categories/:id → exclui (admin) + */ + +import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; +import { + categoryParamsSchema, + createCategoryBodySchema, + updateCategoryBodySchema, +} from './categories.schema.js'; +import { + listCategoriesHandler, + getCategoryHandler, + createCategoryHandler, + updateCategoryHandler, + deleteCategoryHandler, +} from './categories.controller.js'; + +// ─── Guard de role ──────────────────────────────────────────────────────────── +// Verificação de permissão após autenticação +async function requireAdmin(request: FastifyRequest, reply: FastifyReply) { + if (request.user.role !== 'admin') { + await reply.forbidden('Apenas administradores podem gerenciar categorias.'); + } +} + +async function categoriesRoutes(fastify: FastifyInstance): Promise<void> { + + // ── GET /categories ──────────────────────────────────────────────────────── + fastify.get('/', { + schema: { + tags: ['Categorias'], + summary: 'Lista todas as categorias', + }, + preHandler: [fastify.authenticate], + handler: listCategoriesHandler, + }); + + // ── GET /categories/:id ──────────────────────────────────────────────────── + fastify.get('/:id', { + schema: { + tags: ['Categorias'], + summary: 'Busca uma categoria pelo ID', + params: categoryParamsSchema, + }, + preHandler: [fastify.authenticate], + handler: getCategoryHandler, + }); + + // ── POST /categories ─────────────────────────────────────────────────────── + fastify.post('/', { + schema: { + tags: ['Categorias'], + summary: 'Cria uma nova categoria (admin)', + body: createCategoryBodySchema, + }, + preHandler: [fastify.authenticate, requireAdmin], + handler: createCategoryHandler, + }); + + // ── PATCH /categories/:id ────────────────────────────────────────────────── + fastify.patch('/:id', { + schema: { + tags: ['Categorias'], + summary: 'Atualiza uma categoria (admin)', + params: categoryParamsSchema, + body: updateCategoryBodySchema, + }, + preHandler: [fastify.authenticate, requireAdmin], + handler: updateCategoryHandler, + }); + + // ── DELETE /categories/:id ───────────────────────────────────────────────── + fastify.delete('/:id', { + schema: { + tags: ['Categorias'], + summary: 'Exclui uma categoria (admin)', + params: categoryParamsSchema, + }, + preHandler: [fastify.authenticate, requireAdmin], + handler: deleteCategoryHandler, + }); +} + +export default categoriesRoutes; |