aboutsummaryrefslogtreecommitdiff
path: root/api/src/modules/recurring-expenses/recurring-expenses.routes.ts
blob: 73594e8f338f98e4b1277198badf5c635183e6a5 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import type { FastifyPluginAsync } from 'fastify';
import { ZodError } from 'zod';
import {
  createRecurringExpenseSchema,
  recurringExpenseParamsSchema,
  recurringExpenseUserParamsSchema,
  updateRecurringExpenseSchema,
} from './recurring-expenses.schema';
import * as recurringService from './recurring-expenses.service';

const recurringExpensesRoutes: FastifyPluginAsync = async (fastify) => {
  /** GET /users/:userId/recurring-expenses — Lista todos os gastos recorrentes */
  fastify.get<{ Params: { userId: string } }>(
    '/users/:userId/recurring-expenses',
    async (request, reply) => {
      const { userId } = recurringExpenseUserParamsSchema.parse(request.params);
      const result = await recurringService.findRecurringExpensesByUser(userId);
      return reply.send({ data: result });
    },
  );

  /** GET /users/:userId/recurring-expenses/:id — Busca um gasto recorrente específico */
  fastify.get<{ Params: { userId: string; id: string } }>(
    '/users/:userId/recurring-expenses/:id',
    async (request, reply) => {
      const { userId, id } = recurringExpenseParamsSchema.parse(request.params);
      const expense = await recurringService.findRecurringExpenseById(userId, id);

      if (!expense) {
        return reply.status(404).send({ error: 'Gasto recorrente não encontrado.' });
      }
      return reply.send({ data: expense });
    },
  );

  /** POST /users/:userId/recurring-expenses — Cadastra um novo gasto fixo */
  fastify.post<{ Params: { userId: string } }>(
    '/users/:userId/recurring-expenses',
    async (request, reply) => {
      try {
        const { userId } = recurringExpenseUserParamsSchema.parse(request.params);
        const body = createRecurringExpenseSchema.parse(request.body);
        const expense = await recurringService.createRecurringExpense(userId, body);
        return reply.status(201).send({ data: expense });
      } catch (err) {
        if (err instanceof ZodError) {
          return reply.status(400).send({ error: 'Dados inválidos.', details: err.errors });
        }
        throw err;
      }
    },
  );

  /** PUT /users/:userId/recurring-expenses/:id — Atualiza um gasto recorrente */
  fastify.put<{ Params: { userId: string; id: string } }>(
    '/users/:userId/recurring-expenses/:id',
    async (request, reply) => {
      try {
        const { userId, id } = recurringExpenseParamsSchema.parse(request.params);
        const body = updateRecurringExpenseSchema.parse(request.body);
        const expense = await recurringService.updateRecurringExpense(userId, id, body);

        if (!expense) {
          return reply.status(404).send({ error: 'Gasto recorrente não encontrado.' });
        }
        return reply.send({ data: expense });
      } catch (err) {
        if (err instanceof ZodError) {
          return reply.status(400).send({ error: 'Dados inválidos.', details: err.errors });
        }
        throw err;
      }
    },
  );

  /**
   * PATCH /users/:userId/recurring-expenses/:id/toggle
   * Ativa ou desativa um gasto recorrente sem precisar deletar.
   */
  fastify.patch<{ Params: { userId: string; id: string } }>(
    '/users/:userId/recurring-expenses/:id/toggle',
    async (request, reply) => {
      const { userId, id } = recurringExpenseParamsSchema.parse(request.params);
      const expense = await recurringService.toggleRecurringExpense(userId, id);

      if (!expense) {
        return reply.status(404).send({ error: 'Gasto recorrente não encontrado.' });
      }
      return reply.send({ data: expense });
    },
  );

  /** DELETE /users/:userId/recurring-expenses/:id — Remove um gasto recorrente */
  fastify.delete<{ Params: { userId: string; id: string } }>(
    '/users/:userId/recurring-expenses/:id',
    async (request, reply) => {
      const { userId, id } = recurringExpenseParamsSchema.parse(request.params);
      const deleted = await recurringService.deleteRecurringExpense(userId, id);

      if (!deleted) {
        return reply.status(404).send({ error: 'Gasto recorrente não encontrado.' });
      }
      return reply.status(200).send({ data: deleted, message: 'Gasto recorrente removido com sucesso.' });
    },
  );
};

export default recurringExpensesRoutes;