diff options
Diffstat (limited to 'api/src/modules/dashboard/dashboard.routes.ts')
| -rw-r--r-- | api/src/modules/dashboard/dashboard.routes.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/api/src/modules/dashboard/dashboard.routes.ts b/api/src/modules/dashboard/dashboard.routes.ts new file mode 100644 index 0000000..d579ef2 --- /dev/null +++ b/api/src/modules/dashboard/dashboard.routes.ts @@ -0,0 +1,50 @@ +import type { FastifyPluginAsync } from 'fastify'; +import { z } from 'zod'; +import * as dashboardService from './dashboard.service'; + +const dashboardQuerySchema = z.object({ + month: z.coerce.number().int().min(1).max(12).optional(), + year: z.coerce.number().int().min(2020).optional(), +}); + +const dashboardParamsSchema = z.object({ + userId: z.coerce.number().int().positive(), +}); + +const dashboardRoutes: FastifyPluginAsync = async (fastify) => { + /** + * GET /users/:userId/dashboard + * Retorna o resumo financeiro completo do mês. + * + * Query params (opcionais): + * ?month=6&year=2025 → mês/ano específico + * Sem params → mês/ano atual + * + * Resposta: + * - Dados do usuário e salário + * - Lista de gastos recorrentes ativos + * - Lista de gastos avulsos do mês + * - Saldo disponível e percentual comprometido + */ + fastify.get<{ Params: { userId: string }; Querystring: Record<string, string> }>( + '/users/:userId/dashboard', + async (request, reply) => { + const { userId } = dashboardParamsSchema.parse(request.params); + const query = dashboardQuerySchema.parse(request.query); + + const now = new Date(); + const month = query.month ?? now.getMonth() + 1; + const year = query.year ?? now.getFullYear(); + + const data = await dashboardService.getDashboard(userId, month, year); + + if (!data) { + return reply.status(404).send({ error: 'Usuário não encontrado.' }); + } + + return reply.send({ data }); + }, + ); +}; + +export default dashboardRoutes; |