aboutsummaryrefslogtreecommitdiff
path: root/api/src/modules/categories/categories.routes.ts
blob: cdf36026cd97b1238170f5197058408dbe4b8416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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;